The Swift Mailer module extends the basic e-mail sending functionality provided by Drupal by delegating all e-mail handling to the Swift Mailer library. This enables your site to take advantage of the many features which the Swift Mailer library provides, such as : - Sending e-mails directly through a SMTP server of your choice, a locally installed MTA agent such as sendmail or the mail functionality provided by PHP. - Sending HTML e-mails. - Adding file attachments to e-mails. - Adding inline images to e-mails. The module also lets you theme e-mails so that they reflect the general look and feel of your site and at the same time appear correctly in various e-mail browsers. Images required by the theme can be attached to the e-mail and displayed inline. The Swift Mailer module depends on the mailsystem module. This module is responsible for actually making the Swift Mailer module available to Drupal. 1.0 Configuration Please go to 'admin/config/swiftmailer/transport' to configure the Swift Mailer module. The module requires you to download the Swift Mailer library with composer. You should require the module itself with composer. Please the the documentation on Drupal.org for further information on this topic. https://www.drupal.org/docs/develop/using-composer/using-composer-to-manage-drupal-site-dependencies#managing-contributed After the module has been configured with the Swift Mailer library you are advised to make sure that the Swift Mailer library sends e-mails using the right transport option. You can choose between SMTP, sendmail (or any other locally installed MTA) and PHP's mail() function. Please observe the various configuration options which are available for each of the transport options. Tip! You are advised to configure Swift Mailer to send e-mails using a locally installed MTA if performance is a concern. A locally installed MTA will most often accept e-mails right away and queue them up for sending. Your users will thus not have to wait for an SMTP server to respond back with wether the e-mail will be sent or for some reason was rejected. You also have the chance to decide whether e-mail messages should be sent as plain text or HTML by default. Swift Mailer will also, if configured, respect any e-mail format dictated by the e-mail. Furthermore, you can also configure the character set which is to be used as default. You are advised not to change any of these options if you are uncertain about what they mean. Finally, you can test whether the Swift Mailer library sends e-mail messages correctly when the module has been configured and you are ready to put it into action. 2.0 Theming All e-mails sent by the SwiftMailer module can be themed. This is useful when e-mails should reflect the look and feel of the website it was sent from. The Swift Mailer modules delegates theming to the Drupal theming system. This means that you can theme e-mails the same way you theme other content. The theme hook you need to use when interacting with the theming system is 'swiftmailer'. 2.1 Theme File The easiest way to theme e-mails is to create a file named 'swiftmailer.html.twig' in your theme folder. Please see section 2.1.1 for an overview of the variables you can use in the 'swiftmailer.html.twig' file. The theme file should hold markup which wraps the actual content of the message. The below block of code demonstrates what a 'swiftmailer.html.twig' might look like. -- start of 'swiftmailer.html.twig' --
This is an automatically generated test e-mail.
'); //File three (managed by Drupal). $file_three = file_load(2); $message['files'][] = $file_three; $message['subject'] = t('Test'); $message['body'] = $text; break; } } It should be stressed that the module only supports attaching already existing files. In other words, dynamically generated files which are to be added as attachment to an e-mail needs to be generated and stored in a permanent or temporary location before it is provided as an attachment. A recommended way to handle temporary files is to utilise Drupals file system and mark the files as temporary. Drupal will then take care of deleting those files after a set amount of time. 3.1.1 The Attachment Hook The SwiftMailer module allows other modules to add attachments to e-mails. This is done through hook_swiftmailer_attach() which other modules need to implement in order to add attachments to e-mails. The below block of code demonstrates how a module can add attachments to an e-mail using hook_swiftmailer_attach(). function swiftmailer_swiftmailer_attach($key) { // Load file which is managed by Drupal. return file_load(2); } 3.2 Inline Images Adding inline images to e-mails is just as easy as adding files as attachments. You can choose between two ways of adding images. The first one assumes that your module does all the work, and the second assumes that your theme takes care of specifying which images to display as inline images. 3.2.1 Let your module do the work! Images which are to be attached to an e-mail need to be represented as instances of stdClass. This makes it easy for you to add image files that are managed by Drupal, as the file_load() function will return an stdClass instance which represents a given image file. However, in contrast to attachments, you will need to manually apply the field 'cid' to an image file which is to be used as an inline image. The 'cid' field needs to hold the id of the image file, and will be used to establish a link between the attached image and its display location in the e-mail body. All stdClass instances returned by Drupal which represents (image) files are populated with the fields 'uri', 'filename' and 'filemime'. Thus, if you would like to attach an image file that are not managed by Drupal, you then need to create an instance of stdClass and populate that instance with the fields 'uri', 'filename' and 'filemime'. Drupal's drupal_realpath() will be used to determine the actual location of the provided image file as given in the 'uri' field. Thus, image files from both public and private file systems can be attached to e-mails. The below example demonstrates both how to attach an image file that is managed by Drupal. /** * Implementation of hook_mail(). */ function modulename_mail($key, &$message, $params) { switch($key) { default: $logo_id = '390ffcm-sdfd94f'; $text[] = '; $text[] = t('This is an automatically generated test e-mail with an inline image..
'); //Inline image (managed by Drupal). $inline_image = file_load(1); $inline_image->cid = $logo_id; $message['images'][] = inline_image; $message['subject'] = t('Test'); $message['body'] = $text; break; } } 3.2.2 Let your theme do the work! Adding inline images from a theme might be even easier than adding inline images from a module. The first thing you need to do is to add markup to your the theme file which is responsible for showing the images. Now, the markup needs to be a little different than the markup required when adding inline images from a module. Instead of referencing images by CID, you simply reference images by their path. The below line of code demonstrates this. The essential part in the above lines of code is to make sure that the path of the image is prefixed with 'image:'. This tells the Swift Mailer module to embed that image file. The Swift Mailer module will automatically take care of assigning the image a CID to establish a link between the image and the markup. 3.3 Message Format You can specify wether a message should be sent as plain text ('text/plain') or HTML ('text/html'). Furthermore, the character set can also be set. The below example demonstrates how this can be achieved. /** * Send an e-mail * */ function test() { // Define message format. $p = [ 'format' => 'text/html', 'charset' => 'UTF-8', ]; // Send message. \Drupal::service('plugin.manager.mail')->mail('mymodule', 'key', 'test@test.com', \Drupal::languageManager()->getDefaultLanguage()->getId(), $p); } The above example will send an e-mail message as plain text. 4.0 HTML vs. plain Text You can configure Swift Mailer to send messages as HTML and/or plain text. E-mails which are configured to be sent as HTML messages will be themed, whereas e-mails configured to be sent as plain text will not be themed. You can configure Swift Mailer to automatically generate a plain text version of HTML e-mails. Plain text versions not be themed and will be displayed by e-mail clients not capable of showing HTML. This automatic conversion from HTML to plain text is carried out using html2text. Please refer to http://www.chuggnutt.com/html2text for more details. To configure Swift Mailer to carry out automatic conversion for HTML e-mails, go to admin/config/people/swiftmailer/messages and select the option 'Generate alternative plain text version' (Note! This requires the default message format to be set to HTML, or the variable $message['params']['convert'] to be set to 'TRUE'. If you would like to provide both HTML and plain text versions yourself, then simply provide the HTML version in $message['body'] and the plain text version in $message['plain']. Please make sure you set $message['params']['format'] to 'text/html'. The Swift Mailer module will not attempt to generate a plain text version if one is already available. 5.0 Custom settings/behavior If you want to add custom settings or behavior to the mailer or the message, before the it is sent, you can implement hook_swiftmailer_alter(). See swiftmailer.api.php for an example.