CakePHP 3 - 电子邮件未发送到生产中

时间:2016-05-07 19:53:46

标签: php email cakephp cakephp-3.0

使用下面的代码,我的电子邮件将以开发模式发送。现在我已经将我的应用程序置于生产模式,当我尝试触发其中一个动作发送电子邮件时,页面只是加载并加载,直到我收到CakePHP错误。但数据仍会进入数据库。

'EmailTransport' => [
    'default' => [
        'className' => 'Mail',
        // The following keys are used in SMTP transports
        'host' => 'localhost',
        'port' => 25,
        'timeout' => 30,
        'username' => 'user',
        'password' => 'secret',
        'client' => null,
        'tls' => null,
        'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
    ],
    'mailjet' => [
        'host' => 'in-v3.mailjet.com',
        'port' => 587,
        'timeout' => 60,
        'username' => 'removed',
        'password' => 'removed',
        'className' => 'Smtp'
    ],
],



public function register()
{
    $user = $this->Users->newEntity();

    if ($this->request->is('post')) {
        $user = $this->Users->patchEntity($user, $this->request->data);
        $user->role = 'user';
        $user->email_verified = '0';
        $user->email_token = Security::hash($user->username + microtime(), 'sha1', true);
        $user->email_token_expires = strtotime('now');
        $user->email_token_expires = strtotime('+1 hour');

        if ($result = $this->Users->save($user)) {
            $this->Users->lastLogin($user['id']);
            $authUser = $this->Users->get($result->id)->toArray();
            $this->Auth->setUser($authUser);

            $email = new Email();
            $email->template('confirm')
                    ->transport('mailjet')
                    ->emailFormat('both')
                    ->viewVars(['token' => $user->email_token])
                    ->to($this->Auth->user('email'))
                    ->from(['removed' => 'Welcome, ' . $user->username . '!'])
                    ->subject('Please confirm your email!')
                    ->send();

            $this->Flash->success('You have successfully registered. Check your email to confirm email.');
            return $this->redirect(['action' => 'profile']);
        } else {
            $this->Flash->error('The user could not be saved. Please, try again.');
        }
    }
    $this->set(compact('user', 'userEmail', 'token'));
    $this->set('_serialize', ['user']);
}

它可能是什么?

错误:

Connection timed out
Cake\Network\Exception\SocketException

1 个答案:

答案 0 :(得分:0)

上次出现此错误时,smtp地址在我的生产服务器中被阻止。

将smtp地址列入白名单后,一切都很好。