我正在使用下面的代码通过 PHP 发送带有附件的电子邮件。(只是一个中等大小的图像和一个没有任何垃圾内容的小描述)
问题是某些服务将电子邮件标记为垃圾邮件或完全拒绝它。
我是 PHP 新手,在 SO 上找到了这段代码。标题有什么问题或可能导致这种情况吗?
<?php
$file = $_POST["thefile"];
$text_message = $_POST["themessage"];
$subject = $_POST["thesubject"];
$from = $_POST["thesender"];
$to = $_POST["theaddress"];
$attachment=uniqid(rand(), true) . '.png';
$headers="From: $from\r\n";
$headers.="Reply-to: $from\r\n";
$headers.="Return-Path: $from\r\n";
if (isset($_ENV["SERVER_NAME"]))
$headers.="Message-Id: <" . md5(uniqid(microtime())) . "@" . $_ENV["SERVER_NAME"] . ">\r\n";
else
$headers.="Message-Id: <" . md5(uniqid(microtime())) . "@" . 'unknown' . ">\r\n";
$headers.="Date: " . date("r") . "\r\n";
$headers.="X-Mailer: PHP\r\n";
if (isset($_ENV["REMOTE_ADDR"]))
$headers.="X-SenderIP: " . $_ENV["REMOTE_ADDR"] . "\r\n";
else
$headers.="X-SenderIP: " . 'unknown' . "\r\n";
if (isset($_ENV["SERVER_NAME"]))
$headers.="X-WebSite: " . $_ENV["SERVER_NAME"] . "\r\n";
else
$headers.="X-WebSite: " . 'unknown' . "\r\n";
$headers.="X-Script: SWF_Generator\r\n";
$bound_text=md5(uniqid(time()));
$headers.="MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed; boundary=\"PHP-mixed-$bound_text\"\r\n";
$message="--PHP-mixed-$bound_text\r\n"
."Content-Type: text/html; charset=\"utf-8\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
."<html><head></head><body>"
."<div style=\"font-family: Arial, Helvetica, sans-serif; font-size : 1.3em; color: #000000;width: 100%;text-align: left;\">$text_message</div></body></html>\r\n\r\n"
."--PHP-mixed-$bound_text\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment; filename=\"$attachment\"\r\n"
."Content-Type: image/png; name=\"$attachment\"\r\n\r\n"
.chunk_split($file)
."\r\n\r\n"
."--PHP-mixed-$bound_text--\r\n\r\n";
mail($to,$subject,$message,$headers);
}
?>
谢谢你。