I'm working on a project where I'd like to test the emails being sent out to users, but so far i got no luck setting it up. I'm using Codeception and running it inside Vagrant. What I'm trying to do now:
return \Yii::$app->mailer->compose()
->setFrom($from)
->setTo($to)
->setSubject('Welcome')
->send();
I've got a main-local.php, which looks like this:
'mailer'=> ['class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'localhost',
'port' => 1025,
'useFileTransport' => false,
],
At first I left out 'useFileTransport' => false, then I added it before 'transport' and tried moving it inside 'transport', as you can see now, but it's the same. The file/email gets created in the project as .eml file, but no mail is sent. I tried setting up mailcatcher (http://mailcatcher.me/), and actually got that working, but it would be nice if I could just change in the config, instead of using a different method and changing code, and the mail would be sent where I want it. (I also tried with port 1080, so that's not it.)
Any help would be appreciated. If you need more info, please let me know!
Update:
I found out that I got a different config file, where I'm actually able to use 'useFileTransport' => false, and then it won't create the file/email in the project, but still not sending it. I copied the settings shown from main-local.php into this config file, moved 'useFileTransport' => false up before 'transport', but I'm still looking for a way to send these emails
$useFileTransport is a property of the yii\swiftmailer\Mailer class, so it does not belong into transport part.
If your mailer is configured like the following, it will try to deliver mails via SMTP to the mail server running on your localhost on port 1025:
'mailer'=> [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'localhost',
'port' => 1025,
],
],
This sounds somehow unusual to me as the default port for SMTP is 25.
Make sure the SMTP server is actually running on that port. E.g. by using the telnet command:
telnet localhost 1025
which should give you something like this:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 your.servername ESMTP Postfix
Exit the SMTP session by typing quit and hit enter.
You may also check the mailserver log file which is usually located at /var/log/mail.log. You can view messages being logged using
tail -f /var/log/mail.log
while you are trying to send them.
Took me a few days, but finally found the problem!
So the problem was the other config i mentioned in the update section. Hidden inside codeception/config/config.php was missing a line
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
//For email creation in project
//'useFileTransport' => true,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'localhost',
'port' => 1025,
'encryption' => '',
],
],
All i needed was the encryption, which just needs to be empty. Emails are now being sent how they're supposed to, and looking good!
Hope this helps somebody out there, happy testing!
Related
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).
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.
I am using Swiftmailer in Yii2 advanced and everything, this includes sending emails, works fine on my xampp server. But when I try it on my external Server (1&1 btw) I get a
Swift_TransportException
Connection could not be established with host
smtp.1und1.de [Connection timed out #110]
I already googled about it for hours an tried almost every solution including using gmail instead or using ports 465 and 25 with ssl and tls. Also using IP-Adress instead of hostname.
I feel like there could be a firewall or some email options I miss but I have tried everything I can think of.
My code looks like this:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.1und1.de',
'username' => 'mymail#myadress.de',
'password' => 'mypassword',
'port' => '587',
'encryption' => 'tls',
],
],
and again, works perfectly fine on localhost.
Maybe someone has an idea what I am doing wrong?
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.
I wrote this code a couple of months ago. I used to be able to send email with the following configuration. But suddenly today, I am getting the following error.
Swift_TransportException in StreamBuffer.php line 265:
Connection could not be established with host localhost [Connection timed out #110]
This is the configuration from which I was able to send emails.
'driver' => 'smtp',
'host' => 'localhost',
'port' => 587,
'from' => ['address' => "test#xxx.com", 'name' => "test"],
'encryption' => 'tls',
'username' => 'test#xxx.com',
'password' => '*********',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
I have solved the problem. The configuration is wrong. I dont know why it worked earlier. But now I have followed the instructions from this link and now it is working.
Step 1 -> Go to "Email Account". You can find the icon at the home page of the cpanel.
Step 2 -> Click on the account next to the account you want to use. You will see a dropdown menu. There you will see an option saying "Configure Mail Client". Click on it.
Step 3 -> There at the bottom of the page you will find the proper settings. Look for outgoing server settings.