Laravel Mail

We will be making configuration for sending email and how does it work and How to make it working properly?

Laravel Mail is a feature for sending mails to the user. It is a feature that is built in laravel. It is easy to use and configure. It is also easy to customize.

Configuration

.env

In the .env file we need to configure the mail driver, host, port, username, password, encryption, and sender email address.

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=yourusername
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls

this is just a env file configuration. We need to configure the mail.php file in config folder.

config/mail.php it is the file where we can configure the mail driver, host, port, username, password, encryption, and sender email address.

Now what will be doing is creating and writing in Mailables which is available in app/Mail directory.

We can create by using command

php artisan make:mail mailableName

the name of the mailable can be anything name

Once it has been created, there are different methods like

Sender configuration

use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Envelope;
 
/**
* Get the message envelope.
*
* @return \Illuminate\Mail\Mailables\Envelope
*/
public function envelope()
{
   return new Envelope(
       from: new Address('example@example.com', 'Test Sender'),
       subject: 'Test Email',
   );
}

we can configure sender in config/mail.php which is almost similar to coming from env i mean the data are literally coming from env and if not available we can change it

'from' => ['address' => 'example@example.com', 'name' => 'App Name']

like this.