如何使用PEAR邮件发送html邮件

时间:2021-01-24 07:14:01

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邮件系统发送经过身份验证的邮件。我需要发送带有alink的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..

一个标签出现在邮件中。任何想法如何解决这个问题?请帮忙..

3 个解决方案

#1


30  

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 Mime Package和Pear Mail。你可以在这里获得包https://pear.php.net/package/Mail_Mime/download。

#2


13  

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'
);

#3


3  

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 <name@domain.com>> or
From: <"from name" <name@domain.com>> when I tried using quotes.
This caused the smtp connection to quit with an error of invalid address.

具体来说(当我添加调试参数以查看发生的情况时)在名称周围添加了额外的尖括号,因此它尝试将其发送到smtp服务器:From: >或From :当我尝试使用引号时,<“来自名称” >。这导致smtp连接退出并出现无效地址错误。 @domain.com>

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类时,您需要在标题中指定“To:”参数,否则当您收到它时,它似乎会被发送到未公开的地址。因此,使用To param替换Return-Path参数,它将起作用。

#1


30  

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 Mime Package和Pear Mail。你可以在这里获得包https://pear.php.net/package/Mail_Mime/download。

#2


13  

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'
);

#3


3  

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 <name@domain.com>> or
From: <"from name" <name@domain.com>> when I tried using quotes.
This caused the smtp connection to quit with an error of invalid address.

具体来说(当我添加调试参数以查看发生的情况时)在名称周围添加了额外的尖括号,因此它尝试将其发送到smtp服务器:From: >或From :当我尝试使用引号时,<“来自名称” >。这导致smtp连接退出并出现无效地址错误。 @domain.com>

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类时,您需要在标题中指定“To:”参数,否则当您收到它时,它似乎会被发送到未公开的地址。因此,使用To param替换Return-Path参数,它将起作用。