Back to the knowledge base
Koppelenlaravelsmtp

Setting the MAIL variables in Laravel

Laravel reads its mail settings from the environment file of your application. With a handful of MAIL variables you let all mail from your application — registrations, password resets, notifications from a queue — leave through 2Mail, without changing a single line of code.

What you need

  • the server name shown in your 2mail account
  • an SMTP login with password, created in the dashboard for this application; use a separate login for your test and production environments
  • a sender address on a domain registered as a sending domain in your account

Step 1: fill in the variables

Open the environment file of your application and set the mailer to SMTP. Filled in, it looks like this:

MAIL_MAILER=smtp, MAIL_HOST=the server name from your 2mail account, MAIL_PORT=587, MAIL_USERNAME=your SMTP login, MAIL_PASSWORD=your password, MAIL_ENCRYPTION=tls, MAIL_FROM_ADDRESS=an address on your sending domain, MAIL_FROM_NAME=your company name.

Put values containing spaces, such as the sender name, in quotes. Keep the encryption on tls; that is the STARTTLS setting for port 587.

Step 2: refresh the configuration cache

If your production environment runs with a cached configuration, Laravel does not re-read the environment file. Refresh the configuration cache after every change; otherwise you test with the old values and hunt for a bug that is not there.

Step 3: test

Send a simple message, for instance from Laravel's interactive console or through an existing notification, to an address outside your domain. Then check the delivery log in the 2Mail dashboard: there you see whether the message reached us and what the receiving server answered.

Step 4: the queue

If you send mail through a queue, the actual sending happens in the worker, not in the web request. Restart the workers after changing the variables; otherwise they carry on with the old settings. When the worker hits an error, the message appears in the failed jobs table, with the reply from our relay attached.

Common mistakes

  • Connection could not be established — the server name has a typo, or the hosting blocks port 587. Ask your host to open that port.
  • Sender rejected — the from address in an individual mailable overrides the global address and uses a different domain.
  • Works locally, not in production — the configuration cache was not refreshed, or production uses a different environment file with old values.

More about this integration on SMTP for Laravel. If you would rather send without SMTP and get a status back per message, have a look at the email API.

Still stuck?

Send us the message from your log file or from the failed jobs table via contact. The reply from our relay is quoted there word for word.

#laravel mail env#set up smtp laravel#laravel mail_host#smtp relay laravel#laravel send mail
Still stuck?

Does it behave differently than described above, or are you stuck anyway? Get in touch with your domain name or customer number at hand and we will take a look with you.

Contact support
Call us
Send an email