Upgrade Your Drupal Skills
We trained 1,000+ Drupal Developers over the last decade.
See Advanced Courses NAH, I know EnoughInstall and use Swift Mailer in Drupal 8 (part 2 : implementation)
In previous article we explained how we installed the Swift Mailer module and its dependencies.
In this second part, let's see how we configure and implement it to use in our modules to send formated HTML mail with attachment.
First you will need to have Mail system module installed already. There is no particular issue or difficulty here.
1) Configure Swift Mailer
In Swift Mailer configuration (/admin/config/swiftmailer/transport) , we select the following options:
Transport:
Messages:
2) Custom module with email attachment
In our ERP application, we have a function that handles sharing of stored or online generated document via email.
In this function "mail_attachment()", we have an option to use Swift Mailer when available as module to handle the attachment.
Basically, the function handles the parameters received and prepare them for sending using a twig template.
The parameters used are: a list of email addreses, the uri of the file to attach, a text message and some option data (I.e. site name or logo). The part that is important here is the attachment preparation:
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$attachments = new stdClass();
$attachments->uri = '/path/to/file';
$attachments->filename = 'file name';
$attachments->filemime = finfo_file($finfo, $file);
$params['files'][] = $attachments;
Once we have compiled and formatted all information necessary for our email, we call the Drupal service to send our message with attachment:
Drupal::service('plugin.manager.mail')->mail(
'ek_admin',
'attachment',
[email protected],
\Drupal::languageManager()->getDefaultLanguage(),
$params,
$currentuserMail,
TRUE
);
In this piece of code we notice:
- The name of the module used ('ek_admin');
- The name of the key to identify the mail template used ('attachment').
In the above referred module, we have a hook_mail() function that handle the message based on the selected key:
function ek_admin_mail($key, &$message, $params) {
switch($key) {
case 'attachment':
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
$message['options'] = $params['options'];
$message['files'][] = $params['files'];
break;
}
}
The above parameters are important to build the message template.
3) The twig template
The template name format will follow this structure: swiftmailer--[module name]--[key].html.twig
We the create the file swiftmailer--ek_admin--attachment.html following the obove parameters that we used in our module, being the module name and the key.
This twig template can be designed as required with normal html tags to be sent as html mail. Here is a simplified example:
<table width="800px" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style='font-size:1.2em;padding:20px;vertical-align: bottom;'>{{ message.options.site }}</td>
<td ><img class="img-responsive" style="width:130px;float:right;" src="https://arrea-systems.com/Install_use_SwiftMailer_Drupal_8_%28part_2_imp...{{ message.options.logo }}" />
</td>
</tr>
</tbody>
</table>
<hr/>
<p>{{ 'Document'|t }} : {{ message.options.filename }}</p>
<p>{{ 'Document size'|t }} : {{ message.options.size }}</p>
Important: the trick here here is that the template has to be copied in the template folder of the theme used which may be a current limitation to the module. In our example, we use bartik theme:
4) Configure Mail System
In the Mail System configuration (), we select Swift Mailer as default mail handling system and keep the theme as current:
Now in custom configurations, we tell Mail Systems about our module and key described above and to use Swift Mailer when they are called:
After saving the configuration, we have our module and key registered:
5) Send a file
For simple example, we will take a file from a project page and email it to a user
And the result is the email received as follow, according to our Swift Mailer template:
Feel free to add your comments or own experience with Html email with Drupal 8.
Thank you.
About Drupal Sun
Drupal Sun is an Evolving Web project. It allows you to:
- Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
- Facet based on tags, author, or feed
- Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
- View the entire article text inline, or in the context of the site where it was created
See the blog post at Evolving Web