Laravel 11 tutorial #64 Send Email | laravel send mail with smtp
Table of Contents
Introduction
This tutorial will guide you through the process of sending emails using SMTP and Gmail in Laravel 11. By the end of this guide, you'll have a working email configuration in your Laravel application and be able to send test emails.
Step 1: Configure Mail Settings in the .env File
To start sending emails, you need to configure your mail settings in the .env
file.
- Open your
.env
file located in the root of your Laravel project. - Update the following lines with your Gmail SMTP settings:
MAIL_MAILER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=your_email@gmail.com MAIL_PASSWORD=your_generated_app_password MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=your_email@gmail.com MAIL_FROM_NAME="${APP_NAME}"
- Replace
your_email@gmail.com
with your actual Gmail address. - For
your_generated_app_password
, you need to generate an App Password from your Google account.
Practical Tip
- Ensure that you have "Allow less secure apps" turned on in your Google account settings if you're not using App Passwords.
Step 2: Generate App Password in Gmail
To enhance security, Gmail requires generating an app password for sending emails.
- Go to your Google Account.
- Navigate to the "Security" section.
- Under "Signing in to Google," find "App passwords."
- Select "Generate App Password."
- Choose the app and device you want to generate the password for, then click "Generate."
- Copy the generated password and use it in your
.env
file.
Step 3: Create a Mailable Class
Next, you need to create a mailable class that will handle the email content.
- Run the following Artisan command in your terminal:
php artisan make:mail TestEmail
- This command creates a new file in
app/Mail/TestEmail.php
.
Edit the Mailable Class
- Open
TestEmail.php
and modify thebuild
method:public function build() { return $this->view('emails.test') ->subject('Test Email from Laravel'); }
- Create a new directory called
emails
inside theresources/views
folder. - Inside the
emails
directory, create a new Blade file namedtest.blade.php
.
Example Email Template
Add the following HTML content to test.blade.php
:
<h1>Hello from Laravel!</h1>
<p>This is a test email sent from your Laravel application.</p>
Step 4: Create a Controller for Sending Email
Now, create a controller that will handle the email sending logic.
- Run the following command:
php artisan make:controller MailController
- Open
MailController.php
and add the following code:use App\Mail\TestEmail; use Illuminate\Support\Facades\Mail; public function sendEmail() { Mail::to('recipient@example.com')->send(new TestEmail()); return 'Email sent successfully!'; }
Step 5: Define a Route
To access the email sending functionality, define a route.
- Open
routes/web.php
and add the following line:Route::get('/send-email', [MailController::class, 'sendEmail']);
Step 6: Test Sending the Email
Now that everything is set up, you can test sending an email.
- Start your Laravel server using:
php artisan serve
- Open your web browser and navigate to
http://localhost:8000/send-email
. - You should see a message indicating that the email was sent successfully.
Conclusion
In this tutorial, you learned how to configure SMTP settings for Gmail in Laravel 11, create a mailable class, set up a controller for sending emails, and define routes. By following these steps, you can easily send emails from your Laravel application.
Next steps could include exploring email queueing for better performance or customizing your email templates further.