Drupal - drupal_mail() strips all html tags from emails

drupal_mail(), by default sends a mail as plain text.

If you print out the array $message['headers'], you will get something like:

array(
    'MIME-Version' => '1.0',
    'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
    'Content-Transfer-Encoding' => '8Bit',
    'X-Mailer' => 'Drupal',
  );

As you can see, the Content-Type in the mail header is text/plain; charset=UTF-8; format=flowed; delsp=yes. You can try by modifying this header in hook_mail_alter(), you could use one of the modules which allows you to send the mail as HTML, like HTML Mail, or Mime Mail.

Edit:

Following the comments by @Ayesh and @alechko and after looking into the APIs, I found out that the headers are not the only reason for sending the mail as plain text. Because drupal_mail() internally makes a call to drupal_mail_system(), which returns an object that implements the MailSystemInterface interface. The format() method implemented by drupal_mail_system() is responsible for stripping out the HTML from the emails. So, even if the headers of the mail show the formats to be HTML, the mails would be sent as plain text only. So, I guess one could not send an HTML mail using drupal_mail(). You should look for some other alternative (module) mentioned above.


In hook_mail also,you can define the headers as text/html,then you will get mail as an html format,as for example:

$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';

Give a try with HTML Mail module installed and configured.

Lets you theme your messages the same way you theme the rest of your website.

Tags:

7

Emails