CakePHP邮件在服务器上不起作用


CakePHP mail not working on server

我最近上传了一个CakePHP 2.3.8应用程序到Ubuntu 12.04 EC2实例,现在我在使用$Email->send()时无法发送电子邮件,但我可以使用CakeEmail::deliver('to@gmail.com', 'Subject', 'Content');的"快速"方法来完成,但我有一个想要使用的布局。

当我尝试发送电子邮件时,我收到一个错误,上面写着"无法发送电子邮件"。错误:发生内部错误。"

这是我发送电子邮件的代码。

App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->from(array('myemailaddress@gmail.com' => 'Me'));
$Email->to('person@gmail.com');
$Email->subject('Test Email');
$Email->template('layout_1');
$Email->emailFormat('html');
$testvalues = array('item1' => 'test1', 'item2' => 'test2');            
$Email->viewVars(array('tesvalues'=> $testvalues));
$Email->send();
$this->Session->setFlash('Email has been sent');
$this->redirect($this->referer(), null, true); 

在/App/Config/email.php中,这是我对smtp 的了解

public $smtp = array(
    'transport' => 'Smtp',
    'from' => array('me@gmail.com' => 'Me'),
    'host' => 'ssl://smtp.gmail.com',
    'port' => 465,
    'timeout' => 30,
    'username' => 'me@gmail.com',
    'password' => '****',
    'client' => null,
    'log' => false,
    //'charset' => 'utf-8',
    //'headerCharset' => 'utf-8',
);

堆栈跟踪标志在CORE/Cake/Network/Email/MailTransport.php 中的确切行

$this->_mail($to, $email->subject(), $message, $headers, $params);

我查看了错误日志,上面写着

Error: [SocketException] Could not send email.

我要在黑暗中拍摄。

在我看来,你没有正确设置布局,根据你需要告诉蛋糕布局和你想使用的视图的文档,例如:

$Email = new CakeEmail();
$Email->template('welcome', 'fancy')
    ->emailFormat('html')
    ->to('bob@example.com')
    ->from('app@domain.com')
    ->send();

上面的视图将使用app/View/Emails/html/welcome.ctp,布局将使用app/View/Layouts/Emails/html/fancy.ctp。

无论如何,我建议查看Cake日志(app/tmp/logs),并查看错误的原因