从localhost发送的电子邮件,但在托管后未发送


Email sent from localhost but not sent after hosting?

我使用的是yii2MVC框架。我已经创建了一个电子邮件应用程序,它在localhost中运行良好,但在代码联机后会出现连接超时异常。我使用ssl和tls进行加密,但找不到任何真正的解决方案。请帮助

我的配置文件代码是:

'mailer' => [
  'class'            => 'yii'swiftmailer'Mailer',                   
  'useFileTransport' => false,
  'transport'        => [
    'class'      => 'Swift_SmtpTransport',
    'host'       => 'smtp.gmail.com', 
    'username'   => 'email@gmail.com',
    'password'   => 'password',
    'port'       => '587',
    'encryption' => 'tls',
  ],
]

消息发送代码:

Yii::$app->mailer->compose()
                ->setFrom($from)
                ->setTo($to)
                ->setSubject('Hello')
                ->setTextBody('Hello')
                ->send();

错误:

Swift_TransportException
Connection could not be established with host smtp.gmail.com [Connection timed out #110]

您应该尝试更换主机:

smtp.gmail.com

带有

173.194.65.108gmail-smtp-msa.l.google.com

如果仍然不起作用,请尝试将端口更改为25465

在您的配置文件中:

  • port更改为465,将encryption更改为ssl
  • 在您的gmail帐户设置中,检查您是否启用了IMAP

关于gmail设置的信息:

[1]https://support.google.com/a/answer/176600?hl=en&ref_topic=1357753

[2]https://support.google.com/accounts/answer/6010255?hl=en

从config.yml spool: { type: memory } 中删除

然后你将添加像这样的尝试捕获

try{
        $mailer = $this->getMailer();
        $response = $this->getMailer()->send($message);
    }catch('Swift_TransportException $e){
        $response = $e->getMessage() ;
    }

或其他

尝试以下端口号

'port' => 26 

'port' => 465.