我正在使用此代码发送我的邮件:
public function sendMail($receiver, $subject, $message){
$header = "Content-type: text/html";
mail($receiver, $subject, $message, $header);
}
邮件已发送,但稍后打开邮件时内容($message)不完整。它的文字很长,但这可能是原因吗?为什么邮件会被截断?
谢谢!
在标题条目的末尾使用换行符
$header = "From: ...\n";
$header .= "Content-type: text/html\n";
mail($receiver, $subject, $message, $header);
我不知道你是怎么把它填满的,但是缺少一个基本参数,分配标题等。
请参阅此处的功能mail()
说明。对于 HTML 的自动换行,如果您在一行中有 HTML 内容,请使用该功能 - 防止剪切消息。wordwrap()
示例代码:
$from = "one@mail.com";
$mailto = "two@mail.com";
$reply = "three@mail.com";
$subject = "Test";
$message = "<html><body><strong>TEST</strong><p>Bla bla bla</p></body></html>";
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=UTF-8\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";
$headers .= "From: $from\nReply-To: $reply\n";
$check = mail($mailto, $subject, $message, $headers);
if ($check) { echo "OK"; } else { echo "ERROR"; }
示例发送功能:
function sendmail($receiver, $subject, $message) {
$from = "mail@mail.com"; // sender mail
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=UTF-8\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";
$headers .= "From: $from\n";
$check = mail($receiver, $subject, $message, $headers);
if ($check) { return true; } else { return false; }
}
或扩展样本发送功能:
function sendmail($receiver, $subject, $message) {
$from_mail = "mail@mail.com"; // sender mail
$from_name = "Sender Mail"; // mail name
$from = '=?UTF-8?B?'.base64_encode($from_name).'?= <'.$from_mail.'>';
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=UTF-8\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";
$headers .= "From: $from\n";
$check = mail($receiver, '=?UTF-8?B?'.base64_encode($subject).'?=', wordwrap($message,76,"\n"), $headers);
if ($check) { return true; } else { return false; }
}
base64_encode()
预防性转换输入字符,适用于扩展的 ASCII 代码或其他非标准字符(变音符号、语标等)。
wordwrap()
发送前预防性拆分 HTML 内容,仅适用于单行的 HTML 内容。标准是每行 76 个字符(见chunk_split),SMTP 服务器应该支持每行至少 1024 个字符,如果没有,应该自己设置。