How to send HTML Emails in Drupal 8?
Thinking of how to setup emails in Drupal 8 site? Are you searching for different options? Did you get any success? If no, then this blog will be helpful.
Gone are the days when a prospect used to get a simple one line email after submitting a query on the website or filling out a contact form. The world is changing rapidly and so is the communication mediums. Now every organization wants to acknowledge their visitors/prospects when he/she fills up a form on your website by sending out an attractive HTMLised email which contains links and images.
This blog will demonstrate how to send out HTML emails programmatically in Drupal 8.
Header :
All emails are preceded by header that identify routing information of that message.
Header includes
From: Sender’s name and email address (This is mandatory)
To: Recipient’s name and email address (This is mandatory)
Subject: Subject heading
Below we will explain how to send HTML email after successful Contact form is submitted similarly this can be used to setup and send emails when any a different type of form is submitted.
Step 1: Define a function which will be called after your form is submitted (here we are taking example of contact form)
[php]
/**
* hook_form_alter()
*/
function mymodule_form_alter (&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
if($form_id == ‘contact_message_personal_form’){
$form[‘actions’][‘submit’][‘#submit’][] = ‘mymodule_html_mails’; //custom name
}
}
function mymodule_html_mails($form, \Drupal\Core\Form\FormStateInterface $form_state){
$send_mail = new \Drupal\Core\Mail\Plugin\Mail\PhpMail(); // this is used to send HTML emails
$from = ‘from@gmail.com’;
$to = ‘to@gmail.com’;
$message[‘headers’] = array(
‘content-type’ => ‘text/html’,
‘MIME-Version’ => ‘1.0’,
‘reply-to’ => $from,
‘from’ => ‘sender name <‘.$from.’>’
);
$message[‘to’] = $to;
$message[‘subject’] = "Subject Goes here !!!!!";
$message[‘body’] = ‘Hello,
Thank you for reading this blog.’;
$send_mail->mail($message);
}
This will send HTML email to users. Now what if I have huge body content? Should I write whole body content in the above function? Is it good practice? No, not at all. We should ideally create template for the body content using hook_theme() function of Drupal. Below is the example:
/*
* hook_theme()
*/
function hook_theme(){
return [
‘mail-body’ =>
[
‘variables’ => [
‘text 1’ => NULL,
],
]
];
}
[/php]
Now create a variable such as $theme_body, where we will define the theme which will be called.
For Example :
[php]
$theme_body = array(
‘#theme’ => ‘mail-body’,
‘#text’ => ‘Body of mail goes here……..’,
);
[/php]
Here,
#theme : Is the name of theme template
#text : Is the variable which I want to use in my template, This #text can be anything it’s just a variable. We can define any number of variables.
Now, message body will be something like
[php]
$mail_body = drupal_render($theme_body);
$message[‘body’] = $mail_body;
[/php]
Now, Create Mail Template with name : “mail-body.html.twig” under modules/custom/module_name/templates/mail-body.html.twig. And now here write your html body content.
very useful
I read carefully this article and comments too. But, I don’t know where I define this variable $theme_body. Please guide.
around line 26 of the first snippet of code
Now create a variable such as $theme_body, where we will define the theme which will be called.
I can’t understand where in which file I must use this code and where.
In above example under function mymodule_html_mails you will define variable $theme_body. Then in hook_theme function which you will define in .module file will define theme.
Still, it’s not working for me. Please help.
Now create a variable such as $theme_body, where we will define the theme which will be called.
I can’t understand in which file I must do that. Can you explain it for me?
you can do this in .module file
Thanks for posting an excellent article. It was really helpful.
I spent whole evening looking how to send html mail without additional modules and finally found this post. Thank you very match.
Looks interesting but completely useless because of the ‘gt:s’
Thanks for your comment , remove all ‘gt:s’.
I’m seeing a lot of >:s and <:s.
Thanks for your comment removed all >:s and <:s.
Almost perfect except that it doesn’t specify the mail language.
Tried to send in Czech and here’s my modified version, text shown garbled anyway 🙁
$send_mail = new \Drupal\Core\Mail\Plugin\Mail\PhpMail(); // this is used to send HTML emails
$message[‘headers’] = array(
‘content-type’ => ‘text/html’,
‘MIME-Version’ => ‘1.0’,
‘reply-to’ => $from
);
$message[‘to’] = $order->getEmail();
$message[‘from’] = ‘noreply@abc.com’;
$message[‘subject’] = “Email subject”;
$message[‘body’] = ‘Děkujeme za Vaši objednávku. ‘;
$message[‘language’] = \Drupal::currentUser()->getPreferredLangcode();
$send_mail->mail($message);