drupal - 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 标题和被注释掉的全套。我错过了什么?电子邮件发送正常,但它是纯文本。谢谢,让我知道!
解决方案
你可以使用这个功能
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);
随心所欲地定制它!:)
推荐阅读
- visual-studio-code - 为什么 Prettier 扩展会自动格式化一些多行代码而不是其他代码?
- unity3d - Unity - 更改分辨率时文本会更改大小
- c++ - 合并排序未按预期与链表一起工作
- pagination - NextJS 中的 Apollo 分页
- powershell - 执行检查配置文件时出现powershell错误
- javascript - Gatsby 站点地图:GraphQLError:语法错误:预期名称,找到
- python - 将公司名称与 Python 中的索引匹配
- python - 如何通过提升弹性搜索中的显着词来改善搜索结果
- python - 在 Mac OS Anaconda 上运行 Kivy 2.00
- java - 如何使用 d8 编译工具保存清单文件?