PHP HTML-Email结果中的感叹号随机出现

时间:2013-08-23 17:48:09

标签: html email php

我在PHP电子邮件功能的结果中随机出现惊叹号。我读到这是因为我的线太长或者我不得不在Base64中对电子邮件进行编码,但我不知道该怎么做。

这就是我所拥有的:

$to = "you@you.you";
$subject = "Pulling Hair Out";
$from = "me@me.me";
$headers = "From:" . $from;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 64bit\r\n";

mail($to,$subject,$message,$headers); 

我如何解决这个问题,因此没有随意的!在结果?谢谢!

4 个答案:

答案 0 :(得分:14)

如上所述:Exclamation Point in HTML Email

问题是你的字符串太长了。将超过78个字符的HTML字符串提供给邮件功能,最终会得到一个! (bang)在你的字符串中。

这是由RFC2822 http://tools.ietf.org/html/rfc2822#section-2.1.1

中的行长限制造成的

答案 1 :(得分:6)

尝试使用这段代码:

$to = "you@you.you";
$subject = "Pulling Hair Out";
$from = "me@me.me";
$headers = "From:" . $from;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 64bit\r\n";

$finalMessage = wordwrap( $message, 75, "\n" );

mail($to,$subject,$finalMessage,$headers);

问题是一行不应超过998个字符。 (另见https://stackoverflow.com/a/12840338/2136148

答案 2 :(得分:3)

这里的答案有关于行长度的正确信息,但是没有一个为我提供足够的代码片段来解决问题。我环顾四周,找到了最好的方法来做到这一点,就在这里;

<?php
// send base64 encoded email to allow large strings that will not get broken up
// ==============================================
$eol = "\r\n";
// a random hash will be necessary to send mixed content
$separator = md5(time());

$headers  = "MIME-Version: 1.0".$eol;
$headers .= "From: Me <info@example.com>".$eol;
$headers .= "Content-Type: multipart/alternative; boundary=\"$separator\"".$eol;
$headers .= "--$separator".$eol;
$headers .= "Content-Type: text/html; charset=utf-8".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol.$eol;

// message body
$body = rtrim(chunk_split(base64_encode($html)));

mail($email, $subject, $body, $headers);
// ==============================================

答案 3 :(得分:2)

你是对的,那是因为你的电子邮件太长了。尝试在邮件标题中替换此行。

Content-Transfer-Encoding: quoted-printable