CakePHP 2.1.0:捕获电子邮件输出

时间:2012-03-15 20:06:03

标签: email cakephp cakephp-2.1

我正在建立一个发送像这样的电子邮件的CakePHP网站:

$email = new CakeEmail('default');
$email->template('test');
$email->emailFormat('html');
$email->to(array('john_doe@example.com' => 'John Doe'));
$email->subject('Test E-mail');
$email->helpers(array('Html', 'Text'));
$email->viewVars(
    array(
        ...
    )
);

if ($email->send()) {
    $this->Session->setFlash('The e-mail was sent!', 'default', array('class' => 'alert alert-success'));
}
else {
    $this->Session->setFlash('An unexpected error occurred while sending the e-mail.', 'default', array('class' => 'alert alert-error'));
}

除了实际发送电子邮件之外,我还希望能够捕获变量中的电子邮件呈现的HTML。这样,我就可以在数据库中记录电子邮件正文的确切内容。这可行吗?

2 个答案:

答案 0 :(得分:4)

line 50 of the MailTransport class,看来实际的send()函数会返回消息和标题。所以而不是:

if($email->send()) {

尝试:

$mySend = $email->send();
if($mySend) {
    //...

然后,$mySend应该是一个数组:

array('headers' => $headers, 'message' => $message);

答案 1 :(得分:0)

这就是我在EmailLib中所做的事情: https://github.com/dereuromark/tools/blob/2.0/Lib/EmailLib.php

它记录电子邮件尝试并将电子邮件输出捕获到email_trace.log中的日志文件(/tmp/logs/)中 - 如果您处于调试模式,它将仅记录(没有发送电子邮件 - 这已被证明相当对当地发展很有用。)

你可以为你的案子写一个类似的包装器。 但是如果你想把它写回DB Dave的方法似乎更合适。