CakePHP邮件无法在服务器上运行

时间:2013-12-02 18:33:48

标签: php email cakephp

我最近将一个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.

1 个答案:

答案 0 :(得分:2)

我将在黑暗中拍摄。

在我看来,你没有正确设置布局,according to the documentation你需要告诉蛋糕布局和你想要使用的视图,例如:

$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)并查看错误原因

相关问题