Laravel:Swift_TransportException无法使用host smtp.gmail.com建立连接

时间:2017-06-19 13:19:46

标签: php laravel email swiftmailer

我正在尝试使用LaravelSwift Mailer发送电子邮件,我过去3天一直在尝试No Luck

我有Godaddy共享主机,我已经与Customer Support讨论了,他们说从那边没有问题

我无法在此代码中找到错误。

已经提到这篇文章,但没有得到任何解决方案。

  1. Laravel - Connection could not be established with host smtp.gmail.com [ #0]
  2. Swift_TransportException Connection could not be established with host smtp.gmail.com
  3. Connection could not be established with host smtp.gmail.com [Connection timed out #110] in laravel 5.2
  4. How to resolve the error: Message: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Connection refused)
  5.   

    代码

       $transport = \Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
          ->setUsername('my@gmail.com')
          ->setPassword('*****');
    
          $nam = 'abc';
          $from = 'my@gmail.com';
    
          $message = \Swift_Message::newInstance($subject)
    
          ->setFrom(array($from => $name))
          ->setTo(array($to))
          ->setBody($body);
          $message->setContentType("text/html");
          $mailer = \Swift_Mailer::newInstance($transport);
          $result = $mailer->send($message);
            return $result;
    
    
    
    Please ignore Variable declaration, like $subject and ect....
    

    Port Tried。

    1. 465
    2. 587
    3. 80
    4. 25
    5. 110 ( Gmail Commercial Email )
    

    显示错误

      

    消息:fsockopen():无法连接到ssl://smtp.gmail.com:465   (连接被拒绝)

    enter image description here

4 个答案:

答案 0 :(得分:1)

我使用了godaddy的旧服务器,我可以发送电子邮件。我不得不改变sendmail的路径。您可以在info.php - >

上查看
// Show all information, defaults to INFO_ALL
phpinfo();

并搜索:" sendmail_path" ...

在我的情况下 sendmail_path 是" / usr / sbin / sendmail -t -i"

你应该把它放在config / mail.php上:

   /*
    |-----------------------------------------------------------------
    | Sendmail System Path
    |-----------------------------------------------------------------
    |
    | When using the "sendmail" driver to send e-mails, we will need to know
    | the path to where Sendmail lives on this server. A default path has
    | been provided here, which will work well on most of your systems.
    |
    */

    'sendmail' => '/usr/sbin/sendmail -t -i',

答案 1 :(得分:1)

我有一个解决方案。使用以下配置:

        'mail' => [
        'class' => 'yii\swiftmailer\Mailer',
        'htmlLayout' => '@frontend/views/user/mail/layouts/html',
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp-relay.gmail.com',  // e.g. smtp.mandrillapp.com or smtp.gmail.com
            'username' => '',
            'password' => '',
            'port' => '465',
            'encryption' => 'ssl',
            'streamOptions' => [
                'ssl' => [
                    'allow_self_signed' => true,
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                ],
            ]
        ],
    ],

答案 2 :(得分:0)

检查您的smtp连接电子邮件,尝试使用Google电子邮件进行测试

smtp.gmail.com 港口:587 secureconnection:true

在google email secure connectino中禁用

答案 3 :(得分:0)

此配置对我有用!

MAIL_DRIVER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=xxxxxxx@gmail.com
MAIL_PASSWORD=******
MAIL_ENCRYPTION=tls

确保在更改配置后清除缓存。

php artisan cache:clear