Swiftmailer from Advanced Yii 2.0 does not work with gmail - yii2

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?

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.

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

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',
],

Please check :
Internet connection
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',
],
],
],

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.

Adding streamOptions solved my problems!
'streamOptions' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
]

Related

How to configure mail.php and service.php in laravel 9 project when i use Sendinblue smtp service

In development it works perfectly, the emails reach the recipient, but when I display the project in cpanel I get a 500 error when my client presses the send button.
Currently my configuration is like this:
.ENV:
MAIL_MAILER=smtp
MAIL_HOST=smtp-relay.sendinblue.com
MAIL_PORT=587
MAIL_USERNAME=myEmailUserSendinblue
MAIL_PASSWORD=passwordMySendiblueService
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="EmailClient"
MAIL_FROM_NAME="${APP_NAME}"
SENDINBLUE_API_KEY=xxxxxxxxxxxxxxxxxxxxxxx
mail.php:
'mailers' => [
'sendinblue' => [
'transport' => 'sendinblue',
'host' => env('MAIL_HOST', 'smtp-relay.sendinblue.com'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
],
services.php:
return [
'sendinblue' => [
'api_key' => env('SENDINBLUE_API_KEY'),
],
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
'scheme' => 'https',
],
'postmark' => [
'token' => env('POSTMARK_TOKEN'),
],
'ses' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
];
Thank you very much in advance, I remain attentive to your comments.
EDIT 1:
I got an error through the laravel logs, it is as follows:
production.ERROR: Unsupported mail transport [sendinblue]. {"exception":"[object] (InvalidArgumentException(code: 0): Unsupported mail transport [sendinblue]. at /home/-------/public_html/vendor/laravel/framework/src/Illuminate/Mail/MailManager.php:157)
[stacktrace]

Swift_TransportException Failed to authenticate on SMTP server with username "example#gmail.com" using 2 possible authenticators

I am using yii2 mailer function and i am faced the issue the error is as below
Swift_TransportException
Failed to authenticate on SMTP server with username "example#gmail.com" using 2 possible authenticators. Authenticator LOGIN returned Swift_TransportException: Expected response code 235 but got code "535", with message "535 Incorrect authentication data
here is my configuration for mailer
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com', // e.g. smtp.mandrillapp.com or smtp.gmail.com
'username' => 'example#gmail.com', //ses-smtp-user.20180221-103827 username: administrator
'password' => "example123#",
'port' => '587', // Port 25 is a very common port too 25, 465 or 587
'encryption' => 'tls', // It is often used, check your provider or mail server specs
'streamOptions' => [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
]
],
],
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => false,
],
if anyone knows what's the issue please let me know i am not getting where is the mistake thanks!

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

i am trying to send mail via swift mailer using yii2 but it is getting this error.
Swift_TransportException Connection could not be established with host smtp.gmail.com [Connection timed out #110]
when i send mail using local host than it is sending but when i used the these settings on my test server than it is not sending the mail.
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=db553769847.db.1and1.com;dbname=db553769847',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport'=>false,
'viewPath' => '#common/mail',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host'=>'smtp.gmail.com',
'username' => 'myuserename',
'password' =>'mypassword',
'port'=>'587',
'encryption'=>'tls',
],
],
],
];

Yii2 mail in not sending on portal.azure.com

I have migrate a Yii2 website from Apache server to portal.azure.com windows server. I have checked that mail is not sending on windows server.
Do anyone have any idea, what I have missed in this.
Thanks
I have used send-grid(https://sendgrid.com/) to setup mail system in Yii2 on portal.azure.com. It should be like below:-
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.sendgrid.net',
'username' => 'azure_************#azure.com',
'password' => '***********',
'port' => '25',
//'encryption' => 'tls',
],
],
],

Yii2 SwiftMailer sending email via remote smtp server (gmail)

I want to send emails via my gmail account.
My mailer config:
[
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,//set this property to false to send mails to real email addresses
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'my#gmail.com',
'password' => 'pass',
'port' => '587',
'encryption' => 'tls',
],
]
I wrote command MailController:
<?php
namespace app\commands;
use yii\console\Controller;
use Yii;
/**
* Sanding mail
* Class MailController
* #package app\commands
*/
class MailController extends Controller
{
private $from = 'my#gmail.com';
private $to = 'to#gmail.com';
public function actionIndex($type = 'test', $data = null)
{
Yii::$app->mailer->compose($type, ['data' => $data])
->setFrom($this->from)
->setTo($this->to)
->setSubject($this->subjects[$type])
->send();
}
}
When I'm trying to run: php yii mail
I get: sh: 1: /usr/sbin/sendmail: not found
But why it requires sendmail if I want just SMTP connection to smtp.gmail.com?
I think you have configured the mailer wrongly. Because it is still using the default mail function. From the documentation the configuration should be like below. The mailer should be inside components.
'components' => [
...
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'username',
'password' => 'password',
'port' => '587',
'encryption' => 'tls',
],
],
...
],
One more suggestion is to use port "465" and encryption as "ssl" instead of port "587", encryption "tls".
Yii2 Has different config files for web and console works. So you need to config both of them. Regarding this issue, I had to make mail config file (for example mailer.php) and include it in both config files (web.php & console.php) like:
'components' => [
...
'mailer' => require(__DIR__ . '/mailer.php'),
...
],
Might be useful for someone as reference:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'username',
'password' => 'password',
'port' => 587,
'encryption' => 'tls',
'streamOptions' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
]
],
]