尝试使用sendmail发送/发送html电子邮件,但显示电子邮件的源代码

问题描述:

我试图在PHP中发送HTML电子邮件,但它始终显示电子邮件程序中电子邮件的源代码。但它应该将html电子邮件呈现为html,而不是将源代码显示为电子邮件内容。尝试使用sendmail发送/发送html电子邮件,但显示电子邮件的源代码

我把我的邮件是这样的:

$fd = popen("/var/qmail/bin/sendmail -t","w") or die("Couldn't Open Sendmail"); 
    fputs($fd, "To: ".$to2." \n"); 
    fputs($fd, "From: \"Test <[email protected]>\" \n"); 
    fputs($fd, "Subject: ".$subject." \n"); 
    fputs($fd, "X-Mailer: PHP5 \n"); 
    fputs($fd, "Mime-Version: 1.0 \n"); 
    fputs($fd, " \n"); 
    fputs($fd, "--".$mime_boundary.""); 
    fputs($fd, "Content-Type: text/html; charset=\"utf-8\"; boundary=\"".$mime_boundary."\" \n"); 
    fputs($fd, "Content-Transfer-Encoding: quoted-printable \n"); 
    fputs($fd, " \n"); 
    fputs($fd, $sendmail_body." \n"); 
    fputs($fd, "".$mime_boundary."--"); 
    pclose($fd); 

的HTML文件的内容是这样的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Test</title> 
<style type="text/css"> 
body { font: normal 12px Verdana, Arial, Helvetica, sans-serif; } 
</style> 
</head> 
<body> 
</body> 
</html> 

现在的工作:

$ FD = POPEN( “/ var/qmail/bin/sendmail -t”,“w”)或死(“无法打开 Sendmail”); fputs($ fd,“To:”。$ to1。“\ n”); fputs($ fd,“From: \”Test \“\ n”); fputs($ fd,“Subject:”。$ subject。“\ n”); fputs($ fd,“X-Mailer: PHP5 \ n”); fputs($ fd,“Mime-Version:1.0 \ n”); fputs($ fd,“Content-Type:multipart/alternative; boundary = \”“。$ mime_boundary。”\“\ n”); fputs($ fd,“\ n”); fds($ fd,“ - ”。$ mime_boundary。“\ n”); fputs($ fd,“Content-Type: text/html; charset = \”utf-8 \“\ n”); fputs($ fd, “Content-Transfer-Encoding:quoted-printable \ n”); fputs($ fd,“ \ n”); fputs($ fd,$ sendmail_body。“\ n”); fputs($ fd, “ - ”。$ mime_boundary。“ - \ n”); pclose函数($ FD);

而我的html文件的第一行是空的,或者我在html内容前添加一个\ n。

+0

不得不编辑我的问题,因为我忘了一些行 –

我想你应该考虑派遣多,因为一些客户端不支持HTML邮件或只是喜欢纯文本:

$headers = "From: Example <[email protected]>\r\n 
    MIME-Version: 1.0\r\n 
    Content-Type: multipart/alternative; boundary={$mime_boundary}\r\n 
    X-Mailer: PHP5"; 

$message = "This is a MIME-Message. If you can read this your client does not support the MIME format.\r\n 
\r\n 
{$mime_boundary}\r\n 
Content-Transfer-Encoding: quoted-printable\r\n 
Content-Type: text/plain; charset=utf8;\r\n 
\r\n 
Text Content encoded in quoted printable 
\r\n 
\r\n 
{$mime_boundary}\r\n 
Content-Transfer-Encoding: quoted-printable\r\n 
Content-Type: text/html;charset=utf8;\r\n 
\r\n 
HTML Content encoded in quoted printable 
\r\n 
--{$mime_boundary}"; 

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

只要在php.ini中正确配置sendmail路径和参数,这将通过sendmail以multipart/alternative类型发送邮件。

+0

\ n或\ r \ n?你使我感到困惑 –

+0

\ n是类似unix的方式,windows使用\ r \ n和Mac OS只是\ r - 所以\ r \ n会在每个系统上产生换行符... \ r \ n或\ n不应该在邮件上下文有所作为,但有时他们这样做... –

+0

其在Linux上运行的UNIX机器/网络服务器,所以我不neet \ r?有时?所以我应该使用\ r \ n? –

fputs($fd, "X-Mailer: PHP5 \n\n"); 

尝试删除第二个\ n,因为它是标头终止的标志。

+0

然后它的工作? –

这为我工作:

 
<?php 
$message=<<<EOL 
--frontier 
Content-type: text/plain; charset=UTF-8 
Content-Transfer-Encoding: 7bit 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Test</title> 
<style type="text/css"> 
body { font: normal 12px Verdana, Arial, Helvetica, sans-serif; } 
</style> 
</head> 
<body> 
</body> 
</html> 
--frontier-- 
EOL; 

$fd = popen("/var/qmail/bin/sendmail -t","w") or die("Couldn't Open Sendmail"); 

fputs($fd, "To: ".$to." \n"); 
fputs($fd, "From: \"Example\" <[email protected]> \n"); 
fputs($fd, "Subject: ".$subject." \n"); 

fputs($fd,"MIME-Version: 1.0\n"); 
fputs($fd,"Content-type: multipart/alternative; boundary=\"frontier\"\n\n"); 
fputs($fd,"This is a message with multiple parts in MIME format.\n"); 

fputs($fd, $message); 
pclose($fd); 
?> 

我希望这将是有益的

+0

和我的doctype的HTML文件?我应该改变所有这些东西,如Transfer-Encoding,Content-Type,... –

+0

尝试过它(我在html文件中使用doctype),但收到的邮件是空的(outlook,甚至没有sourcecode存在于电子邮件中平原空),并在iPhone上显示源代码 –