【问题标题】:Trying to send / sending html emails with sendmail but shows sourcecode of email尝试使用发送邮件发送/发送 html 电子邮件,但显示电子邮件的源代码
【发布时间】:2012-01-06 06:27:39
【问题描述】:

我正在尝试用 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") 或 die("无法打开 发送邮件"); fputs($fd, "收件人: ".$to1." \n"); fputs($fd, "发件人: \"测试\" \n"); fputs($fd, "主题: ".$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, "内容类型: 文本/html;字符集=\"utf-8\"\n"); fputs($fd, “内容传输编码:引用打印\n”); fputs($fd, " \n"); fputs($fd, $sendmail_body." \n"); fputs($fd, "--".$mime_boundary."--\n"); pclose($fd);

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

【问题讨论】:

  • 因为我忘记了一些行,所以不得不编辑我的问题

标签: php sendmail html-email


【解决方案1】:

我认为你应该考虑发送多部分,因为有些客户端不支持 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 类型发送邮件。

【讨论】:

  • \n 或 \r\n ?你让我很困惑
  • \n 是类 unix 的方式,windows 使用 \r\n 而 Mac OS 只是 \r - 所以 \r\n 会在每个系统上产生换行符... \r\n或 \n 不应该对邮件上下文产生影响,但有时他们会这样做......
  • 它是在 linux 上运行的 unix 机器/网络服务器,所以我不需要 \r?有时?所以我一定要用\r\n吗?
  • 这取决于收件人使用的邮件服务器软件。但是大多数守护进程只要使用 \n 就可以了。
【解决方案2】:
【解决方案3】:
fputs($fd, "X-Mailer: PHP5 \n\n"); 

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

【讨论】:

    猜你喜欢
    • 2011-09-10
    • 1970-01-01
    • 2016-11-23
    • 2012-12-29
    • 2011-03-30
    • 2018-12-29
    • 2020-05-03
    • 1970-01-01
    • 2011-09-16
    相关资源
    最近更新 更多