thecodingmachine / swift-twig-mail-template
This package contains a implementation of the SwiftMailTemplate interface which use twig to render the mail message.
Installs: 30 566
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 9
Forks: 10
Open Issues: 1
Requires
- php: >=7.0.0
- thecodingmachine/swift-mail-template-interface: ^5.0
- twig/twig: ~1.27 || ~2.0
Requires (Dev)
- phpunit/phpunit: ^5.7.22
README
Swift Twig Mail Generator
This package takes a Twig template and generates a Swift mail from this template.
Installation
composer require thecodingmachine/swift-twig-mail-template
Once installed, you can start creating an instance of the SwiftTwigMailTemplate
class.
The SwiftTwigMailTemplate
represents a mail template that can generate Swift mails.
Example
Because we consider that an example is better than everything else...
Start by creating your mail template. Your template should have two blocks:
{% block subject %} Your suject {% endblock %} {% block body_html %} Body with HTML. {% endblock %}
If you want you can add another block containing your text body. This block is optional since we can get your the text body directly from the html one.
{% block body_text %} Body without HTML. {% endblock %}
Also, you can create a mail with a text body only (and no HTML):
{% block subject %} Your suject {% endblock %} {% block body_text %} Body with text only. {% endblock %}
Now, let's create a SwiftTwigMailTemplate
instance. This object will generate a SwiftMail
from the twig template.
// We assume that $twigEnvironment is a valid TwigEnvironment instance $twigSwiftMailTemplate = new SwiftTwigMailTemplate($twigEnvironment, 'path/to/template.twig'); // The renderMail method generates a Swift mail object. $swiftMail = $twigSwiftMailTemplate->renderMail(['paramKey' => paramValue]); // We fill the swift mail with additional information $swiftMail->setFrom('sender@example.com'); $swiftMail->setTo('recipient@example.com'); // We assume that $mailer is a valid Swift_Mailer instance $mailer->send($swiftMail);
You can also embed images directly into the email by using the embedImage function. By embedding an image into the email, most mail clients will display the image directly without printing the traditional warning.
Note: embedImage expects one parameter: the full path to the image file.
{% block subject %} Welcome ! {% endblock %} {% block body_html %} <img src="{{ embedImage(constant('ROOT_PATH')~'/public/img/header.png') }}" alt="header"> <h1>Hello World!</h1> {% endblock %}
Going further
The SwiftTwigMailTemplate
class has been designed with dependency injection in mind. Instances can be easily put in your container for easy reuse.
Furthermore, the SwiftTwigMailTemplate
class shares a lot with the Swift_Mail
class. You can:
- setting the from address
- setting the from name
- setting the to address
- setting the to name
- setting the Bcc address
- setting the Bcc name
- setting the Cc address
- setting the Cc name
- setting the ReplyTo address
- setting the ReplyTo name
- setting the max line size
- setting the priority
- setting the read receip to
- setting the return path