【问题标题】:CakePHP 3 - Emails not sending in productionCakePHP 3 - 电子邮件不在生产中发送
【发布时间】:2016-09-02 17:55:53
【问题描述】:

使用下面的代码,我的电子邮件可以在开发模式下正常发送。现在,当我尝试触发其中一个操作以发送电子邮件时,我已经将我的应用程序置于生产模式,页面只是加载和加载,直到我收到 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

【问题讨论】:

  • 加载,直到我收到 CakePHP 错误。 它说什么?错误日志说明了什么?
  • 发生内部错误 错误:发生内部错误。
  • 连接超时 Cake\Network\Exception\SocketException
  • 那么您需要验证您是否可以从您的生产服务器连接到 SMTP 服务器。这可能会帮助您进一步调试stackoverflow.com/a/11988455/1604068
  • StackOverflow 不是调试服务,所以是关于特定的编程相关问题,你必须自己检查你的服务器环境发生了什么。鉴于您将提供更多上下文信息,您的问题可能适合服务器故障。

标签: php email cakephp cakephp-3.0


【解决方案1】:

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

将 smtp 地址列入白名单后一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    相关资源
    最近更新 更多