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

时间:2011-11-25 10:52:07

标签: php sendmail html-email

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

我发送这样的电子邮件:

$fd = popen("/var/qmail/bin/sendmail -t","w") or die("Couldn't Open Sendmail"); 
    fputs($fd, "To: ".$to2." \n"); 
    fputs($fd, "From: \"Test <test@test.com>\" \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,”来自:   \“测试\”\ 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”);         fputs($ fd,“ - ”。$ mime_boundary。“\ n”); fputs($ fd,“内容类型:   为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。

3 个答案:

答案 0 :(得分:2)

我认为你应该考虑发送multipart,因为有些客户不支持HTML邮件或只是更喜欢纯文本:

$headers = "From: Example <example@example.com>\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类型发送邮件。

答案 1 :(得分:1)

这对我有用:

<?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\" <example@example.com> \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);
?>

我希望它会有所帮助

答案 2 :(得分:0)

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

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