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.
Practice
(1/5)
1. What is the main purpose of the .env file in Laravel mail configuration?
easy
A. To define routes for sending emails
B. To write the email content directly
C. To create email templates
D. To store mail settings like driver, host, and port for easy changes
Solution
Step 1: Understand the role of .env file
The .env file holds environment-specific settings, including mail configuration, so you can change them without altering code.
Step 2: Identify mail settings stored in .env
Mail driver, host, port, username, and password are set here for flexibility across environments.
Final Answer:
To store mail settings like driver, host, and port for easy changes -> Option D
Quick Check:
.env stores mail settings = C [OK]
Hint: Remember: .env holds settings, not email content [OK]
Common Mistakes:
Thinking email content is set in .env
Confusing routes with mail settings
Trying to define templates in .env
2. Which of the following is the correct way to set the mail driver in Laravel's .env file?
easy
A. MAIL_DRIVER: smtp
B. mail_driver = smtp
C. MAIL_DRIVER=smtp
D. mailDriver = smtp
Solution
Step 1: Recall .env syntax
Environment variables use uppercase keys with equals sign, no spaces: KEY=VALUE.