Hi, can you try set
MAIL_ENCRYTPION=tls or empty
PORT=587
MAIL_HOST=smtp.your-domain
It's work for me.
Martin
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello all!
I'm trying to send mail on my web app with gmail and I always get the error
Swift_TransportException in StreamBuffer.php line 265:
Connection could not be established with host smtp.gmail.com [Connection timed out #110]
my .env is:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=******@gmail.com
MAIL_PASSWORD=*****
MAIL_ENCRYPTION=ssl
I'm using Digital Ocean, And the have already allowed smtp on my account.
Any suggestions? Thank you!
Hi, can you try set
MAIL_ENCRYTPION=tls or empty
PORT=587
MAIL_HOST=smtp.your-domain
It's work for me.
Martin
I'm afraid I already tried that, and it didn't work.
Have you set FROM and FROM_NAME when you sending email?
@Mbezdek I did, and it worked on localhost, but not on the server.
Some new users at DigitalOcean have their SMTP ports blocked by default. It sounds like this might have been the case for you. If you open a support ticket, the team would be happy to remove that block for you.
and
In your gmail, Click on settings -> click on the Forwarding/IMAP tab and scroll down to the IMAP Access section: IMAP must be enabled in order for emails to be properly copied to your sent folder.
The DigitalOcean staff already unblocked the ports, and IMAP is already enabled.
I have a 4.2 installation that routes through a Gmail account using port = 587 and encryption = 'tls' that works. Maybe try those settings in your env file. I'm assuming you're on a newer version of Laravel here.
I see that maybe you already tried that...
and what about this? https://www.google.com/settings/security/lesssecureapps
Did this as well, this problem is driving me crazy...
Stay calm :) Can you send me your actual .env file (just a bit with MAIL config) and config/mail.php file?
the .env is on the post, here is the config/mail.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "ses", "log"
|
*/
'driver' => env('MAIL_DRIVER', 'smtp'),
/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Mailgun mail service which will provide reliable deliveries.
|
*/
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to deliver e-mails to
| users of the application. Like the host we have set this value to
| stay compatible with the Mailgun e-mail application by default.
|
*/
'port' => env('MAIL_PORT', 587),
/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/
'from' => ['address' => null, 'name' => null],
/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/
'username' => env('MAIL_USERNAME'),
/*
|--------------------------------------------------------------------------
| SMTP Server Password
|--------------------------------------------------------------------------
|
| Here you may set the password required by your SMTP server to send out
| messages from your application. This will be given to the server on
| connection so that the application will be able to send messages.
|
*/
'password' => env('MAIL_PASSWORD'),
/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/
'sendmail' => '/usr/sbin/sendmail -bs',
/*
|--------------------------------------------------------------------------
| Mail "Pretend"
|--------------------------------------------------------------------------
|
| When this option is enabled, e-mail will not actually be sent over the
| web and will instead be written to your application's logs files so
| you may inspect the message. This is great for local development.
|
*/
'pretend' => false,
];
Ok, works send email with standart PHP mail(); function?
It doesn't return the error, but it doesn't send the mail.
Call
print_r(error_get_last())
after mail function is called.
It prints nothing, but I think it's the configuration, isn't it different using the PHP's mail driver, isn't it?
Yes, you're right. But I wanted to try that error is in Laravel config or in server configuration. If mail function really doesn't work, error is in server configuration. But why any error?
Can you try add error_reporting(-1); before mail function is called?
it's returning null
error_reporting(-1); return null, it only set php to show all errors. And if you now call get last error func, still return nothing?
dd(error_get_last()); returns null
Ok, it's really weird.
I was looking for some informations about this problem and I found this tutorial.
https://www.digitalocean.com/community/tutorials/how-to-use-gmail-or-yahoo-with-php-mail-function Did you see it?
I solved it using mailgun. Thanks for the replies @Mbezdek !
Has anyone managed to use a gmail account? If so please list the working attributes naming 'port' and 'encryption'.
1: Either you must allow less secure apps or use app password by enabling 2 step verification on your gmail acc.
2: Disable any antivirus on your machine(this is something no one will tell u but is necessary in many cases).
3: Don't forget to clear your cache (sometimes it don't take updated content from .env file)
4: You don't need to change anything in Mailer inside config directory(not recommended), you should use env variables
Please or to participate in this conversation.