繁体   English   中英

Drupal 7:如何发送 HTML 电子邮件

[英]Drupal 7: How to send HTML Email

有人可以告诉我使用 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 标题和被注释掉的完整集。 我错过了什么? 电子邮件发送正常,但它是纯文本。 谢谢,让我知道!

你可以使用这个功能

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); 

随心定制! :)

需要做几件事:

/**
 * Class SomeCustomModuleMailSystem Implements MailSystemInterface.
 *
 * Used to enable HTML email to be sent.
 */
class SomeCustomModuleMailSystem extends DefaultMailSystem {
  public function format(array $message) {
    $message['body'] = implode("\n\n", $message['body']);
    $message['body'] = drupal_wrap_mail($message['body']);
    return $message;
  }
}

这要做一次,所以可能在 hook_enable 或 hook_update 中:

  $current = variable_get('mail_system', ['default-system' => 'DefaultMailSystem']);
  $addition = ['some_custom_module' => 'SomeCustomModuleMailSystem'];
  variable_set('mail_system', array_merge($current, $addition));

正常调用 hook_mail,例如

/**
 * Implements hook_mail().
 */
function some_custom_module_mail($key, &$message, $params) {
  switch ($key) {
    case 'some_mail_key':
    $message['headers']['Content-Type'] = 'text/html; charset=UTF-8;';
    $message['subject'] = $params['subject'];
    $message['body'][] = $params['body'];
    break;
  }
}

最后用这样的东西调用它:

  // Set variables required for the email.
  $module = 'some_custom_module';
  $key = 'some_mail_key';
  $to = $email = 'thetoaddress@something.com';
  $language = language_default();
  $params['subject'] = 'Email subject';
  $params['body'] = '<html><body>The HTML!</body></html>';
  $from = 'thefromaddress@something.com';
  $send = TRUE;

  // Send the mail and log the result.
  $result = drupal_mail($module, $key, $to, $language, $params, $from, $send);
  if ($result['result'] === TRUE) {
    watchdog('some_custom_module', 'HTML email successfully sent.', [], WATCHDOG_INFO);
  }
  else {
    watchdog('some_custom_module', 'HTML email failed to send', [], WATCHDOG_ERROR);
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM