Server error when sending mail with mailgun on amazon ec2 - laravel

I have Laravel app with a mailgun account for handling emails.
My app is on amazon instance.
When sending mail on local all works great, but when deploy it gives me 'server error' what makes me think that I should do something on my amazon instance.
Any help with this will be great!
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=2525
MAIL_USERNAME=username
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null
The error that I am getting is :
Connection could not be established with host smtp.mailtgun.org
[php_network_getaddresses: getaddrinfo failed: Name or service not
known #0]
running on Ubuntu

Related

Google mail of a laravel project not working in cpanel

I was using the following lines in .env file when I was on the localhost and everything was working:
MAIL_MAILER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=myaddress#gmail.com
MAIL_PASSWORD=mypwd
MAIL_ENCRYPTION=ssl
But the moment I hosted the laravel project through cpanel it shows the following error:
Connection could not be established with host smtp.googlemail.com :stream_socket_client(): unable to connect to ssl://smtp.googlemail.com:465 (Network is unreachable)
I then tried with the following .env file, the form sends the mail but I cant actually receive anything:
MAIL_MAILER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465 //tried amending it to 587
MAIL_USERNAME=myaddress#gmail.com
MAIL_PASSWORD=mypwd
MAIL_ENCRYPTION=ssl //tried amending it to tsl when I changed the port to 587 but still same issue
You can’t send mail without enabling less secure apps in google with third-party apps. So let's enable it.
Log in your Gmail account which you want to use for this
Open URL https://myaccount.google.com/lesssecureapps
If it is OFF then make it ON.
Your .env file will then look something like this
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=ENTER_YOUR_EMAIL_ADDRESS(GMAIL)
MAIL_PASSWORD=ENTER_YOUR_GMAIL_PASSWORD
MAIL_ENCRYPTION=ssl
Don't forget to run php artisan config:cache after you make changes in your .env file.

Connection could not be established with host smtp.sendgrid.net :stream_socket_client(): unable to connect to tcp://smtp.sendgrid.net:465

I am trying to send mail using sendgrid in Laravel but it is working on localserver but as i hosted it on server it is giving me following error message:
my mail settings in .env file:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=myusername
MAIL_PASSWORD=XXX
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=info#xyz.com
MAIL_FROM_NAME=xyz
For laravel >= 7
And you are getting this error :
Connection could not be established with host smtp.sendgrid.net :stream_socket_client(): unable to connect to tcp://smtp.sendgrid.net:587 (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
Simply update your .env file
From:
MAIL_MAILER=smtp
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=your_sendgrid_api_key
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="Your Sender Name"
MAIL_FROM_ADDRESS=info#example.com
To:
MAIL_MAILER=sendMail
MAIL_DRIVER=sendMail
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=your_sendgrid_api_key
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="Your Sender Name"
MAIL_FROM_ADDRESS=info#example.com
Then run this command to make sure .env changes is sync with the code.
php artisan config:cache
Update your env file to:
MAIL_MAILER=sendmail
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=apiPassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=info#example.com
MAIL_FROM_NAME="${APP_NAME}"
Also in your Laravel project under the config folder look for
mail.php
and make changes as follows:
'default' => env('MAIL_MAILER', 'sendmail'),
N/B: SMTP works on localhost and sendmail works on live server.
If you say it works locally but not on production server, try a php artisan config:cache on your web host server. Make sure the ENV configuration is loaded properly (if your host does not support SSH connections, you need to manually update the configuration from ENV into /app/config/* files.
If the issue persists, there are only two possibilities:
Make sure your web host allows outgoing SMTP connections.
Make sure sendgrid.net did not ban your web host outgoing SMTP IP address.
I Experienced this while using a Linode server. Apparently, Some service provider like Linode blocks an SMTP port until you reach out to them via support and ask them for unblock before you can use that PORT and send Email. That was the fix for me
when I migrated to laravel 9, sending with sendgrid didn't work anymore.
The problem was because the new symfony mailer was trying to send with ssl beside tls was set in the config file.
So in the config file, I changed the port from 587 to 465 and it worked again.

Laravel SMTP mailtrap [Connection timed out #110]

I'm trying to send email with Laravel using mailtrap for testing in a local homestead server.
The firewall shows no rules blocking port 2525.
Mail driver config:
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=$USERNAME
MAIL_PASSWORD=$PASSWORD
MAIL_ENCRYPTION=null
Error returned:
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host mailtrap.io [Connection timed out #110]'
Your hostname is invalid. It should be smtp.mailtrap.io. More info here: https://mailtrap.io/blog/2018-03-09-why-mailtrap-updated-its-smtp-host
P.S. Mailtrap doesn't have static IP addresses, so your way with IPv4 will work not so long amount of time.
Adding to Alexey Vasiliev answer , after you fix the hostname don't forget to clear the cache using the following commands
php artisan config:clear
php artisan cache:clear

Error: connection could not be established with host smtp.gmail.com #0 - LARAVEL

I am trying send emails but I am getting the error below:
"connection could not be established with host smtp.gmail.com #0".
I made test with different ports (465, 578), with SSL and TLS and the last with the mail account and it is working.
I think that the problem is on XAMPP.
What am I doing wrong and how can I resolve it?
Try using:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_ENCRYPTION=tls
MAIL_USERNAME=y*****4#gmail.com
MAIL_PASSWORD=a*****
Hope, it works for you.

Email Sending not working in CentOS 7 but same credentials works on localhost

I created an email account on godaddy which works perfectly on server and on localhost. When I did the testing on Linode server using the same email credentials. It did not work and gave 500 Internal Server Error.
Swift_TransportException Connection could not be established with host
sg6plcpnl0067.prod.sin3.secureserver.net [ #0]
Am I missing something?
.env details
MAIL_DRIVER=smtp
MAIL_HOST=sg6plcpnl0067.prod.sin3.secureserver.net
MAIL_PORT=465
MAIL_USERNAME=noreply#myserver.in
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl
MAIL_FROM_NAME=myserver
MAIL_FROM_ADDRESS=noreply#myserver.in

Resources