PHP:发送邮件后邮件正文不完整?

时间:2012-06-08 11:43:56

标签: php email

我正在使用此代码发送邮件:

public function sendMail($receiver, $subject, $message){

    $header = "Content-type: text/html";

    mail($receiver, $subject, $message, $header);
}

邮件已发送,但稍后我打开邮件时内容($ message)未完成。它是一个很长的文本,但这可能是原因吗?为什么要剪切邮件?

谢谢!

2 个答案:

答案 0 :(得分:0)

在标题条目末尾使用换行符

$header = "From: ...\n";
$header .= "Content-type: text/html\n";
mail($receiver, $subject, $message, $header);

答案 1 :(得分:0)

我不知道你是如何把它弄满的,但缺少一个基本参数,分配标题等。

请参阅mail()函数here的说明。 对于HTML的自动换行,使用wordwrap()函数,如果您在一行中有HTML内容 - 防止剪切消息。

示例代码:

$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个字符,否则,应由您自己设置。