我无法在PHP电子邮件中附加文件。如果没有附加文件,以下工作正常。
在附加文件时,会抛出错误Error! Please try again later.'
错误日志PHP Warning: mail(): Multiple or malformed newlines found in additional_header in /home/..../mail.php on line 177
if (!isset($_SERVER['X-Requested-With']) && !isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
die();
}
$tmp = date('r');
$message = "<!DOCTYPE html><html><body>";
$message .= "<p><strong></strong>{$val['txtName']}</p>";
$message .= "<p><strong></strong>{$val['txtEmail']}</p>";
$message .= "<p><strong> </strong>{$val['txtSubject']}</p>";
$message .= "<p><strong></strong>{$val['txtPhone']}</p>";
$message .= "<p><strong></strong>{$val['txtText']}</p><br>";
$message .= "</body></html>";
if (isset($_FILES)) {
$filename = $_FILES["txtFile"]["name"];
$uid = md5(uniqid(time()));
$headers = "From: " . $val['txtName'] . " <" . $val['txtEmail'] . ">\r\n";
$headers .= "Reply-To: " . $val['txtName'] . " <" . $val['txtEmail'] . ">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--" . $uid . "\r\n";
$headers .= "Content-type:text/html; charset=utf-8\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
$headers .= $message . "\r\n";
$headers .= "--" . $uid . "\r\n";
$content = chunk_split(base64_encode(file_get_contents($_FILES["txtFile"]["tmp_name"])));
$headers .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n";
$headers .= $content . "\r\n";
$headers .= "--" . $uid . "--";
} else {
$headers = "From: {$val['txtName']} <" . $val['txtEmail'] . ">\r\n";
$headers .= "Reply-To: {$val['txtName']} <" . $val['txtEmail'] . ">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
}
if (mail('info@mail.com', $val['txtSubject'], $message, $headers)) {
die(json_encode(array(
'error' => '0',
'title' => 'Success! Your message has been sent.'
)));
} else {
die(json_encode(array(
'error' => '1',
'title' => 'Error! Please try again later.'
)));
}
尝试了另一个标头集(但同样的问题):
$file = fopen($_FILES["txtFile"]["tmp_name"], "r");
if ($file == false) {
die(json_encode(array(
'code' => 'error',
'title' => 'Error! Could not read the file.'
)));
}
$size = $_FILES["txtFile"]["size"];
$content = fread($file, $size);
$encoded_content = chunk_split(base64_encode($content));
$num = md5(time());
$header = "From: " . $val['txtName'] . " <" . $val['txtEmail'] . ">\r\n";
$header .= "Reply-To: " . $val['txtName'] . " <" . $val['txtEmail'] . ">\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$num\r\n";
$header .= "--$num\r\n";
$header .= "Content-Type: text/plain\r\n";
$header .= "Content-Transfer-Encoding:8bit\r\n\n";
$header .= "$message\r\n";
$header .= "--$num\r\n";
$header .= "Content-Type: multipart/mixed; ";
$header .= "name=\"$filename\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; ";
$header .= "filename=\"$filename\"\r\n\n";
$header .= "$encoded_content\r\n";
$header .= "--$num--";
请告诉我错误的地方。
答案 0 :(得分:0)
最好的方法是不要自己创建邮件标题。在这样做时,所有规则都是一致的并不容易。
我建议使用任何库(例如:phpMailer),它会对你有所帮助,你的代码会变得更干净,更易于维护。