CakePHP邮件不能在服务器上工作 [英] CakePHP mail not working on server

查看:180
本文介绍了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(); (array('myemailaddress@gmail.com'=>'Me'));
$ Email-> from
$ 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'=>数组('me@gmail.com'=>'我'),
'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); 

我检查了错误日志,它说

 错误:[SocketException]无法发送电子邮件。 


解决方案

我将在黑暗中拍摄



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

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

以上将使用app / View / Emails / html / welcome.ctp作为视图和应用/View/Layouts/Emails/html/fancy.ctp作为布局。



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


I recently uploaded a CakePHP 2.3.8 app to a Ubuntu 12.04 EC2 instance and now I cannot send emails when using $Email->send() but I can do it with the "fast" method of CakeEmail::deliver('to@gmail.com', 'Subject', 'Content');, however I have a layout that I want to use.

When I try to send an email I get an error stating "Could not send email. Error: An Internal Error Has Occurred."

Here is my code for sending the email.

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

In /App/Config/email.php here is what I have for 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',
);

The exact line that the stack trace flags is in CORE/Cake/Network/Email/MailTransport.php

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

I checked the error log and it says

Error: [SocketException] Could not send email.

解决方案

I am going to shoot in the dark here.

It seems to me that you are not setting the layout correctly, according to the documentation you need to tell cake the layout and the view you want to use, for example:

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

The above would use app/View/Emails/html/welcome.ctp for the view, and app/View/Layouts/Emails/html/fancy.ctp for the layout.

Anyways, I recommend taking a look at Cake logs (app/tmp/logs) and see the cause of your error

这篇关于CakePHP邮件不能在服务器上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆