Search code examples
phpemailyii2smtpswiftmailer

Yii2 : Connection could not be established with host smtp.gmail.com [Connection refused #111] in a godaddy hosting


I'm trying to use Swift Mailer in Yii2.

My host is Godaddy and I'm trying to use a gmail account to send emails.

The code seems to be working fine, but when I try to use it I receive the error: Connection could not be established with host smtp.gmail.com [Connection refused #111]

I double-checked that I'm using the correct credentials for gmail. Could it be an issue with my host and if so, is there a way to fix it? This is the code

'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'transport' => [
                    'class' => 'Swift_SmtpTransport',
                    'host'=>'smtp.gmail.com',
                    'username'=>'[email protected]',
                    'password'=>'******',
                    'port'=>'587',
                    'encryption'=>'tls',
                ],
            'useFileTransport' => false, //for the testing purpose, you need to enable this


        ],
I also tried with **`'class' => 'Swift_MailTransport',`**
but the error was not resolved.

The error is 

Connection could not be established with host smtp.gmail.com [Connection refused #111]

Please help me in this regard. Thanks,


Solution

  • It seems that your configuration settings for Swift Mailer are unable to connect host Gmail, try below changes, most probably it will work:

    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'useFileTransport' => false,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host'=>'smtp.gmail.com',
            'username'=>'[email protected]',
            'password'=>'******',
            'port'=>'465',
            'encryption'=>'ssl',
        ],
    ],
    

    Changes made:

    'port'=>'465',
    'encryption'=>'ssl',