I am creating a simple module which will be sending HTML emails to users for deals in our website.

I have searched a lot, but I can't find a solution proper solution for Drupal. Now I can send mails but plain text.

What is a step-by-step procedure to send HTML emails in Drupal 7?

function example_form_submit( $form,&$form_state) {
    $mailto =$friend_email;  //gift to a friend
    $mailfrom ='[email protected]';
    $subject = "another message for  HTML email from example.com";
    $body="<h2 style='font-size:28px;color:red;'>Hello EMAIL, i here i want to be <b>bolded</b></h2>";
    if (drupal_mail('example', 'send_gift', $mailto, language_default(),$params,$mailfrom,TRUE)) {
        drupal_set_message(t('Your gift was sent successfully!!!'));
    else {
        drupal_set_message(t('<font color="red">Error occured while sending your mail!!!</font>'));

function example_mail($key,&$message,$params) {
    $language = $message['language'];
    switch ($key) {
        case 'send_gift':
            $message['subject']=t($params['subject'], $var, $language->language);
            $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
  • You are not passing Full HTML page
    – GoodSp33d
    Commented Mar 31, 2012 at 14:23

5 Answers 5


Like geoff mentioned one option would be to use the Mime Mail module, but you can also enable your custom module to send HTML e-mails with some extra code. In short you'd need to alter the mail_system variable and write your own implementation of MailSystemInterface. An example of that is the following code.

Inside 'mymodule.install':

function mymodule_enable() {
  $current = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
  $addition = array('mymodule' => 'MymoduleMailSystem');
  variable_set('mail_system', array_merge($current, $addition));

function mymodule_disable() {
  $mail_system = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
  variable_set('mail_system', $mail_system);

Inside 'mymodule.module':

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

You'd have to replace mymodule instances with your actual module's name then disable and re-enable your module to get the variable changed after which things should be working. For a complete example you can check out my answer here. Additionally this page on drupal.org is from where I originally got all the information from.

  • 2
    i followed your instructions + the complete example you gave now everything is working... Thank you very much.
    – mrcniceguy
    Commented Apr 3, 2012 at 17:47

Mime Mail module is almost certainly what you are looking for.

This is a Mime Mail component module (for use by other modules).

  • It permits users to receive HTML email and can be used by other modules. The mail functionality accepts an HTML message body, mime-endcodes it and sends it.
  • If the HTML has embedded graphics, these graphics are MIME-encoded and included as a message attachment.
  • Adopts your site's style by automatically including your theme's stylesheet files in a themeable HTML message format
  • If the recipient's preference is available and they prefer plaintext, the HTML will be converted to plain text and sent as-is. Otherwise, the email will be sent in themeable HTML with a plaintext alternative.
  • Allows you to theme messages with a specific mailkey.
  • Converts CSS styles into inline style attributes.
  • Provides simple system actions and Rules actions to send HTML email with embedded images and attachments.
  • 5
    i have downloaded the mimemail module+mail system... will you be kind to please explain to call the mimemail function to my module as the code above.
    – mrcniceguy
    Commented Mar 31, 2012 at 12:34

None of the above answers worked for me, so here is my take on the original question.

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() {
    'example_send_gift' => 'MimeMailSystem',

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

Example module (send_gift key) class


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

$message['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!


You might also want to look at the Swift Mailer module. It lets you send HTML e-mails with both inline and regular attachments (such as images or files). Furthermore, it is also capable of automatically generating plain text versions based on the HTML e-mail.

You can have a look at it http://www.drupal.org/project/swiftmailer

For the record: I'm the author and maintainer of the module.


You are not sending proper HTML , you are only including H1 tags and B tags, you probably need to include Full HTML tags as well starting from
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  • thx for ur comment,i used @Dooshta example,also added the proper html as u suggested.. everything is working.
    – mrcniceguy
    Commented Apr 3, 2012 at 17:49
  • I'm sure there is no harm in using a doctype & fully validating HTML, but it is not required to send an HTML email through drupal. I just removed the doctype & html tag and the email rendered well in Gmail & Outlook. However, it is very important to have Dooshta's fix in place: before I implemented those changes I was not able to generate any html at all.
    – doub1ejack
    Commented Nov 13, 2012 at 21:06

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.