php 如何使用 PEAR 邮件发送 html 邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1361762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to send html mails using PEAR mail
提问by Sakura
I am using PEAR mail system to send authenticated mails.I need to send HTML mails that has alinks.It was working fine before i started using PEAR mail.Now i am not able to send HTML mails.
我正在使用 PEAR 邮件系统发送经过身份验证的邮件。我需要发送带有链接的 HTML 邮件。在我开始使用 PEAR 邮件之前它工作正常。现在我无法发送 HTML 邮件。
mail body looks like this:
邮件正文如下所示:
$body = <<<EOD
Hiya $username
You might be interested in the current 'haves' and 'wants' on example.com
Latest Haves
<a href="http://www.exmaple.com/product/have/64/Titan+Fast+Track+SunGlass">Titan Fast Track SunGlass</a>
EOD;
a tag appears as it is in the mail.Any idea how to solve this??Pls help..
一个标签出现在邮件中。知道如何解决这个问题吗??请帮忙..
回答by karim79
If you follow this example there's no reason it shouldn't work:
如果您遵循此示例,则没有理由它不起作用:
<?php
include('Mail.php');
include('Mail/mime.php');
// Constructing the email
$sender = "Leigh <leigh@no_spam.net>";// Your name and email address
$recipient = "Leigh <leigh@no_spam.net>"; // The Recipients name and email address
$subject = "Test Email";// Subject for the email
$text = 'This is a text message.';// Text version of the email
$html = '<html><body><p>HTML message</p></body></html>';// HTML version of the email
$crlf = "\r\n";
$headers = array('From' => $sender, 'Return-Path' => $sender, 'Subject' => $subject);
// Creating the Mime message
$mime = new Mail_mime($crlf);
// Setting the body of the email
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers);
// Sending the email
$mail =& Mail::factory('mail');
$mail->send($recipient, $headers, $body);
?>
NOTE:in order for the above example to work one needs the Pear Mail Mime Package in addition the Pear Mail one. You can get the package here https://pear.php.net/package/Mail_Mime/download.
注意:为了使上面的示例工作,除了 Pear Mail 之外,还需要 Pear Mail Mime Package。你可以在这里https://pear.php.net/package/Mail_Mime/download获取包。
回答by story
What do your headers look like? Here are mine:
你的标题是什么样的?这是我的:
$headers = array(
'To' => $recipients,
'From' => $adminEmail,
'Subject' => $subject,
'MIME-Version' => 1,
'Content-type' => 'text/html;charset=iso-8859-1'
);
回答by Robert
Please note that the example posted by karim79 has a header parameter that may cause you much grief: "Return-Path" - when I included this parameter like the example it prevented me from adding a from name, only a sender email address worked.
请注意,由 karim79 发布的示例有一个标题参数可能会让您感到非常难过:“返回路径”-当我像示例一样包含此参数时,它阻止我添加发件人姓名,只有发件人电子邮件地址有效。
Specifically (when I added a debug param to see what was happening) there were extra angle brackets added around the from name so it tried to send this to the smtp server:
From: <from name <[email protected]>> or
From: <"from name" <[email protected]>> when I tried using quotes.
This caused the smtp connection to quit with an error of invalid address.
具体来说(当我添加调试参数以查看发生了什么时)在 from 名称周围添加了额外的尖括号,因此它尝试将其发送到 smtp 服务器:
From: <from name <[email protected]>> 或
From : <"from name" <[email protected]>> 当我尝试使用引号时。
这导致 smtp 连接以无效地址错误退出。
Also when using the mime_mail class you need to specify the "To:" parameter in the headers or it will appear to be sent to undisclosed addresses when you receive it. So replace the Return-Path param with a To param and it will work.
此外,在使用 mime_mail 类时,您需要在标题中指定“收件人:”参数,否则在您收到它时它似乎会发送到未公开的地址。因此,将 Return-Path 参数替换为 To 参数,它将起作用。

