我有一个多部分的电子邮件脚本,它需要一个POSTed电子邮件地址,并以HTML和/或纯文本形式向他们发送一封简单的电子邮件。它在Gmail和Outlook中正确显示,但不能在eM中正确显示(甚至无法通过Communigate服务器)。代码:
<?php
$email_address = addslashes($_POST['email_address']);
if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
header("Location: ./?error=invalid-email");
exit();
}
$subject_line = "This is a test multi-part email";
$boundary = uniqid();
$headers = "MIME-Version:1.0'r'n";
$headers .= "From: Maggie Multipart <web@ukipme.com>'r'n";
$headers .= "To: " . $email_address . "'r'n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "'r'n";
$message = "This is a MIME encoded message.";
$message .= "'r'n'r'n--" . $boundary . "'r'n";
$message .= "Content-Type: text/plain;charset=utf-8'r'n'r'n";
$message .= "Hello,'nThis is a test email, the text/plain version.'n'nRegards'nMaggie Multipart";
$message .= "'r'n'r'n--" . $boundary . "'r'n";
$message .= "Content-Type: text/html;charset=utf-8'r'n'r'n";
$message .= "<p>Hello,<br>This is a test email, the text/html version.</p><p>Regards<br><strong>Maggie Multipart</strong></p>";
$message .= "'r'n'r'n--" . $boundary . "--";
mail("", $subject_line, $message, $headers);
header("Location: ./?success=email-sent");
exit();
// var_dump($_POST);
?>
在 eM 中接收消息,如下所示:
内容类型:文本/纯文本;字符集=utf-8
你好
这是一封测试电子邮件,文本/纯文本版本。
问候
玛姬多部分
但是,eM 设置为接收 HTML 电子邮件(并且经常这样做)。有人可以帮我解决这个问题吗?我是否缺少任何标题?
我对创建电子邮件的一般建议是:不要自己做(无论如何使用一些字符串连接函数/运算符)。我选择的武器是 swiftmailer,但网络上还有其他可行的库。
<?php
require_once('autoload.php'); // swiftmailer was installed via Composer
$message = Swift_Message::newInstance('This is a test multi-part email')
->setBody(
"Hello,'nThis is a test email, the text/plain version.'n'nRegards'nMaggie Multipart",
'text/plain',
'utf-8'
)
->addPart(
"<p>Hello,<br>This is a test email, the text/html version.</p><p>Regards<br><strong>Maggie Multipart</strong></p>",
'text/html',
'utf-8'
)
->setFrom(array('...@...' => '...'))
->setTo(array('...@...' => '...'));
$transport = Swift_SmtpTransport::newInstance('MSERV', 25, 'tls')
->setUsername('...')
->setPassword('...');
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);