{"id":45933,"date":"2017-02-14T15:48:28","date_gmt":"2017-02-14T10:18:28","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=45933"},"modified":"2017-05-22T14:06:58","modified_gmt":"2017-05-22T08:36:58","slug":"how-to-send-html-emails-in-drupal-8","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-send-html-emails-in-drupal-8\/","title":{"rendered":"How to send HTML Emails in Drupal 8?"},"content":{"rendered":"<p>Thinking of how to setup emails in <a title=\"Drupal development\" href=\"http:\/\/www.tothenew.com\/wcm\/drupal-development-consulting-services\">Drupal 8<\/a> site? Are you searching\u00a0for different options? Did you get any success? If no, then this blog will be helpful.<\/p>\n<p>Gone are the days when a prospect used to get a simple one line email after submitting a\u00a0query\u00a0on 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\u00a0attractive HTMLised email which contains links and images.<\/p>\n<p>This blog will demonstrate how to send out HTML emails programmatically in Drupal 8.<\/p>\n<p>Header :<br \/>\nAll emails are preceded by header that identify routing information of that message.<br \/>\nHeader includes<\/p>\n<p>From: Sender&#8217;s name and email address (This is mandatory)<br \/>\nTo: Recipient&#8217;s name and email address (This is mandatory)<br \/>\nSubject: Subject heading<\/p>\n<p>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.<\/p>\n<p>Step 1: Define a function which will be called after your form is submitted (here we are taking example of contact form)<\/p>\n<p>[php]<br \/>\n\/**<br \/>\n* hook_form_alter()<br \/>\n*\/<\/p>\n<p>function mymodule_form_alter (&amp;amp;$form, \\Drupal\\Core\\Form\\FormStateInterface $form_state, $form_id){<br \/>\nif($form_id == &#8216;contact_message_personal_form&#8217;){<br \/>\n$form[&#8216;actions&#8217;][&#8216;submit&#8217;][&#8216;#submit&#8217;][] = &#8216;mymodule_html_mails&#8217;; \/\/custom name<br \/>\n}<br \/>\n}<\/p>\n<p>function mymodule_html_mails($form, \\Drupal\\Core\\Form\\FormStateInterface $form_state){<br \/>\n$send_mail = new \\Drupal\\Core\\Mail\\Plugin\\Mail\\PhpMail(); \/\/ this is used to send HTML emails<br \/>\n$from = \u2018from@gmail.com\u2019;<br \/>\n$to = \u2018to@gmail.com\u2019;<br \/>\n$message[&#8216;headers&#8217;] = array(<br \/>\n&#8216;content-type&#8217; =&gt; &#8216;text\/html&#8217;,<br \/>\n&#8216;MIME-Version&#8217; =&gt; &#8216;1.0&#8217;,<br \/>\n&#8216;reply-to&#8217; =&gt; $from,<br \/>\n&#8216;from&#8217; =&gt; &#8216;sender name &lt;&#8216;.$from.&#8217;&gt;&#8217;<br \/>\n);<br \/>\n$message[&#8216;to&#8217;] = $to;<br \/>\n$message[&#8216;subject&#8217;] = &quot;Subject Goes here !!!!!&quot;;<\/p>\n<p>$message[&#8216;body&#8217;] = &#8216;Hello,<br \/>\nThank you for reading this blog.&#8217;;<\/p>\n<p>$send_mail-&gt;mail($message);<br \/>\n}<\/p>\n<p>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:<\/p>\n<p>\/*<br \/>\n* hook_theme()<br \/>\n*\/<br \/>\nfunction hook_theme(){<br \/>\nreturn [<br \/>\n &#8216;mail-body&#8217; =&gt;<br \/>\n [<br \/>\n  &#8216;variables&#8217; =&gt; [<br \/>\n   &#8216;text 1&#8217; =&gt; NULL,<br \/>\n  ],<br \/>\n ]<br \/>\n];<br \/>\n}<br \/>\n[\/php]<\/p>\n<p>Now create a variable such as $theme_body, where we will define the theme which will be called.<\/p>\n<p>For Example :<\/p>\n<p>[php]<br \/>\n$theme_body = array(<br \/>\n&#8216;#theme&#8217; =&gt; &#8216;mail-body&#8217;,<br \/>\n&#8216;#text\u2019 =&gt; &#8216;Body of mail goes here\u2026\u2026..&#8217;,<br \/>\n);<br \/>\n[\/php]<\/p>\n<p>Here,<br \/>\n#theme : Is the name of theme template<br \/>\n#text : Is the variable which I want to use in my template, This #text can be anything it&#8217;s just a variable. We can define any number of variables.<\/p>\n<p>Now, message body will be something like<\/p>\n<p>[php]<br \/>\n$mail_body = drupal_render($theme_body);<br \/>\n$message[&#8216;body&#8217;] = $mail_body;<br \/>\n[\/php]<\/p>\n<p>Now, Create Mail Template with name : \u201cmail-body.html.twig\u201d under modules\/custom\/module_name\/templates\/mail-body.html.twig. And now here write your html body content.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thinking of how to setup emails in Drupal 8 site? Are you searching\u00a0for 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\u00a0query\u00a0on the website or filling out a contact form. The [&hellip;]<\/p>\n","protected":false},"author":1053,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":42},"categories":[3602],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/45933"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1053"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=45933"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/45933\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=45933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=45933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=45933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}