Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal 7 drupal_mail stripping out HTML?

I have a custom module that I'm trying to generate an HTML email from using the drupal_mail function (D7). Mail is coming through, and even shows text/html, however something somewhere appears to be stripping out the HTMl before it gets to an inbox.

First, in a function I'm building my title/body/other vars and sending to a custom function:

    $body = "We thought you'd like to know that ".$fullname." has marked your project as completed.
    <br /><br />
    Please visit the link at <a href='http://".$_SERVER['HTTP_HOST']."/survey/customer/".$customer[0]->unique_id."'>http://".$_SERVER['HTTP_HOST']."/survey/customer/".$customer[0]->unique_id."</a> to take the survey.";
    $returnMail = latch_send_mail('pro_realized',$customer[0]->customer_email,$user->mail,$title,$body);

Then I have the latch_mail latch_send_email functions:

function latch_mail($key, &$message, $params) {
    $headers = array(
    'MIME-Version' => '1.0',
    'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
    'Content-Transfer-Encoding' => '8Bit',
    'X-Mailer' => 'Drupal'
);

foreach ($headers as $key => $value) {
    $message['headers'][$key] = $value;
}

$message['body'][] = $params['body'];
$message['subject'] = $params['subject'];
}

and

function latch_send_mail($key,$to,$from,$title,$body,$headers='') {
    $params['body']=$body;
    $params['subject'] = t($title);
    return drupal_mail('latch', $key, $to, language_default(), $params, $from,TRUE);
}

I would expect the emails to come through with my a tags and br tags, but it comes through like this:

We thought you'd like to know that John Doe has marked your project as completed. Please visit the link at http://latch.local/survey/customer/34c91b8883cd70b32c65feb7adf9c393 [1] to take the survey. [1] http://latch.local/survey/customer/34c91b8883cd70b32c65feb7adf9c393

Somehow it's taking my links and turning them into footnotes while removing the br tags completely.

Any help you can provide would be appreciated. Thanks!

like image 253
Cory Fischer Avatar asked Aug 29 '12 09:08

Cory Fischer


2 Answers

Out of the box, Drupal can't send HTML email. In order for Drupal to support HTML email you need the HTML Mail module. http://drupal.org/project/htmlmail Once you have that all HTML should be sent as such.

like image 54
TelFiRE Avatar answered Sep 21 '22 14:09

TelFiRE


Here's an alternative method with a complete explenation. First of all, install and enable the Mime Mail module. You can read the README.txt for complete instructions on how to use it. I'll give you the short version.

You need to enable Mime Mail for your module. You can do this using hook_enable or hook_update_N in example.install:

function example_enable() {
  mailsystem_set(array(
    'example_examplekey' => 'MimeMailSystem',
  ));
}

When you go to admin/config/system/mailsystem you will see that a new entry has been added for your module:

Example module (examplekey key) class

MimeMailSystem

Now you don't need to specificy any text/html headers anymore, Mime Mail takes care of this. So you don't need this:

$headers['Content-Type'] = ...

If you want, you can add $message['plaintext'] to your mail for a non-HTML alternative, but this is not required.

That's it!

like image 22
Wim Mostrey Avatar answered Sep 21 '22 14:09

Wim Mostrey