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


Trying to send / sending html emails with sendmail but shows sourcecode of 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"(或die("无法打开Sendmail"(;fputs($fd,"收件人:".$to1."''n"(;fputs($fd,"发件人:''"测试''"''n"(;fputs($fd,"主题:".$Subject."''n"(;fputs($fd,"X-Mailer:PHP5''n"(;fputs($fd,"Mime版本:1.0''n"(;fputs($fd,"内容类型:多部分/可选;边界=''".$mime_boundary."''"''n"(;fputs($fd,"''n"(;fputs($fd,"--".$mime_boundary."''n"(;fputs($fd,"内容类型:text/html;charset=''"utf-8''"''n"(;fputs($fd,"内容传输编码:带引号可打印''n"(;fputs($fd,"''n"(;fputs($fd,$sendmail_body."''n"(;fputs($fd,"——"$mime_boundary。"--''n"(;pclose($fd(;

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

我认为你应该考虑发送多部分,因为有些客户端不支持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路径和params,它就会以多部分/替代类型通过sendmail发送邮件。

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

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

这对我有效:

<?php$message=<lt<EOL--边界内容类型:文本/纯文本;charset=UTF-8内容传输编码:7位<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="de"lang="de"><头部><meta http equiv="Content Type"Content="text.html;charset=utf-8"/><标题>测试<标题><style="text/css">body{font:normal 12px宋体,Arial,Helvetica,sans-serif;}<样式><头部><身体><身体><html>--边界--EOL;$fd=popen("/var/qmail/bin/sendmail-t","w"(或die("无法打开sendmail"(;fputs($fd,"To:".$To."''n"(;fputs($fd,"From:''"Example''"<example@example.com>''n"(;fputs($fd,"主题:".$Subject."''n"(;fputs($fd,"MIME版本:1.0''n"(;fputs($fd,"内容类型:多部分/可选;boundary=''"border''"''n''n"(;fputs($fd,"这是一个MIME格式的包含多个部分的消息。''n"(;fputs($fd,$message(;pclose($fd(;>

我希望这将对有帮助