首页 > 解决方案 > Drupal 7:如何发送 HTML 电子邮件

问题描述

有人可以告诉我使用 Drupal 的功能发送 HTML 电子邮件时缺少什么吗?这是我的电话:

try{
        drupal_mail('my_module', 'forgot', $node->field_email_address['und'][0]['value'], language_default(), array('reset_key' => $key),'do-not-reply@myemailaddress.com');
      }catch(Exception $e){
        print_r($e->getMessage());die();
      }

这是功能:

function my_module_mail($key, &$message, $params) {

  $body = '<p>Click the link below to reset your password.</p>
  <p><a href="http://mywebsite.com/reset/'.$params['reset_key'].'">Click this link to reset your password</a></p>
';

//  $headers = array(
//    'MIME-Version' => '1.0',
//    'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
//    'Content-Transfer-Encoding' => '8Bit',
//    'X-Mailer' => 'Drupal'
//  );
//  $message['headers'] = $headers;
  $message['subject'] = 'Why wont this send html??';
  $message['headers']['Content-Type'] = 'text/html; charset=UTF-8;';
  $message['body'][] = $body;
  $message['from'] = 'do-not-reply@myemailaddress.com';

}

我厌倦了只是 html 标题和被注释掉的全套。我错过了什么?电子邮件发送正常,但它是纯文本。谢谢,让我知道!

标签: drupaldrupal-7drupal-modules

解决方案


你可以使用这个功能

function my_module_custom_drupal_mail($target = NULL, $from = null, $subject, $message, $attachment = NULL){
      $my_module = 'my_module';
      $my_mail_token = microtime();
      $message = array(
        'id'      => $my_module . '_' . $my_mail_token,
        'to'      => $target,
        'subject' => $subject,
        'body'    => array($message),
        'module'  => $my_module,
        'key'     => $my_mail_token,
        'from'    => "$from <email@email.com>",
        'headers' => array(
          'From'        => "$from <email@email.com>",
          'Sender'      => "$from <email@email.com>",
          'Return-Path' => "$from <email@email.com>",
          'Content-Type' => 'text/html; charset=utf-8'
        ),
      );
      if ($attachment) {
        $file_content = file_get_contents($attachment[0]);
        $message['params']['attachments'][] = array(
          'filecontent' => $file_content,
          'filename'    => $attachment[1],
          'filemime'    => $attachment[2],
        );
      }
      $system = drupal_mail_system($my_module, $my_mail_token);
      $message = $system->format($message);

      if ($system->mail($message)) {
        return TRUE;
      }
      else {
        return FALSE;
      }
    }

并称之为:

$body = '<p>Click the link below to reset your password.</p>
  <p><a href="http://mywebsite.com/reset/'.$params['reset_key'].'">Click this link to reset your password</a></p>
';

$subject  ='Why wont this send html??';
$from = 'myemail@email.com';

$sent = my_module_custom_drupal_mail($node->field_email_address['und'][0]['value'], $from, $subject, $body); 

随心所欲地定制它!:)


推荐阅读