在cakephp中执行登录时如何向用户发送电子邮件

时间:2019-01-18 11:10:16

标签: email cakephp login

我对cakephp邮件一无所知,因此简要解释一下解决方案,我的意思是从一开始就应该做什么和怎么做。

从cakephp官方的角度来看,我只是使用了“ use Cake \ Mailer \ Email;”。然后显示邮件功能,但显示错误消息,如下所示

  

无法发送电子邮件:mail():在以下位置无法连接到邮件服务器   “ server.com”端口25,验证您的“ SMTP”并   php.ini中的“ smtp_port”设置或使用ini_set()

我的用户控制器登录功能

public function login() {
        $this->viewBuilder()->setLayout('');

        if ($this->request->is('post')) {
            $data = $this->request->getData();

            $query = $this->Users->find()->where(['email' => $data['email'], 'password' => md5($data['password'])]);
            if ($query->count()) {
                $user = $query->first()->toArray();
                $this->Auth->setUser($user);  
//FOR MAIL START                  
                ini_set('SMTP', "server.com");
                ini_set('smtp_port', "25");
                ini_set('sendmail_from', "restrange5@gmail.com");

                $email = new Email('default');
                $email->setFrom(['restrange5@gmail.com' => 'My Site'])
                        ->setTo('ramakantasahoo835@gmail.com')
                        ->setSubject('About')
                        ->send('My message');
        //FOR MAIL END

                $this->Flash->success(_('Login Successfull'));
                $this->redirect(['action' => 'dashboard']);
            } else {
                $this->Flash->error(__('Username/Password not found!!'));
                return $this->redirect($this->referer());
            }
        }
    }

仅在用户控制器中更改了多少,我知道多少。我还有什么建议呢?

1 个答案:

答案 0 :(得分:0)

**Your cofiguration in app.php file is something like this **'
EmailTransport' => [
    'default' => [
        'className' => 'Smtp',
        // The following keys are used in SMTP transports
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'timeout' => 30,
        'username' => 'email here',
        'password' => 'password here',
        'client' => null,
        'tls' => null,
        'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
    ],
]