Sending email — for account verification, notifications, or password resets — is a must-have feature in many applications. Laravel makes it easy through the Mailable class. This article walks you through sending email using Gmail SMTP from scratch.
Step 1: Set Up a Gmail App Password
Gmail no longer allows SMTP login with your regular password. You need an App Password:
- Enable 2-step verification on your Google account.
- Go to Google Account → Security → App passwords.
- Create a new app password, then copy the 16 characters that appear.
Step 2: Configure .env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=your_16_character_app_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=youremail@gmail.com
MAIL_FROM_NAME="Application Name"
After editing .env, run php artisan config:clear.
Step 3: Create a Mailable Class
php artisan make:mail WelcomeMail
Set the email content in app/Mail/WelcomeMail.php:
public function build()
{
return $this->subject('Welcome!')
->view('emails.welcome');
}
Step 4: Create the Email View
Create the file resources/views/emails/welcome.blade.php:
<h1>Hello, {{ $name }}!</h1>
<p>Thank you for signing up for our application.</p>
Step 5: Send the Email
use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeMail;
Mail::to('recipient@email.com')->send(new WelcomeMail());
Tip: Send Email Through a Queue
Sending email can be slow and hold up the response. It's better to run it through a queue so the page stays responsive:
Mail::to($user)->queue(new WelcomeMail());
Fixing Common Errors
- "Failed to authenticate": You're using your regular password instead of an App Password. Use an App Password.
- Connection timeout: wrong port/encryption — use port
587withtls, or465withssl. - Emails landing in spam: for serious production use, consider a service like Mailgun or Amazon SES with a verified domain.
Conclusion
Sending email in Laravel is straightforward: create a Gmail App Password, configure .env, build a Mailable plus a view, then send it with Mail::to(). For better performance, use a queue. Gmail is fine for development and low volume; for larger scale, switch to a dedicated email service.