Drupal 8 sending custom html mailers

Many of developers faced situation where you have to send custom html formatted emails from Drupal 8 website. But by default Drupal strips html content from email template.

In websites depending on branding requirement there will be multiple emails need to be send to customers with different lay outs. For example if you are creating contact form you need to send a html email to customers which include images and graphic elements to show details in awesome way.

Also another example the same website have an subscribe form when customers subscribed we need to send a confirmation email to customer email which have confirmation message with services offered by website owner represented in rich graphics including images and buttons and links to other pages.

Normally you will be sending through your custom modules. So by default Drupal strips all html tags and sends mail as plain text. Here we going to set up email system in which you can send multiple custom HTML templates as emails.

Here in step by step we are setting custom modules and latter writing code to send emails with templates.
Make sure smtp server is configured in your server and confirm test mails are sending from Drupal.

Before going to coding part of sending custom HTML mail we have to do some configuration and enable some modules in order to avoid striping HTML tags by Drupal system.

Below are the configurations that we are going to do.

  • Install and configure Mail system module
  • Install and configure Mime Mail module

Mail system module

Install and enable Mail system module

https://www.drupal.org/project/mailsystem

Provides an Administrative UI and Developers API for managing the used mail backend/plugin.
you can see configuration of mail system in below menu path.

Configuration – > System -> Mail System

Confirm below configurations are enabled in mail system

Mime Mail Module

Now we need to install below module which enable to send HTML mails.

Without this if you send emails , Drupal will remove all html tags and send mail as plain text.

We can download this module from below URL.

https://www.drupal.org/project/mimemail

Download module and place in contrb folder of project folder

Click on Extend on menu which will display all  modules and enable mimeemail module , hit install button.

Configuring Mime Mail module

After installation complete, go to Mail system as given below

 

Configuration – > System -> Mail System

 

You can see  module specific configurations in this page.Say YOUR_MODULE is your custom module name.

Select YOUR_MODULE   from drop down you can add multiple modules from where you want to send customized template emails.

Then from Sender Plugin Drop down select  mimeemail

Fromatter plugin keep it as default.

Then Click Add  and save configurations.

Go to below menu and confirm drop down selected as Basic HTML.

Configuration – > System ->mimeemail

Drop down should be selected as Basic HTML

Now All configurations done for sending custom HTML emails without stripping html  elements.  Now we are  going to prepare code for sending multiple emails with different email templates from a custom module.

Now we are going to prepare code to send mails. We group these mail to below steps.

  • Prepare custom PHP template file
  • hook_mail() to define email templates
  • Send the mail using mail manager

 

Custom PHP template file

 Before going to create hook_mail() function here we are creating HTML body of mailer in a separate PHP file and assign to a variable. This will be very help full for managing large email templates indented for marketing purposes.

So if you want to customise HTML template, means not need to edit custom module code every time , you have just edit this php template file and upload it in to the server.

For this purpose we creating folder called mail in theme folder where we are placing php template file.

We are creating a php file which have mail template in an variable . we will use this variable in our hook_mail()

Suppose php fiile name is   campaigner.php . in this initially we will take path of images in theme folder. This image path is used to load images an icons in html template also html template is assigning to a php variable this variable will be used in hook_mail() as message body.

Please see staring part of php code in campaigner.php

Get image paths

 

Next we will include this php file in hook mail.

 

hook_mail() to define email templates

Now we need to implement hook_mail() to define email templates ,  we have to create

Hook function YOUR_MODULE _mail in our custom module here YOUR_MODULE is the name of your custom module.

Please see below hook function

We can create multiple template in  below function using switch statement.

Here we are using ‘form_campaigner’ key for our custom html template.

Here, the variables $key, $message and params can be described as :

  • $key: An identifier of the mail template.
  • $message: An array to be filled in. Elements in this array include: id, to, subject, body, from, headers
  • $params: An array of parameters supplied by the caller of drupal_mail().

 

Send the mail using mail manager

Now lets create a custom function to send the mail using mail manager.

place this this function in your module and call where ever you want to send an email by passing a key and parameters.

 

Now You will receive mails with exact template that you passed.

Conclusion

There are different articles which follows different methods to send emails through Drupal 8. Here I am using same methods with additional modules in order to avoid one common problem , striping of HTML tags while receiving email templates.

You can download a Free Ebook using below link.

Downlod Free Ebook on  Custom html mailers in Drupal

Get Free E-book
Get a free Ebook on Drupal 8 -theme tutorial
I agree to have my personal information transfered to MailChimp ( more information )

You may also like...