SMTP Relay in Laravel and other PHP frameworks: the .env settings and a first test e-mail

PHP & Development·· 10 views· 3 min read

The short answer: in Laravel, SMTP Relay goes in the .env file, without touching the code: server, port 587, the credential username and password, and a sender on the verified domain. Clear the config cache, send a test e-mail from tinker and check it in the Email Center log. In other frameworks the idea is the same; only the variable names change.

The Laravel .env

MAIL_MAILER=smtp
MAIL_HOST=SMTP_HOST
MAIL_PORT=587
MAIL_ENCRYPTION=tls
MAIL_USERNAME=SMTP_USER
MAIL_PASSWORD=SMTP_PASSWORD
MAIL_FROM_ADDRESS=no-reply@asuaempresa.co.mz
MAIL_FROM_NAME="${APP_NAME}"

Replace SMTP_HOST with the server in the Connection card, and SMTP_USER and SMTP_PASSWORD with the credential details. In recent Laravel versions the MAIL_ENCRYPTION line is no longer read: on port 587 STARTTLS is negotiated automatically, and for port 465 you write MAIL_SCHEME=smtps.

The first test e-mail

php artisan config:clear
php artisan tinker
>>> Mail::raw('SMTP Relay test', fn ($m) => $m->to('you@example.com')->subject('Test'));

If there is no error, open the Email Center log: the e-mail must be there. If there is one, the message nearly always says whether it is the connection, TLS, login or sender: see the common problems.

Other frameworks

Framework Where it is set
Symfony In the MAILER_DSN variable, in the form smtp://USERNAME:PASSWORD@SMTP_HOST:587. If the password has special characters, URL-encode them.
CodeIgniter, CakePHP and others In the e-mail configuration file: SMTP protocol, server, port 587, TLS encryption, username and password.
Plain PHP With PHPMailer: see sending with PHPMailer. Why not the mail() function: mail() or SMTP.
The .env file must not be reachable. It belongs outside the public folder of the site and outside the repository. A .env that opens in a browser hands the credential password to anyone: if that happened, see leaked password.
Send through a queue. A page that waits for the SMTP server before answering feels slow. In Laravel, use Mail::queue() or classes with ShouldQueue, and keep a php artisan queue:work process running.

Application still not sending? Open a ticket with the error message from the application log, without the password.

Open a support ticket

SEE ALSO

Sending e-mail from your own code with PHPMailer

Sending e-mail from PHP: mail() or SMTP

Sending through SMTP Relay from Node.js and Python: Nodemailer and smtplib, with STARTTLS

SMTP Relay in any program: what Host, Port, Encryption, Username and From mean on each screen

SMTP Relay not sending: connection, TLS, login, sender and recipient problems, one by one

Using SMTP Relay and the e-mail API together: the same domains, log and blocked list

RECOMMENDED PRODUCT

Professional e-mail on your domain

Mailboxes in your company name, no adverts, with spam filtering. from $5.36/mo (3-year plan, with coupon)

See plans
Was this answer helpful? Your answer helps us improve this article.