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

gouseferoz's avatar

Elastic Email is not working in Laravel

Hi,

I am using elastic email to send my emails. In local, i used mailtrap and it worked fine. I updated the .env file to use elastic mail credentials

MAIL_DRIVER=smtp
MAIL_HOST=smtp.elasticemail.com
MAIL_PORT=2525
MAIL_USERNAME=elasticmail_username
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=no-reply@domain
MAIL_FROM_NAME=Laravel

Can anyone help me on this?

Regards, Feroz.

0 likes
20 replies
bobbybouwmann's avatar

So if this is not working the only thing that can be wrong are the credentials. I've read that elasticemail sometimes requires you to use the api key as the username, depending on how you registered. You could give that a try?

Otherwise ask support for the correct credentials ;)

gouseferoz's avatar

@BOBBYBOUWMANN - i tried the above method but it didnt work, the API key and the password are the same in the elasticemail settings.

gouseferoz's avatar

I used the same credentials in local environment and it was working fine without any issue.

The problem is with the production environment

gouseferoz's avatar

i tried using some elastic mail wrappers too, but they are working in local but not in production. Can someone please help me here?

bobbybouwmann's avatar

You can see the logs in storage/logs. If there is nothing you might even your your php-fpm logs

gouseferoz's avatar

Looks like my .env is caching. The changes i am making to this file is not reflecting.

I tried to clear the cache using

php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan config:cache
php artisan route:cache
php artisan optimize

then i am getting this error

   LogicException  : Unable to prepare route [api/user] for serialization. Uses Closure.

  at /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php:917
    913|      */
    914|     public function prepareForSerialization()
    915|     {
    916|         if ($this->action['uses'] instanceof Closure) {
  > 917|             throw new LogicException("Unable to prepare route [{$this->uri}] for serialization. Uses Closure.");
    918|         }
    919|
    920|         $this->compileRoute();
    921|

  Exception trace:

  1   Illuminate\Routing\Route::prepareForSerialization()
      /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteCacheCommand.php:62

  2   Illuminate\Foundation\Console\RouteCacheCommand::handle()
      /var/www/laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

  Please use the argument -v to see more details.

How to solve this issue?

Snapey's avatar

You can ignore that error. Its when you try to cache the routes, and the default routes/api.php contains a closure.

If you don't have any API routes, just clear that file out

Snapey's avatar

To check your mail config, open tinker and type

>>> config('mail')

check the values there are what you expect

Snapey's avatar

If you have # in your mail provider API key or password, make sure you put quotes around the string

gouseferoz's avatar

@SNAPEY - i did config('mail') and it matches what I intend to. And also checked the quotes around password.

The issue here is, when i trigger a mail, it is sending to mailtrap which i used for testing purpose but now i have replaced it with elastic mail credentials, but the mail is being received in mailtrap.

Snapey's avatar

Well tinker shows you exactly what the application is using, after caching, and after the .env data is imported into the config/mail.php file.

It does not make sense that "Host" could be elasticmail but the mail turn up at mailtrap

gouseferoz's avatar

i will hard code the credentials into the mail.php file and see if that works.

siangboon's avatar

Email not working can have more than dozen of reasons, it's better to check the log first, either laravel or the server log may helpful to identify the root cause.

By the way, is the elasticemail_username using full email address?

In this case, i would first simply telnet to smtp port from the server and test out the credential with the server and send a message through smtp protocol.

gouseferoz's avatar

@SIANGBOON - I have checked the telnet and its working fine, I was able to send out a test mail.

After hardcoding the credentials, i was able to send the mails. I have to monitor these for a while. Dont know what might have caused the issue.

GuntarV's avatar

@gouseferoz did you get the elastic working OK? I am running into similar issues,

[2020-12-04 19:39:44] production.ERROR: Expected response code 250 but got code "550", with message "550 5.7.1 Relaying denied
" {"exception":"[object] (Swift_TransportException(code: 550): Expected response code 250 but got code \"550\", with message \"550 5.7.1 Relaying denied
\" at 
Snapey's avatar

hardcoding the email credentials in mail.php should give exactly the same outcome as you see in Tinker?

siangboon's avatar

@GOUSEFEROZ - reverse back the steps you changed one by one and dd() out the credentials of each point before it hit to find out the real cause of the issue.

GuntarV's avatar

I came across this post: https://stackoverflow.com/a/49819941 I am on cloudways as well, it is weird, but the API key works in place of user name and password. Since I signed up for elastic email through cloudways, all what cloudways provided was API key, no other instructions. Still not sure is that how it suppose to be.

1 like

Please or to participate in this conversation.