Tags

How to fix "Cannot send message without a sender address" - Laravel

Miguel Lima By Miguel Lima
Published on

A lots of time when we start a new laravel application we forget to setup the mail driver. And then, when we try to send an email it will fail.

When mail driver it is not configured, a "Swift_TransportException" will be thrown.

To fix it, we just need to configure a mail driver. Laravel offers a variety of drivers options like "smtp", "sendmail", "mailgun", "ses", "postmark", "log", "array".

Configure Mail Driver

Today we will learn how to configure mail driver using mailtrap.

Mailtrap is a self email testing for staging and development, where you can inspect and debug your email samples before delivering them to your customers.

So open your .env file. Here you will see the default configuration variables:

.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

You need to have an account on mailtrap.io.

On mailtrap go to you "inbox > settings", there you can see your "Credentials", copy the username and password under "STMP".

Paste the username on MAIL_USERNAME, and the password on MAIL_PASSWORD. Finally you just need to choose a "from email adress".

Our final .env variables:

.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=someUsername
MAIL_PASSWORD=yourPassword
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=no-reply@myemail.com
MAIL_FROM_NAME="${APP_NAME}"

Learn something from this article?

We would love to share more content like this with you!

    We won't send you spam. Unsubscribe at any time.