Send Email In Laravel

Websolutionstuff | Sep-02-2020 | Categories : Laravel PHP

In this article, we will explore the process of sending emails in Laravel, covering versions 6, 7, 8, 9, and 10. Email functionality is a fundamental and essential feature in web development, and it plays a crucial role in communication with clients and users.

Laravel simplifies email handling with its clean and straightforward email API, which is driven by the widely used Symfony Mailer component. Regardless of the version of Laravel you're working with, you can configure email services through your application's configuration file, typically located at config/mail.php.

Whether you're using Laravel 6, 7, 8, 9, or 10, this article will provide you with the knowledge and guidance you need to effectively send emails within your Laravel applications.

Laravel 6/7/8/9/10 Send Mail Example

  • Setup Mail Configuration
  • Create Mail
  • Create Blade File
  • Create Route
  • Create Controller

 

 

Step 1: Setup Mail Configuration

First of all, we need to set the configuration in the .env file for sending mail, here we use SMTP. So, set the configuration as below.

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=TLS

 

Step 2: Create Mail Using Artisan Command

Laravel provides an inbuilt mail class for sending mail. So, we will create testmail class for the same.

php artisan make:mail TestMail

Now you can find the file in this location app/Mail/TestMail.php

Add the below code for viewing.

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class TestMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('mail.Test_mail');
    }
}

 

 

Step 3: Create Blade File

Now, I have created a Test_mail.blade.php file and we will add some text dummy text for email test purposes.

view/mail/Test_mail.blade.php

Hi <br/>
This is Test Mail From Websolutionstuff. <br />
Thank you !!

 

Step 4: Create Route

Now, create a route for testing and send mail.

Route::get('test_mail','UserController@testMail');

 

Step 5: Create Controller

 Now, we will create UserController and add the below code.

public function testMail()
{
	$mail = 'websolutionstuff @gmail.com';
	Mail::to($mail)->send(new TestMail);

	dd('Mail Send Successfully !!');
}

 

And after that, you get output like the below screen print.

send_email_in_laravel

 

Conclusion:

In conclusion, we've learned how to send emails in Laravel, covering versions 6, 7, 8, 9, and 10. Email is a vital part of web development, and Laravel offers a user-friendly way to handle it.

Now, armed with this knowledge, I can seamlessly integrate email functionality into my Laravel applications, ensuring effective communication with clients and users.

 


You might also like:

Recommended Post
Featured Post
Laravel 9 User Roles and Permissions Without Package
Laravel 9 User Roles and Permi...

In this article, we will see laravel 9 user roles and permissions without package. Roles and permissions are an imp...

Read More

Apr-14-2022

Jquery Append And Prepend Example
Jquery Append And Prepend Exam...

In this article I will give you example of jquery append() and prepend() method. The append() method inserts t...

Read More

Dec-06-2021

How To Use Google Recaptcha V2 In Laravel 9
How To Use Google Recaptcha V2...

In this article, we will see how to use google ReCaptcha v2 in laravel 9. Google ReCaptcha is used for advance...

Read More

Jun-14-2022

How To Use OpenAI In Laravel 8/9
How To Use OpenAI In Laravel 8...

In this article, we will explore the integration of OpenAI into Laravel versions 8, 9, and 10. Our focus will be on unde...

Read More

Feb-06-2023