0

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>myemail@email.com</strong>,&nbsp;</p>

<p>Your friend <strong>another_email@email.com</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.

  • Take a look @drupal.org/node/900794 It's an instruction about creating html mails in modules for drupal 7 – d1m5n Jun 11 '14 at 11:53
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 :)

|improve this answer|||||
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);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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