我有以下邮件功能:
<?php
{
$to = "alee@####.com";
$subject = "General Contact.";
$headers = "From: ####<noreply@####.com>\r\n";
$headers .= "Reply-To: info@####.com\r\n";
$headers .= "Return-Path: info@####.com\r\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$name = filter_input(INPUT_POST, 'name');
$telephone = filter_input(INPUT_POST, 'phone');
$nature = filter_input(INPUT_POST, 'nature');
$comments = filter_input(INPUT_POST, 'comments');
$message =
"<span style='font-weight: bold;'>Name: </span>"."<br />".$name."<br />"."<br />".
"<span style='font-weight: bold;'>Telephone: </span>"."<br />".$telephone."<br />"."<br />".
"<span style='font-weight: bold;'>Nature of enquiry: </span>"."<br />".$nature."<br />"."<br />".
"<span style='font-weight: bold;'>Comments: </span>"."<br />".$comments."<br />"."<br />";
mail($to, $subject,"We have received a message via the online form at www.####.com<br /> <br />" . $message, $headers);
echo "Your message was successfully sent. Please <a href='contact.php'>click here</a> to return to the site.";
?>
<?php }
?>
我遇到的问题是,当使用表单并且邮件到达我的收件箱时,HTML将显示为纯文本,而不是正确显示。因此,我可以看到所有内联HTML元素,即,将显示为段落的一部分,而不显示新行。任何帮助都会很棒!
修改
Reply-To: info@###.com
Return-Path: info@###.com
MIME-Version: 1.0
Content-type: text/html; charset: utf8
We have received a message via the online form at www.###.com<br /><br /><span style='font-weight: bold;'>Name: </span><br />Aaron Lee<br /><br /><span style='font-weight: bold;'>Telephone: </span><br />#####<br /><br /><span style='font-weight: bold;'>Nature of enquiry: </span><br />###<br /><br /><span style='font-weight: bold;'>Comments: </span><br />Testttttttttttt<br /><br />
答案 0 :(得分:1)
尝试替换这些行:
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
使用:
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset: utf8\r\n";
答案 1 :(得分:0)
看起来它正在发送带有额外换行符的标题。
从您的示例中,您似乎在标题后面有两个换行符。我会尝试删除\r
,因此标题的最后一行只有\n
尝试更改行
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
要
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
我知道这不合适,但我已经看到同样的问题以这种方式解决了。
答案 2 :(得分:0)
您可以尝试更改以下行:
$headers .= 'MIME-Version: 1.0' . "\n";
到
$headers .= 'MIME-Version: 1.0' . "\r\n";
所有行都应以\ r \ n而不是其他任何内容结束。
答案 3 :(得分:0)
我使用此方法发送邮件。也许你可以看到它可以提供的帮助:
public static function mailSend($mail_to,$fname,$mail_sub,$mail_msg,$from){
$To=$mail_to;
$Body =$mail_msg."<BR>";
$Subject=$mail_sub;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: Robot <info@#####.com> \n";
$headers .= "X-Mailer: www.#####.com\n"; //mailer
$headers .= "Return-Path: <info@#####.com>\n";
$headers .= "X-Priority: 3\n"; //1 UrgentMessage, 3 Normal
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->IsHTML(true);
$mail->From = $from;
$mail->FromName = $fname;
$mail->AddAddress($To);
$mail->WordWrap = 100; // set word wrap to 100 characters
$mail->Subject = $Subject;
$mail->Body = $Body;
$mail->AltBody = $Body;
$mail->Send();
}
答案 4 :(得分:0)
消息
您传递给mail()
函数的邮件不是有效的HTML。这可能是您的客户未正确显示的原因。
试试这个:
$message =
"<!DOCTYPE html>\r\n"
. "<html lang="en">\r\n"
. "<body>\r\n"
. "We have received a message via the online form at my.domain.com<br><br>\r\n"
. "<strong>Name:</strong><br>{$name}<br><br>\r\n"
. "<strong>Telephone:</strong><br>{$telephone}<br><br>\r\n"
. "<strong>Nature of enquiry:</strong><br>{$nature}<br><br>\r\n"
. "<strong>Comments:</strong><br>{$comments}<br><br>\r\n"
. "</body>\r\n"
. "</html>\r\n";
还要确保标记中的属性值包含在双引号中,而不是单引号。所以它是<a href="#">link</a>
,而不是 <a href='#'>link</a>
。
标题
关于你正在使用的标题的一些注释(虽然我不认为这会导致你的问题):
\r\n
分隔(\n
不是MIME-Version
)。From
的语法应为From: <email>
或From: "name" <email>
。Reply-To
的语法应为Reply-To: <email>
或Reply-To: "name" <email>
。Return-Path
的语法应为Return-Path: <email>
。Content-Length
标题。试试这个:
$headers = implode(
"\r\n",
array(
'From: "My Name" <noreply@my.domain.com>',
'Reply-To: <info@my.domain.com>',
'Return-Path: <info@my.domain.com>',
'MIME-Version: 1.0',
'Content-type: text/html; charset=iso-8859-1',
'Content-Length: ' . strlen($message)
)
);
请勿使用mail()
PHP mail()
函数存在很多问题。您应该考虑使用第三方库来发送电子邮件。
我建议您查看Swift Mailer,不要使用“邮件传输”(Swift_MailTransport
);)
答案 5 :(得分:0)
我总是使用混合邮件,在发送HTML格式的邮件时,会导致某些人禁用html。 构建邮件,multipart / mixed,并添加一些行来分配部分($ trenner)
我的解决方案与您的解决方案的主要区别在于属性 Content-Transfer-Encoding,文本部分为7位,html为
引用可打印$crlf = "\r\n";
$trenner = '---wbs'.md5(uniqid(time()));
if(!$this->from)throw new exception('wbs_mail:Kein Absender angegeben');
if(!$this->to)throw new exception('wbs_mail:Kein Empfänger angegeben');
if(!$this->subject)throw new exception('wbs_mail:Kein Betreff angegeben');
if(!$this->content['html'])throw new exception('wbs_mail:Kein HTML-Inhalt angegeben');
if(!$this->content['text'])throw new exception('wbs_mail:Kein Text-Inhalt angegeben');
$this->header = "From: ".$this->from.$crlf;
$this->header .= "X-Mailer: wbs_mail php_ver". phpversion().$crlf;
$this->header .= "MIME-Version: 1.0".$crlf;
$this->header .= 'Content-Type: multipart/mixed; boundary="'.$trenner.'"'.$crlf;
// Der Textblock
$content = $trenner.$crlf;
$content .= 'Content-Type: text/plain; charset="iso-8859-1"'.$crlf;
$content .= 'Content-Transfer-Encoding: 7bit'.$crlf;
$content .= $this->getContent('text').$crlf.$crlf;
// Der HTML Block
$content .= $trenner.$crlf.$crlf;
$content_html = 'Content-Type: text/html; charset="iso-8859-1"'.$crlf;
$content_html .= 'Content-Transfer-Encoding: quoted-printable'.$crlf;
$content_html .= $this->getContent('html').$crlf;
$content .= $content_html.$crlf;
$content .= $trenner.$crlf;
答案 6 :(得分:0)
您还可以使用PHP_EOL
(PHP行尾)常量而不是\n
或\r\n
。
PHP_EOL(string)
此平台的正确“行尾”符号。自PHP 4.3.10和PHP 5.0.2起可用
来源:http://www.php.net/manual/en/reserved.constants.php
另请参阅:When do I use the PHP constant "PHP_EOL"?
在实践中,您可以按如下方式使用它:
$br = PHP_EOL;
$headers = 'From: Avatar <avatarparto@gmail.com>.'.$br;
$headers .= 'Reply-To: Avatar <avatarparto@gmail.com>.'.$br;
$headers .= 'Subject: Hello'.$br;
$headers .= 'MIME-Version: 1.0'.$br;
$headers .= 'X-Mailer: PHP'.phpversion();
答案 7 :(得分:0)
尝试使用以下功能,它会使用多部分数据
发送邮件function sendEmail($to,$from,$sub,$msg){
$subject = $sub;
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $msg . "\n\n";
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;
$ok = @mail($to, $subject, $message, $headers, $returnpath);
return $ok;
}
答案 8 :(得分:-1)
尝试更改行
$headers = "From: ####<noreply@####.com>\r\n";
要
$headers = 'From: <urmail@mail.com>' . "\r\n";
我已将双引号更改为单引号。它对我来说很好。试试你的运气。