6

I'm trying to configure swiftmailer in the advanced yii 2.0 template. I've gone through many posts and I understand that there are some issues with gmail. My configuration environment in development is the following:

'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        'useFileTransport' => false,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => gethostbyname('smtp.gmail.com'),
            'username' => 'xxx@gmail.com',
            'password' => 'xxssxxxx',
            'port' => '465',
            'encryption' => 'ssl'
        ]

I've also set the support mail that's used in the controller to the same gmail address in the main-local config shown above. I've tried switching to a less secure configuration for apps in the gmail account and it did not work and I'm not particularly fond on changing this. I get the following error when I use ssl encryption

connection could not be established with host ... [ #0]

If I don't specify the encryption I get a time out error.

I have OpenSSL support enabled according to my phpinfo file but I cannot make it work. TLS does not work either because if I change the config (port:'587' & encryption='tls') I get the following error

stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

Any ideas on how can I fix this? Is this an unfixed issue? Should I use another mailing extension?

Betty Sanchez
  • 219
  • 3
  • 11

6 Answers6

18

The options can be set like this:

'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    'viewPath' => '@common/mail',
    'useFileTransport' => false,
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => gethostbyname('smtp.gmail.com'),
        'username' => 'xxx@gmail.com',
        'password' => 'xxssxxxx',
        'port' => '465',
        'encryption' => 'ssl',
        'streamOptions' => [ 
            'ssl' => [ 
                'allow_self_signed' => true,
                'verify_peer' => false,
                'verify_peer_name' => false,
            ],
        ]
    ]

Editor's note: disabling SSL verification has security implications. Without verification of the authenticity of SSL/HTTPS connections, a malicious attacker can impersonate a trusted endpoint (such as GitHub or some other remote Git host), and you'll be vulnerable to a Man-in-the-Middle Attack. Be sure you fully understand the security issues before using this as a solution.

PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
saeedeh
  • 343
  • 2
  • 7
2

Try using the gmail smtp IP

            'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => '64.233.171.108',
            'username' => 'XXXXXXX@gmail.com',
            'password' => 'XXXXXXX',
            'port' => '587',
            'encryption' => 'tls',
        ],

Some production servers cannot solve FQDN via DNS servers

1

For gmail: encryption must be setted to tls, port to 587 and host to smtp.gmail.com (check if your gethostbyname('smtp.gmail.com'), get the correct value) see the sample below:

 'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => 'yourUsername@gmail.com',
            'password' => 'yourPassword',
            'port' => '587',
            'encryption' => 'tls',
        ],
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • I've already tried this configuration and I got the above error related to SSL operation failed when using tls. I tried them both ssl and tls with and without the getHostByName function and it gives one of the two results mentioned above. – Betty Sanchez Jun 25 '15 at 18:33
  • Are you sure, you are usinge gmail like mail server for routing the emali?l or could be you are based on another mail server.? eg your application is hosted on a server. – ScaisEdge Jun 25 '15 at 18:38
  • My application is hosted on a local xampp server. My partner works with the same localhost and she doesnt have the problem with the same configuration. Our difference is that she uses Windows and I'm on a mac. – Betty Sanchez Jul 30 '15 at 12:48
0

Please check :

  1. Internet connection
  2. email Configuration

This is my configuration. An Email succesfuly send.

 'components'=>[
        'mailer' => [
           'class' => 'yii\swiftmailer\Mailer',
           'viewPath' => '@app/mail',
           'useFileTransport' => false,
           'transport' => [
              'class'=>'Swift_SmtpTransport',
              'host'=>'smtp.gmail.com', //sample
              'username'=>'my@emaul.com', //sample email
              'password'=>'~!@#$%%^&&', // sample password
              'port'=>'465', // gmail ssl use port 465, 
              'encryption'=>'ssl',
           ],
       ],
    ],
0

I had this issue my oversight being i was connecting with tls as my encryption , while the remote server only supported the older ssl encryption.

All i did was to change my encryption parameter value from "tls" to "ssl" and everything worked.

I hope this helps someone.

chitwarnold
  • 1,331
  • 2
  • 8
  • 11
-1

Adding streamOptions solved my problems!

'streamOptions' => [ 
        'ssl' => [ 
            'allow_self_signed' => true,
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ]
Condesign
  • 49
  • 2
  • 2
    This creates security issue - it will open you to MITM attacks. **Do not use it**. – rob006 Jul 10 '18 at 12:36
  • Thanks for your comment. I've removed it, but somehow my mail didn't work without this solution. Now google has done something and it works. – Condesign Dec 07 '20 at 16:21