Gmail SMTP is not working in ec2 instance - php

i am using Ubuntu based ec2 instance, recently i have shifted my code on aws and it seemed email functionality is not working. I am using gmail SMTP service
below is my code
'Smtpmail'=>array(
'class'=>'application.extensions.smtpmail.PHPMailer',
'Host'=>"smtp.gmail.com",
'Username'=>'username',
'Password'=>'password',
'From'=>'from addr',
'setForm'=>'from name',
'Mailer'=>'smtp',
'SMTPSecure'=>'tls',
'Port'=>25,
'SMTPAuth'=>true,
),
this code is working on my unix based server but on aws it is giving below error
Mailer Error: The following From address failed: test#email.com : MAIL not accepted from server,530,5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 y6sm27370508qen.21 - gsmtp
Please help

MAIL not accepted from server
Because of the spam abuse that has historically been sent from people using EC2 instances, virtually ALL popular mail providers block the receipt of email from EC2 instances. The world of email and anti-spam measures is part-technical, part-political. For this reason, AWS offers Amazon Simple Email Service.
AWS works with mail providers to ensure that the nodes used by SES have been whitelisted because we do proper authorization/verification up-front.
In this case, the ability to send email from one server but not AWS is, in all likelihood, due to EC2's IP range being blacklisted by Google.
Authentication Required
If the same user/pass works elsewhere without changes, I'm not sure what to say. I know that lots and lots of mail traffic no longer uses port 25, opting instead for 587 for non-SSL and 465 for SSL. That's the first place I'd start poking to find a solution.

Using a TLS connection (port number: 587) on AWS Lambda.
How I solved the problem:
In your GMAIL account, turn on 2-factor authentication. This can be found under security settings when you go to general settings for your account.
Create a new App Password. (The field is right below the 2-factor). You can call it whatever you want. Just make sure to copy it.
Use that for your previous password.
I did that and I can send emails from AWS Lambda with no problem.
Note: You can't have "Less Secure Apps" turned on for this option. This will require you to use the App password for every application you use this email for. I had to make a few different passwords for the different apps that I use a random Gmail account for.
I got the idea from SMTP Authentication Error 534.

This has nothing to do with your ec2 instance (unless the relevant outbound ports are closed) but with the fact that Gmail really doesn't like a connection it can't identify. As stated in the thread, make sure you go to https://www.google.com/settings/security/lesssecureapps.
Then, once your instance tries to connect to the SMTP server (by sending an email in your web app I suppose), connect to your mail client with the gmail account credentials used for the SMTP connection (Gmail, thunderbird, whatever you normally use). You will have then received an email from google stating that there was some suspicious activity and that they blocked the connection, just tell them that the ec2 instance IP was indeed you and you will be all set.

I was using a Gsuite account. For me, it worked with the following steps
Make sure you have 2 step verification enabled
Generate App password from myaccount.google.com -> security (from left panel) -> 2-step verification (under "signing in to google" section)
Once 2 step verification is enabled then generate the app password.
Use that app password in your config/mail.php on .env along with the following settings
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls
Open the terminal/ command prompt and connect to the EC2 server and run the following commands to clear config and cache
php artisan config:cache
php artisan cache:clear
If that still does not work then click on the following link (Make sure you are logged in with the account which you want to use to send email via Gsuite)
https://accounts.google.com/DisplayUnlockCaptcha

This only need to verify gmail account when u login from another ip address it will ask for verification that you are the same person same thing happen here it is not verified from amazon web server IP so it require to authenticate your IP.
Do only one thing will solve the issue. Login remotely on aws server and login your gmail account from here it will ask for verification give it and your mail will work easily.

If you use port 25 for SMTP you need to open SMTP port on EC2 instance security group inbound rules.
Note: Gmail does not offer port 25 in an unsecured connection. For use port 25 secure connection are required as per gmail Configuration requirements
Read this question & answer
How do I use Gmail SMTP with port 587 on AWS EC2?

Go to https://www.google.com/settings/security/lesssecureapps and set Access for less secure apps to On
This gmail security option if on Off detects if there is inhabitual connexion pattern and block the connexion unless you verify your identity, but you need a web interface to do so.

Log into your AWS account, add rule under security group of your instance and allow the SMTP, it will work, check the port number is important.

Related

PHPMailer GMAIL XOAUTH2 with multiple GMAIL accounts for multiple virtual hosts

I host a web application that each client uses a different host name. I have successfully configured PHPMailer in the application to allow the client to enter their own SMTP information and send emails. Unfortunately, when the client uses Google's email service, the client needs to configure Gmail to allow less secure email clients. Instead of requiring the client to allow less secure email clients, I would like to use setup XOAUTH2.
All of the instructions I found setting up XOAUTH2 with Google's APIs, configure one host. I would like to setup XOAUTH2 allow multiple hosts without manually adding a new host to Google's API. Any help is greatly appreciated.
I don't think you can. I don't remember if in the callback urls you can put differents domains for the same application (I never tested that). But the consents screen can use only one.

Send email via SMTP of gmail through proxy localhost

I'm building a laravel website with simple email sending functionality. I can't get my email sending work. I've been doing a lot of email sending before (PHP /Laravel) but this time, i'm super stuck with my current set up. I already used Mailtrap,SMTP gmail,mailgun, sendmail of XAMPP, sendmail of laragon but none of them get to work. I am just trying to send mail to my test gmail account on my localhost but the error keep showing
SMTP ERROR: Failed to connect to server: A connection attempt failed
because the connected party did not properly respond after a period of
time, or established connection failed because connected host has failed
to respond.(10060)
Since i am not directly connected to the internet, i'm suspecting my proxy server. Is there some kind of configuration to be set up on my machine? already try some bunch of tutorial but don't get any luck.
You can send emails from your localhost using your gmail account
But you need to first create an app password in your Google Account settings
Choose Sign-in & Security
If you are logged in scroll down and find the section Signing in to Google and click on App Passwords in the box to the right
Generate app password using Select the app and device you want to generate the app password for form. Select App, Select Device and click Generate. You should see a password generated.
Configure your .env file to look like this:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremailusername
MAIL_PASSWORD=generated-password
MAIL_ENCRYPTION=tls
Run php artisan config:clear
and you should now be able to send email form localhost if your Mail sending code is okay and you are connected to the internet.
I found out that our proxy server is blocking all SMTP. We cannot use any outside smtp. We need to contact our admin to allow Gmail SMTP.
Thank u!

configure 1and1 mail on google cloud platform with laravel 5

I have setup an instance in google cloud platform. I am running laravel 5.2 project on that instance. Everything is working good but i am facing some issues while configuring 1and1 mail.
I know that google cloud platform is blocked the 587,465 and 25 port by default and we can setup some vpn which will help to remove this restriction. But i don't want to go in that way because it would have security problems.
I have tried to send email through smtp and mail driver. I am getting connection failure error through smtp driver, and through mail driver, it shows that email is sent but i am not getting the email in my inbox. Here is my .env file for mail driver settings for 1and1. I have also tried 25 port also.
MAIL_DRIVER=mail
MAIL_HOST=auth.smtp.1and1.co.uk
MAIL_PORT=587
MAIL_USERNAME=test#mydomain.com
MAIL_PASSWORD=pass
MAIL_ENCRYPTION=tls
Can you please help me to configure it?
As you alluded to, and documented, GCE doesn't allow outbound 25, 465 or 587. This is blocking your connection to 1and1.
If you can ask 1and1 to provide an alternate SMTP port (e.g. 2525 etc) then you can use that without issue.
Google also has partners that will allow you to send up to tens of thousands of emails monthly, for free. Pick one and follow the instructions here.

Error connection mandrill (Laravel)

I have an issue with the mandrill API, I got this message :
Failed to connect to mandrillapp.com port 443: Connection timed out
It's works on my production server (ex mysite.com), but not on my dev server (ex mysite.com:8080).
The two servers have same config by the way.
Anyone knows why I got this issue ?
Thanks !
Make sure your hosting provider or internet provider (ISP) allows
outbound SMTP connections. Some shared hosting providers only allow
outbound SMTP connections on dedicated servers, while others block
them completely. In some cases, hosting providers might redirect the
connection, so instead of connecting to smtp.mandrillapp.com, you
connect to their local server instead.
Make sure the port you've selected is one that your hosting provider
or ISP has available for outbound SMTP connections. Some hosts block
all connections on port 25, for example, so you can try using a
different supported port.
Double check that you're using a valid API key to connect via SMTP,
and not the password that's used to log in to Mandrill's web
application.
If you're using Postfix, make sure that you have an SASL library
(like libsasl2 or cyrus) installed and that it's up to date.
Otherwise, you may be connecting but not passing authentication
credentials.
For other SMTP libraries, make sure you're using LOGIN or PLAIN
authentication methods.
Once you've confirmed all of the above, if you're still seeing issues,
please enable additional logging in your SMTP program or library. If
you're using an integration, contact the integration developer for
information on configuring logging of the SMTP conversation.
Where do I find my SMTP credentials?
After you create a Mandrill account, get your SMTP credentials on the
SMTP & API Info page in your account.
The SMTP password is any active API key for your account, not the
password used to log in to Mandrill. The credentials list port 587,
but any port supported by Mandrill will work, and there's no
configuration change needed within Mandrill to activate one of the
alternate ports.
Which SMTP ports can I use?
You can use port 25, 587, or 2525 if you're not encrypting the
communication between your system and Mandrill or if you want to use
the STARTTLS extension (also known as TLS encryption). SSL is
supported on port 465.
ISPs may redirect traffic on certain ports, so it's up to you which
port you use.

PHP PEAR mail fails using Google Apps mail account - error 535 authentication failed

I have a PHP page that uses PEAR Mail to send a message through a Google Apps mail account. The page is only active in the fall and winter, and it was working great when it was last used in January.
Now, as I prep the site for the fall, this page no longer works. I have the exact same problem as described at SMTP Error 535 (Incorrect authentication data) using PEAR mail There have been no answers to that question and it won't allow me to comment, so I'm starting a new question.
I am using code identical to that posted by pavan kumar at Send email using the GMail SMTP server from a PHP page .
The error message is:
authentication failure [SMTP: Invalid response code received from server (code: 535, response: Incorrect authentication data)]
I double-checked that the login for the mail user had not been changed since January. I copied and pasted the address and password from my script into the Gmail login in my browser. It worked, so I know my parameters are correct. This account is a business domain address, not #gmail.com.
I am trying to connect at:
$host = "ssl://smtp.gmail.com";
$port = "465";
I also tried specifying tls:// and port 587, no luck. I also set the host to "googlemail.com" instead of "gmail.com" per a suggestion in another SO thread.
I did a var_dump() of the $smtp object, and confirmed my address and password were passed into it successfully and accurately.
The account is on a cPanel system that recently upgraded to PHP 5.3.5 and Apache 2.2.22. It seems unlikely that change could have triggered this problem, but I mention it in case it could be a factor.
Edit: Found http://support.google.com/a/bin/answer.py?hl=en&answer=176600 which says "...you must configure an SPF record for your domain with the IP address of the device or application to ensure that recipients do not reject mail sent from it. You must also add this IP address to the Email Whitelist box in your Google Apps control panel. For example. if your sending device sends from 123.45.67.89, add that address to your SPF record without removing the Google Apps mail servers from the record..." Unfortunately I don't have an Email Whitelist box in this domain - it's the free <10 user account which evidently doesn't have that feature.
Edit 2:
Related: http://support.google.com/mail/bin/answer.py?hl=en&answer=78775
Edit 3:
I set up the account in Opera mail, and sent successfully using both AUTH LOGIN and AUTH PLAIN settings.
The problem was located at my web host, not Google's mail server.
The account is on a cPanel server, and the admin had set it so that only designated user accounts could access external SMTP servers. He disabled that requirement in order for me to test, and my mail system immediately started working.
So far, though, I have found no documentation on how to log into cPanel as one of the authorized users when its SMTP Authentication is activated. I will update this if I find some.

Categories