Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jorge_fal's avatar

Mail trying to connect mail gun but env variables are setup to gmail

i setup my env variables to

APP_ENV=local APP_DEBUG=false APP_KEY=xxx APP_DOMAIN=xxx MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=xxx@gmail.com MAIL_PASSWORD=xxx

but when i try to send an email, i got an error on logs, says " 'Connection could not be established with host smtp.mailgun.org "

Thx for help!.

0 likes
8 replies
usman's avatar

@jorge_fal if you are testing from tinker shell then exit and rerun the tinker shell. If you are using the builtin php server then restart the server.

usman's avatar

@jorge_fal I think the .env file is not readable then. Try setting the readable permissions for the web server user.

jorge_fal's avatar

It's so weird, because in a controller i can print $test = env('MAIL_USERNAME') and get the real value, but when i send this variable to mail the app crash #how it works locally $emailStatus = Mail::send('emails.welcome', $data, function($message) use ($email, $fname) { $message->from($email, $fname); $message->to('xxx@gmail.com', 'testing message')->subject('testing subject'); });

#server test $test = env('MAIL_USERNAME') $emailStatus = Mail::send('emails.welcome', $data, function($message) use ($test, $fname) { $message->from($test, $fname); $message->to('xxx@gmail.com', 'testing message')->subject('testing subject'); });

then the app crash or still trying to connect to smtp.mailgun (only in server side)

i changed the permission of .env file but doesn't work yet. @usman

mstnorris's avatar

@jorge_fal check out the .env.example file directly on GitHub.

I think you may need to add MAIL_ENCRYPTION=null variable.

APP_ENV=local
APP_DEBUG=false
APP_KEY=xxx
APP_DOMAIN=xxx
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com // You're not using Gmail, you're using smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=xxx@gmail.com
MAIL_PASSWORD=xxx

but when i try to send an email, i got an error on logs, says " 'Connection could not be established with host smtp.mailgun.org "

You may be using the wrong host. Check your config/mail.php file and see what the defaults are and what you are/aren't overriding.

jorge_fal's avatar

i overwrite the defaults values in config/mail.php and doesn't work,

i always got this error: @mstnorris local.ERROR: exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.mailgun.org

jorge_fal's avatar

i run :

php artisan config:cache

And laravel takes env variables values (this solve the original problem). But i got another error with smtp.gmail.com. i cant still connect to smtp server.

I found the error, godaddy blocks the smtp ports, laravel and godaddy projects needs to use a mail services like mandrill.

Thx for help @mstnorris and @usman

Please or to participate in this conversation.