【发布时间】:2017-12-01 05:08:17
【问题描述】:
当我的 html 代码通过 php 文件通过电子邮件发送出去时,我无法正确显示它(没有 html 标签)。当收件人收到电子邮件时,我错过了什么$header 或$nmessage 才能正确显示?
我的 php 脚本如下所示,省略了 html 代码:
<?php
if(@$_REQUEST['weakPassword'] != "TEST")
{
echo "failed";
die();
}
function mail_attachment_from_file($the_file, $filename , $mailto, $from_mail, $from_name, $replyto, $subject, $message)
{
$content = $the_file;
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$nmessage .= $message."\r\n\r\n";
$nmessage .= "--".$uid."\r\n";
$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$nmessage .= $content."\r\n\r\n";
$nmessage .= "--".$uid."--";
mail($mailto, $subject, $nmessage, $header);
return true;
}
$to = @$_REQUEST['emailGuest'];
$from = @$_REQUEST['emailFrom'];
$subject = @$_REQUEST['emailSubject'];
$message = '
<html>
// HTML code
</html>';
$fileName = @$_REQUEST['fileName'];
$sep = sha1(date('r', time()));
// Read in our file attachment
$the_file = file_get_contents($_FILES['Filedata']['tmp_name']);
mail_attachment_from_file($the_file, $fileName , $to, $from, "BLAH", $from, $subject, $message);
echo "done";
?>
【问题讨论】:
-
Content-type:text/plain; charset=iso-8859-1这是你的罪魁祸首。您是说电子邮件是以纯文本格式发送的 -
您可能还想考虑使用 PEAR 或其他有助于为您的邮件创建正确的 MIME 边界的库。这将有助于确保它看起来不像垃圾邮件并符合 RFC 标准。还要记住,RFC 声明如果您发送 HTML 格式的电子邮件,也应该有适当的 TEXT MIME 版本。
-
@Twisty 你会推荐 PEAR 还是 phpmailer 库?
-
我个人是 PEAR 的粉丝。 pear.php.net/packages.php?catpid=14&catname=Mail 试试 Mail2 和 Mail_MIME2。
标签: php html email html-email