How to send HTML Markup content email in Drupal 8

Business Email

Sending an HTML markup email is making the mail recipient getting the cool feature when they see our content in a formatted one rather than sending the plain text mail. Everyone will prefer to send an HTML markup email for a newsletter or product promotions, logo revealing, or any other email campaign.

Drupal Email trigger

So wherever we want we can write with the below code to achieve to build mail content. Once the content of the mail body is ready we just need to hook it in the drupal mail function to trigger the formatted mail. Since it is HTML mail content we start with use the Markup statement in our code, it can any of your files. Then we need to define the mail manager to handle the mail function. We need to mention which module and what is the key to handle the mail function, so below code, we have mail manager’s mail function will run with the respective parameter like, module name, key, recipient address, and other parameters.

use Drupal\Core\Render\Markup;
.
.
.

$host = realpath(DRUPAL_ROOT);
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'MODUELNAME';
$key = 'KEY';
$to = $webform_data['email'];
$params['message'] = Markup::create('Welcome');
$params['subject'] = "SUBJECT";
$host = realpath(DRUPAL_ROOT);
$file = $host.'/'.drupal_get_path('module', 'MODULENAME') . '/pdf/test.pdf';
$params['attachments'] = [$file,];
$attachments->uri = $file;
$attachments->filename = 'file.pdf';
$attachments->filemime = finfo_file($finfo, $file);
$params['files'][] = $attachments;
\Drupal::logger('Test')->notice($file);
$langcode = \Drupal::currentUser()->getPreferredLangcode();
$send = true;
$result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);

if ( ! $result['result'] ) {
$message = t('There was a problem sending your email notification to @email', array('@email' => $to));
drupal_set_message($message, 'error');
\Drupal::logger('Error in Mail sending')->error($message);
}
else{
\Drupal::logger('Test')->notice('Mail has been sent to '.$to);
}

How does the mail trigger work?

In our custom module we are defining our email function with hook_mail, so from the above code mail manager’s mail function based on the module name and key parameter the below custom module’s hook will execute with key case switching. So all the parameters will be taken from the above trigger part and at the end, the recipient will get a successful email from our Drupal application programmatically.

/*
* Implements hook_mail().
*/
function hook_mail($key, &$message, $params) {
switch ($key) {
case 'key':
$from = \Drupal::config('system.site')->get('mail');
$message['from'] = $from;
$message['subject'] = $params['subject'];
$message['body'][] = $params['message'];
$message['headers']['Content-Type'] = 'text/html';
$message['headers']['Return-Path'] = $from;
$message['headers']['Sender'] = $from;
$message['headers']['Reply-to'] = $from;
$message['headers']['From'] = $from;
//$message['params']['files'] = $params['attachments'];
if (!empty($params['files'])) {
// Add an attachments to $message array.
$message['files'][] = $params['files'];
}
$body_data = [
'#theme' => 'your_template',
'#data' => $params['message']
];
$message['body'][] = \Drupal::service('renderer')->render($body_data);
break;
}
}

References

If you need more clarification you can refer to the below link to send an HTML email in your Drupal application.

https://drupalium.com/articles/send-email-with-attachment-without-contrib-modules-drupal-8

https://stefvanlooveren.me/blog/send-e-mail-managed-file-attachment-drupal-8

https://arrea-systems.com/Install_use_SwiftMailer_Drupal_8_(part_2_implementation)

Leave a Comment