【发布时间】:2012-09-21 22:02:00
【问题描述】:
好的,已经尝试了几个小时,查看大量问题和文档,但没有运气。
我可以使用 PEAR 从 gmail 帐户发送纯文本电子邮件。没问题。
但我意识到我需要一个链接,所以我想将其设为 HTML 电子邮件。尝试过的问题和文档here、here、here 等等。
到目前为止,我有:
$em = new EmailObject;
$em->to = 'recipient@someplace.com';
$em->htmlbody = 'Some HTML msg with a <a href="goto.com?link=true">link</a>';
$em->body = 'Backup text';
$em->sendMail();
class EmailObject
{
public $to = '';
public $cc = '';
public $bcc = '';
public $subject = '';
public $body = '';
public $htmlbody = '';
function sendMail()
{
$headers = array('From' => ***withheld*** ,
//'Return-Path' => ***withheld*** ,
'To' => $this->to,
/* 'CC' => $this->cc,
'BCC' => $this->bcc, */
'Subject' => $this->subject);
/*if ($this->cc <> '') { $headers['CC'] = $this->cc; }
if ($this->bcc <> '') { $headers['BCC'] = $this->bcc; } */
$server = array('host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => ***withheld*** ,
'password' => ***withheld*** );
$clrf = "\n";
//Tried all sorts here:
//$mime = new Mail_mime( array('eol' => $clrf, 'text_charset' => 'utf-8', 'html_charset' => 'utf-8') );
$mime = new Mail_mime();
$mime->setTXTBody = $this->body;
$mime->setHTMLBody = $this->htmlbody;
// Tried all sorts here:
// $body = $mime->get();
// $body = $mime->getMessageBody();
// $body = $mime->getMessageBody(array('eol' => $clrf, 'text_charset' => 'utf-8', 'html_charset' => 'utf-8'));
$body = $mime->get(array('eol' => $clrf, 'text_charset' => 'utf-8', 'html_charset' => 'utf-8'));
$headers = $mime->headers($headers);
$smtp =& Mail::factory('smtp', $server);
$mail = $smtp->send($this->to, $headers, $body);
print '<pre>';
var_dump($body);
var_dump($headers);
print '</pre>';
/* Working snippet for plain text:
$smtp = @Mail::factory('smtp', $server);
$mail = @$smtp->send($this->to, $headers, $this->body);*/
return PEAR::isError($mail);
}
}
现在电子邮件确实发送了,但是没有创建正文。它有正确的收件人和抄送,但电子邮件正文中没有任何内容。当我转储$mime->get() 返回的$body 或要使用的任何一个时,它总是返回NULL。到底是怎么回事?我做错了什么?
【问题讨论】:
-
您真的需要使用 pear 来发送基于 html 的电子邮件还是只是希望通过 pear 发送?
-
那里有其他邮件包,或者您可以使用
mail并自己构建标题(我有,这有点棘手但肯定可行)。检查已发送内容的最佳方法是转到 Gmail 并“显示原件”。这将为您提供该邮件客户端 (Gmail) 的实际 given 内容。这就是全部信息。您需要查看此验证所有标题是否正确,mimetypes 或正确,编码没有乱码,并且您的随机生成的部分分隔符出现并且格式正确。使用 Show Original 来执行此操作。 -
我选择了 PEAR,因为我受限于可以安装的内容,而且 PEAR 现在包含在 PHP 中。此外,我确实尝试过其他人,而 PEAR 是唯一一个我可以得到一个“基本”示例来工作的人。
-
@Jared Farrish 我不能使用
mail,因为我需要使用 SSL 和 SMTP 身份验证。以上适用于纯文本,我似乎无法让 any 正文出现在 HTML 电子邮件中。感谢“显示原始”技巧,但正如预测的那样,它没有显示任何身体,因为var_dump()调用表明$mime->get()等返回的$body是NULL。有什么想法吗?
标签: php email pear html-email