0
0
Laravelframework~5 mins

Mail configuration in Laravel

Choose your learning style9 modes available
Introduction

Mail configuration sets up how your Laravel app sends emails. It tells Laravel where and how to send messages.

You want to send a welcome email after user registration.
You need to send password reset links to users.
You want to notify admins when a new order is placed.
You want to send newsletters or promotional emails.
You want to test email sending during development.
Syntax
Laravel
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=example@example.com
MAIL_FROM_NAME="Example App"
This configuration goes into the .env file in your Laravel project root.
You can change MAIL_MAILER to other drivers like sendmail or log depending on your needs.
Examples
Example using Gmail SMTP to send emails securely with TLS encryption.
Laravel
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email@gmail.com
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_email@gmail.com
MAIL_FROM_NAME="Your Name"
Example using the log driver to write emails to the log file instead of sending them. Useful for development.
Laravel
MAIL_MAILER=log
MAIL_FROM_ADDRESS=example@example.com
MAIL_FROM_NAME="Example App"
Example using the sendmail driver to send emails via the server's sendmail program.
Laravel
MAIL_MAILER=sendmail
MAIL_FROM_ADDRESS=example@example.com
MAIL_FROM_NAME="Example App"
Sample Program

This example shows how to send a welcome email using Mail::send() with the configured mail settings. When you visit /send-welcome, it sends the email and returns a confirmation message.

Laravel
<?php

use Illuminate\Support\Facades\Mail;

// In a controller or route closure
Route::get('/send-welcome', function () {
    Mail::send('emails.welcome', [], function ($message) {
        $message->from(config('mail.from.address'), config('mail.from.name'))
                ->to('user@example.com')
                ->subject('Welcome to Our App');
    });
    return 'Welcome email sent!';
});
OutputSuccess
Important Notes

Always keep your mail credentials safe and do not commit them to public repositories.

Use environment variables in .env to easily switch mail providers without changing code.

Test your mail configuration locally using services like Mailtrap before going live.

Summary

Mail configuration tells Laravel how to send emails.

Set mail settings in the .env file for flexibility.

Use different mail drivers for development and production.