Yii2 smtp error 500 5.5.1 command unrecognized - php

In my Yii2 application i've a contact form that sends email with swift.
I've configured my application in this way:
those are the configurations in web.php:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// 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,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'authsmtp.myhost.com',
'username' => 'username',
'password' => '************',
'port' => '25',
'encryption' => 'tls',
],
],
and this is the part of my controller where i send the mail:
if($contact->load($post) && $contact->validate())
{
Yii::$app->mailer->compose('request',['contact' => $contact ])
->setFrom('mail1#mail.it')
->setTo('mail2#mail.it')
->setSubject('This is a test email')
->send();
return $this->redirect(['login/index']);
}
I receive a Swift_TransportException with this error:
Expected response code 220 but got code "500", with message "500 5.5.1 command unrecognized"
Is there a way to retrieve more information about the error generated by swift?
This message is too general.
Thanks in advance for all the help

For logging use yii\swiftmailer\Logger:
Logger is a SwiftMailer plugin, which allows passing of the SwiftMailer internal logs to the Yii logging mechanism. Each native SwiftMailer log message will be converted into Yii 'info' log entry.
SMTP ports are 465 or 587 with SSL/TLS, and 25 without. From the Wikipedia page on SMTP:
SMTP by default uses TCP port 25. The protocol for mail submission is the same, but uses port 587. SMTP connections secured by SSL, known as SMTPS, default to port 465 (nonstandard, but sometimes used for legacy reasons).
Unless your server has been explicitly set to use port 25 with SSL/TLS you shouldn't use encryption.

Related

Why the Swiftmailer in Yii2 does not work and IP in blacklist?

The email was worked, but now it is crash because IP is blacklisted.
How to resolve this problem?
When I send email using Swiftmailer this error I see
Expected response code 220 but got code "550", with the message
"550-Message rejected because [50.87.249.98]:20423 is blacklisted see
Blocked - 550 Too many failed logins "
This code in conmmon/config/main-local.php
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'box1298.bluehost.com',
'username' => 'test#domain.co', // my email
'password' => '**************', // password
'port' => 465,
'encryption' => 'ssl',
'streamOptions' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
]
],
],
This has nothing to do with your code. If your SMTP server blocks your IP you should contact with your hosting provider and ask about unblocking this IP. Or wait - such blocks are usually temporary.
But in your case it looks like your server blocked itself, so I suggest to contact with hosting support to explain the situation.
Shameless plug: you can use a free email API like Flute Mail which automatically circumvents temporary blacklists. How it works: you can setup multiple email servers or providers on your Flute account, and it will resend your email through a different provider if it detects an error like this.
It's completely free for small volume senders, so all you need to do is change your code to forward requests through your Virtual Flute (and then pop replace your SMTP credentials). The Virtual Flute will need to be be configured to send through your SMTP server (or other free email APIs like Mailgun).
Let me know if you have any questions about Flute (I built it).

Swift_TransportExceptionConnection could not be established with host smtp.gmail.com

Trying to send email in php.
transport: smtp
host: smtp.gmail.com
username: example#example.com
password: password
port: 587
encryption: ssl
Options already tried:
Tried all combinations of port (22, 465, 587) and encryption (No encryption, SSL, TLS)
Allow access to less secure apps is on and two ways authentication is off.
checked host details:
and tried with above host name and ips.
Tested mailtrap.io and its working fine! (receiving email in mailtrap inbox, so no issue in code)
I'm using same gmail account in another .net application and its working fine over there.
What could be the option left to try?
I use this and work
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => false,//set this property to false to send mails to real email addresses
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'your_name#gmail.com', // a valid gmail account
'password' => 'your_password', // the related passwordd
'port' => '587',
'encryption' => 'tls',
],
Added this line in streamBuffer.php and it worked!
$options = array_merge($options, array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));
Added this line in streamBuffer.php and it worked!
$options = array_merge($options, array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));
Don't disable ssl certificate verification - this will open you to MITM attacks.
I'm pretty sure that Gmail has correct certificate, so you should try to fix this at your end.
Make sure that you have proper Certificate authority bundle at your server.
If you're behind some trusted proxy which works like MITM, you should add proxy's certificate to your trusted certificates.
You may want to look at How to update cURL CA bundle on RedHat?, How do you add a certificate authority (CA) to Ubuntu? or similar instructions related to your system.
Also, take a look at similar issue at Composer bug tracker.

Using gmail smtp via Laravel: [Connection timed out #110]

I know this question is asked many times and have different solutions and I have tried all but no one has worked. This is my settings.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=abc#mydomain.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl
And application is currently running on Centos 7 OS.
I always get
Swift_TransportException in StreamBuffer.php line 269: Connection could not be established with host smtp.gmail.com [Connection timed out #110]
at Swift_Transport_StreamBuffer->initialize(array('protocol' => 'ssl', 'host' => 'smtp.gmail.com', 'port' => '465', 'timeout' => '30', 'blocking' => '1', 'tls' => false, 'type' => '1', 'stream_context_options' => array())) in AbstractSmtpTransport.php line 113
I have tried all solutions, by Replacing: smtp.gmail.com with 173.194.65.108, or replacing smtp.gmail.com by gmail-smtp-msa.l.google.com. When I do these two steps, it got server not found error.
If I change mail driver from smtp to sendmail or mail, no error is thrown but no email is sent.
If I use ip address of smtp.gmail.com, I got same result. If I change port from 465 to 587 and encryption from ssl to tls, nothing happens.
I have also created a file in etc/gai.conf and put precedence ::ffff:0:0/96 100. It worked one time. But later after two hours, it stopped working and started throwing same error.
I have also tried using my gmail id but all in vain.
The only solution that I couldn't be able to test is verifying httpd_can_sendmail as whenever I run command getsebool httpd_can_sendmail I get getsebool: SELinux is disabled. Is this the problem or is there any other way to get it fixed?
Here is my working mail config file:
return [
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => ['address' => '*******#gmail.com', 'name' => '****'],
'encryption' => 'tls',
'username' => '*******#gmail.com',
'password' => '*******',
'sendmail' => '/usr/sbin/sendmail -bs',
];
but from your question i can see you already tried all this, so i can assume it is a network problem based on error 'connection time out', what i mean is that something is blocking your connection, it might be your firewall if you have.
so first you should check that the port 587 is open, or you can check if other application can connect with smtp.gmail.com (don't know how to do it in centOs).
And at last this might not be a problem but you have to enable 'Allow less secure apps' in your google account just for testing. (This is not permanent solution but for testing, if it works then you should enable two step verification in google account and then create new application, you can create new special password which you can use as your password for smtp server)
In my case it was necessary to clear config cache to pull up .env file new settings
php artisan config:cache
Since you are using gmail, but I don't know what password you are setting, the MAIL_PASSWORD variable must be the same as the gmail application password. In this case, Gmail provides a third-party application service so that these, (in this case your application), can send emails using this type of authentication.

Sending Mail via SMTP on Japan ISP

I have this weird problem. I have setup my PHP app to send email to users, for now it is using gmail. There is no problem sending mail from my machine. But when my colleague in Japan tried my app, it doesn't send the mail. In the logs, it says that the app failed to authenticate.
'driver' => 'smtp',
'smtp' => array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'test.email#gmail.com',
'password' => 'password',
'timeout' => 5,
),
'newline' => "\r\n"
He is running the app in his machine, not from a remote server.
Google might have blocked your login because it was deemed as suspicious. (https://support.google.com/accounts/answer/6063333?hl=en)
You might want to login to this account and navigate to https://security.google.com/settings/security/activity and allow blocked device to continue.

Swift_TransportException Failed to authenticate on SMTP server with username email#domain.com using 2 possible authenticators on shared host

I'm develop a website using Laravel 4.1. I config my email system and send email successfully on built-in server localhost:8000. But when I host the web on a shared hosting, it always tell: Swift_TransportException
Failed to authenticate on SMTP server with username "email#domain.com" using 2 possible authenticators
I don't know what the reason is? I've search on this site but no solution works.
return array(
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => array('address' => 'email#gmail.com', 'name' => 'Test email'),
'encryption' => 'ssl',
'username' => 'your_gmail_username',
'password' => 'your_gmail_password',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
I also used my our domain's email but it doesn't work neither. Hope for help.
In my case, I had a Cpanel dedicated server. My host informed me that exim mail server was failing and restarting a couple times a day.
It looked like all connection threads were being used up and then locked until it restarts.
My host added another 100 connection threads to the pool. Most probably this help you as well.

Categories