Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 27, 2022 09:35 am GMT

How to send email in laravel 9 ?

Send MAIL In Laravel 9

Today I am going to explain how you can send MAIL using SMTP in Laravel 9.

Laravel UI Installation.

After Installing Laravel 9 we will open the code in Editor.

Step - 1

Open .env file and change the MAIL Provider SMTP Details

MAIL_MAILER=smtpMAIL_HOST=mailhogMAIL_PORT=1025MAIL_USERNAME=nullMAIL_PASSWORD=nullMAIL_ENCRYPTION=null

You can use MailTrap to generate basic SMTP Details and test your email faeture.

Step - 2

Now Generate the email class by running command

php artisan make:mail TestEmail

this command will generate the file in app/Mail/TestEmail.php

Open the file and update the code below.

<?phpnamespace App\Mail;use Illuminate\Bus\Queueable;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Mail\Mailable;use Illuminate\Queue\SerializesModels;class TestEmail extends Mailable{    use Queueable, SerializesModels;    public $mailData;    /**     * Create a new message instance.     *     * @return void     */    public function __construct($mailData)    {        $this->mailData = $mailData;    }    /**     * Build the message.     *     * @return $this     */    public function build()    {        return $this->subject('Test Email')->view('email.test');    }}

Step - 3

Now Let's create a blade view file, for that let's create email folder first inside resources/view.

Now create a test.blade.php file inside email folder and paste the code below.

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>Test Email</title></head><body>    <h1>Test EMAIL</h1>    <p>Name: {{ $mailData['name'] }}</p>    <p>DOB: {{ $mailData['dob'] }}</p></body></html>

Step - 4

Now create a route to send email put the code below to routes/web.php

use App\Mail\TestEmail;use Illuminate\Support\Facades\Mail;Route::get('send-email', function(){    $mailData = [        "name" => "Test NAME",        "dob" => "12/12/1990"    ];    Mail::to("[email protected]")->send(new TestEmail($mailData));    dd("Mail Sent Successfully!");});

Now visit to /send-email in browser it should send the email to inbox.

The Email Will look like.

EMAIl INBOX

The complete Tutorial is below in the video.

If you face any issue while installing, please comment your query.

Thank You for Reading

Reach Out To me.
Twitter
Instagram
TechToolIndia YouTube Channel


Original Link: https://dev.to/shanisingh03/how-to-send-email-in-laravel-9--13db

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To