使用emailcomponent从cakephp中的localhost发送电子邮件

时间:2011-12-03 14:22:02

标签: email cakephp

<?php 

    class EmailsController extends AppController
    {
        var $uses=null;
        var $components=array(
                'Email'=>array(
                    'delivery'=>'smtp',
                    'smtpOptions'=>array(
                      'host'=>'ssl://smtp.google.com',
                      'username'=>'username@gmail.com',
                      'password'=>'password',
                      'port'=>465
                      )


        ));

         function sendEmail() {
            $this->Email->to = 'Neil <neil6502@gmail.com>';
            $this->Email->subject = 'Cake test simple email';
            $this->Email->replyTo = 'neil6502@gmail.com';
            $this->Email->from = 'Cake Test Account <neil6502@gmail.com>';
            //Set the body of the mail as we send it.
            //Note: the text can be an array, each element will appear as a
            //seperate line in the message body.
            if ( $this->Email->send('Here is the body of the email') ) {
                $this->Session->setFlash('Simple email sent');
            } else {
                $this->Session->setFlash('Simple email not sent');
            }
            $this->redirect('/');
            } 





    }


?>

上面的代码是我的控制器,负责发送电子邮件......

但是当我使用网址sendEmail()运行此功能http://localhost/authentication/emails/sendemail时,它甚至没有显示任何错误或任何响应...完整的空白页。我不知道原因。

4 个答案:

答案 0 :(得分:0)

我想我刚才有同样的问题。您可能需要将to地址更改为包含 地址的值,因此您应使用Name <email@example.com>代替email@example.com

您可以通过使用以下命令记录(或调试)smtp错误来检查错误:

$this->log($this->Email->smtpError, 'debug');

debug($this->Email->smtpError);
祝你好运。希望这会有所帮助。

答案 1 :(得分:0)

/* Auf SMTP-Fehler prüfen */
 $this->set('smtp_errors', $this->Email->smtpError);

答案 2 :(得分:0)

我会将电子邮件配置添加到位于 /app/Config/email.php 中的email.php文件中,如果它不存在将email.php.default复制到电子邮件中.php,更改那里的smtp设置

public $smtp = array(
    'host' => 'ssl://smtp.gmail.com',
    'port' => 465,
    'username' => 'my@gmail.com',
    'password' => 'secret'
);

在控制台顶部,上面的类EmailsController扩展了AppController add,

App::uses('CakeEmail', 'Network/Email');

然后在你的函数sendEmail()中尝试

$Email = new CakeEmail();
$Email->from(array('me@example.com' => 'My Site'))
    ->to('you@example.com')
    ->subject('About')
    ->send('My message');

测试电子邮件我通常会将它们发送到Cake Logs,

**在/app/Config/email.php中,包含:(日志输出应为/app/tmp/logs/debug.log)

public $test = array(
  'log' => true
);

此外将“测试”添加到您的$ Email变量,例如**

$Email = new CakeEmail('test');

答案 3 :(得分:0)

实际上在我的情况下:我收到一条错误消息“SMTP服务器不接受密码。”

之后我按照以下链接解决问题: 第1步:https://blog.shaharia.com/send-email-from-localhost-in-cakephp-using-cakeemail/ 第2步:Sending Activation Email , SMTP server did not accept the password

相关问题