[英]HTML/Text E-mail with attachment using PHP
我正在尝试建立一个Web应用程序,该应用程序在完成时向用户发送带有附件的电子邮件。 我希望电子邮件使用HTML和文本格式,以确保每个人都可以查看它,而无论其邮件设置如何。 我无法使其正常工作,所以我想知道是否有人可以发现我的代码存在的问题。
我正在使用的代码如下。 这是另一个站点的模板,但是我已经做了大量的阅读工作,所以我对它们的工作原理有一个大概的了解。 不幸的是,对解决问题的理解还不够!
$firstname = $_POST['firstname'];
$email = $_POST['email'];
//define the receiver of the email
$to = $email;
//define the subject of the email
$subject = "$firstname, thanks for using the One Minute Leveller";
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: The Xenon Group\r\nReply-To: no-reply@xenongroup.co.uk";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('Level3info.pdf')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
TEXT E-MAIL GOES HERE. ACTUAL CONTENT REMOVED
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<html><h1>HTML CONTENT GOES HERE. ACTUAL CONTENT REMOVED</h1></html>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/pdf; name="Level3info.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
mail( $to, $subject, $message, $headers );
有人可以帮忙吗?
首先,不要重新发明轮子-使用诸如Pear Mail http://pear.php.net/package/Mail/redirected之类的库
其次,您的标头之间有空格,这是不允许的。
从邮件RFC:
在此标准中,可以自由插入注释和FWS的几个位置。 为了适应该语法,为可能出现注释和/或FWS的位置定义了“ CFWS”的附加标记。 但是,在此标准中出现CFWS的地方,绝不能以这样的方式插入CFWS:折叠的标头字段的任何行都完全由WSP字符组成,别无其他。
亲切的问候,
约翰
这是我使用的有效方法。 它没有考虑文本版本与HTML版本...仅考虑HTML版本,但也许它将引导您找到答案:
<?php
$mime_boundary = md5(uniqid(time()));
// to
$to = $_REQUEST["to"];
if (is_array($to)) $to = implode(", ", $to);
// from
$header = "From:". ($_REQUEST["fromName"] == "" ? $_REQUEST["fromAddress"] : "{$_REQUEST["fromName"]} <{$_REQUEST["fromAddress"]}>") ."\r\n";
$header .= "MIME-Version:1.0\r\n";
$header .= "Content-Type:multipart/mixed; boundary=\"{$mime_boundary}\"\r\n\r\n";
// message
$header .= "--{$mime_boundary}\r\n";
$header .= "Content-Type:text/html; charset=\"ISO-8859-1\"\r\n";
$header .= "Content-Transfer-Encoding:7bit\r\n\r\n";
$header .= "<html><head><style type=\"text/css\">body { font:10pt Arial; }</style></head><body>". str_replace("\r", "", str_replace("\n", "<br />", $_REQUEST["message"])) ."</body></html>\r\n\r\n";
// attachment
$attachments = (array)$_REQUEST["attachment"];
$attachmentNames = (array)$_REQUEST["attachmentName"];
for ($i = 0; $i < count($attachments); $i++)
{
if (empty($attachmentNames[$i]))
$attachmentNames[$i] = "attachment". ($i+1) .".txt";
if (!base64_decode($attachments[$i], true))
$attachments[$i] = base64_encode($attachments[$i]);
else
$attachments[$i] = str_replace("\r", "", str_replace("\n", "", str_replace(" ", "+", $attachments[$i])));
$header .= "--{$mime_boundary}\r\n";
$header .= "Content-Type:application/octet-stream; name=\"{$attachmentNames[$i]}\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; filename=\"{$attachmentNames[$i]}\"\r\n";
$header .= "{$attachments[$i]}\r\n\r\n";
}
if (mail($to, $_REQUEST["subject"], "", $header))
echo "SUCCESS";
else
echo "FAIL";
?>
也许您想看看PHPmailer 。 它提供了您要使用的所有功能,但是比内置的mail()函数更简洁,最新的标准方式。 并且那里有一些很好的教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.