1

I have a problem while sending a HTML email through my module, Here is my code below:

.install file

function invite_an_user_enable() {
  $current = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
  $addition = array('invite_an_user' => 'invite_an_userMailSystem');
  variable_set('mail_system', array_merge($current, $addition));
}

function invite_an_user_disable() {
  $mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
  unset($mail_system['invite_an_user']);
  variable_set('mail_system', $mail_system);
}

and here is the .module file:

class invite_an_userMailSystem extends DefaultMailSystem {
  public function format(array $message) {
    $message['body'] = implode("\n\n", $message['body']);
    $message['body'] = drupal_wrap_mail($message['body']);
    return $message;
  }
}

Here is the email that I got:

<p>Hi <strong>[email protected]</strong>,&nbsp;</p>

<p>Your friend <strong>[email protected]</strong>, invited you in <strong>My Site Name</strong>. Please cleck on the link to accept the invitation below:</p>

<p>http://something.com/referral/3139323e</p>

<p>&nbsp;</p>

<p>Thanks,<br />
My Site</p>

Now the problem is that, when I received the email I can see all the tags in my email. And the header of the email is

MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed; delsp=yes
Content-Transfer-Encoding: 8Bit
X-Mailer: Drupal

I believe this should be text/html rather than text/plain. Am I doing something wrong? Is there any different method to send HTML mail? Feel free to ask any questions.

1
  • Take a look @drupal.org/node/900794 It's an instruction about creating html mails in modules for drupal 7
    – d1m5n
    Jun 11, 2014 at 11:53

2 Answers 2

0

Solved that. I added a line on my code i.e $message['headers']['Content-Type'] = 'text/html; charset=UTF-8;'

class invite_an_userMailSystem extends DefaultMailSystem {
  public function format(array $message) {
    $message['headers']['Content-Type'] = 'text/html; charset=UTF-8;';
    $message['body'] = implode("\n\n", $message['body']);
    $message['body'] = drupal_wrap_mail($message['body']);
    return $message;
  }
}

This is now working for me.. Yeppi :)

0

Add your custom module hook_mail:

/**
 * Implements hook_mail().
 */
custom_module_mail($key, &$message, $params) {

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

Make the "drupal_mail" call as follows:

drupal_mail('custom_module', 'unique_key_first_arg', $to, user_preferred_language($currentUser), ['body' => 'content goes here', 'subject' => 'SUBJECT LINE goes here'], $from, TRUE);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.