它位于Windows Server 2016上。我在MailEnable上为127.0.0.1启用了中继,因此它可以正常运行,并且不需要凭据(已使用SMTP Tester对其进行了测试)。 PHP站点也在同一台服务器上。我不是在寻找其他解决方案,比如实现另一个PHP邮件库。
答案 0 :(得分:1)
我花了很多时间创建可以带或不带附件发送的PHP文件。我在Windows Server 2016,IIS 10,PHP 5.6上测试了这个。也许这会对某人有所帮助。
关于MailEnable; MailEnable不是问题所在。我使用过的旧代码来自较旧的PHP版本,但不起作用。对于MailEnable,您只需启用127.0.0.1的中继(SMTP属性),默认情况下已打开,如果您从127.0.0.1发送,则不需要使用身份验证。
<?php
/* include_once("db/dbUtil.php");
$dbUtil = new DbUtil();*/
// carriage return type (RFC)
$eol = "\r\n";
$name = "";
if (isset($_REQUEST['name'])) {
$name = $_REQUEST['name'];
}
$email = "";
if (isset($_REQUEST['email'])) {
$email = $_REQUEST['email'];
}
$message = '';
if (isset($_REQUEST['question'])) {
$message = $_REQUEST['question'];
}
$fileToUpload = "";
if (isset($_FILES["fileToUpload"]["tmp_name"])) {
$fileToUpload = $_FILES["fileToUpload"]["tmp_name"];
}
// main header
$headers = "From: " . $name . " <" . str_replace(array("\r", "\n"), '', $email) . ">" . $eol;
if ($fileToUpload != "") {
// a random hash will be necessary to send mixed content
$separator = md5(time());
// main header (multipart mandatory)
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"utf-8\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$body .= $message . $eol;
$filename = basename($_FILES["fileToUpload"]["name"]);
$content = file_get_contents($fileToUpload);
$content = chunk_split(base64_encode($content));
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
}
else{
$headers .= "Content-type:text/plain; charset=utf-8" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$body = $message;
}
$mailto = 'john@gmail.com';
$subject = 'NSD';
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
}
?>