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

2022-08-30 17:02:23

我正在使用PEAR邮件系统发送经过身份验证的邮件。我需要发送包含链接的 HTML 邮件。在我开始使用PEAR邮件之前,它工作正常。现在我无法发送HTML邮件。

邮件正文如下所示:

$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;

标记将按其在邮件中的样子显示。任何想法如何解决这个问题??请帮忙..


答案 1

如果你遵循这个例子,没有理由它不应该工作:

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

注意:为了使上述示例正常工作,除了Pear Mail之外,还需要Pear Mail Mime包。您可以在此处 https://pear.php.net/package/Mail_Mime/download 获得套餐。


答案 2

您的标题是什么样的?这是我的:

$headers = array(
    'To' => $recipients,
    'From' => $adminEmail,
    'Subject' => $subject,
    'MIME-Version' => 1,
    'Content-type' => 'text/html;charset=iso-8859-1'
);

推荐