我正在尝试开发一种发送电子邮件的功能,该功能可用于我网站的各个部分。我希望电子邮件经过 SMTP 身份验证,所以我使用的是 Pear。
我的函数的一个参数被设置为确定电子邮件应该是纯文本、html 还是两者都发送(多部分/替代)....
if($set_content_type == 'text'){
$content_type = 'text/plain';
} elseif($set_content_type == 'html'){
$content_type = 'text/html';
} elseif($set_content_type == 'html_and_text'){
$boundary = uniqid();
$content_type = 'multipart/alternative; boundary=' . $boundary;
}
然后我使用 Mail_mime 发送电子邮件...
$mime = new Mail_mime();
$mime->setHTMLBody($html);
$mime->setTXTBody($text);
$body = $mime->get();
$host = "myhost";
$port = "25"; // 8025, 587 and 25 can also be used.
$username = "myusername";
$password = "mypassword";
$headers = array (
'From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type:' => $content_type);
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
我的问题:在这段代码中,我可以在哪里设置消息的各个部分以遵循多部分/替代 mime 类型并正确使用边界,就像这个答案 (Different Content Types in email) 中显示的那样?强>
最佳答案
您必须在 $mime 对象上设置 ContentType
$mime = new Mail_mime();
$mime->setHTMLBody($html);
$mime->setTXTBody($text);
$mime->setContentType($content_type);
$body = $mime->get();
现在我建议您使用另一种邮件而不是来自 pear 的邮件。 Pear 是一个有点老的数据包管理器,一些应用程序不再支持它,如 PHPUnit 和 Symfony。
最好使用另一个可以通过 composer 安装的。你可以看看Swiftmailer
关于php - 使用Pear Mail发送纯文本和html邮件(如何设置 "Content-Type"并正确使用边界),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38747254/