How to Send an Email in Laravel - TechvBlogs

How to Send an Email in Laravel

Email is one of the most used tools for communication in web applications because it helps you reach your users directly and build your brand.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

2 years ago

TechvBlogs - Google News

Laravel is a PHP framework that stands out for its simplicity, modernity, and connectivity. Probably, those characteristics make it especially popular among startups.

Laravel is widely used for building websites, marketplaces, web apps, and even frameworks.

Laravel uses the free feature-rich library SwiftMailer to send emails. Using the library function, we can easily send emails without too many hassles. The e-mail templates are loaded in the same way as views, which means you can use the Blade syntax and inject data into your templates.

We are using Mailtrap for Sending an Email.

Mailtrap is a "fake SMTP server" used for development purposes. Instead of having to test your code with your own email account, and potentially flooding your inbox with test emails, you can instead use Mailtrap as the endpoint.

In this blog, I will give you step-by-step instructions to send emails in Laravel 8. you can create a blade file design and also dynamic information for mail layout. so let's see step by step guide and send an email to your requirement.

Step 1: Create Laravel Project

First, open Terminal and run the following command to create a fresh Laravel project:

composer create-project --prefer-dist laravel/laravel laramail

or, if you have installed the Laravel Installer as a global composer dependency:

laravel new laramail

Step 2: Setup .env File

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=<USERNAME>
MAIL_PASSWORD=<PASSWORD>
MAIL_ENCRYPTION=tls

How to Send an Email in Laravel - TechvBlogs

Read Also: How to send an email with Node.js

Step 3: Create Mailable

In this step, we will create a mailable class SendTestMail for sending an email. For Create Mailable class run the following command:

php artisan make:mail SendTestMail

app/Mail/SendTestMail.php

<?php

namespace App\Mail;

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

class SendTestMail extends Mailable
{
    use Queueable, SerializesModels;

    public $data;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(array $data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Send Test Mail From Laravel')
            ->view('mails.testMail');
    }
}

Step 4: Create View File

In this step, we will create a view file and write an email that we want to send to the user. now we just write some dummy text. create blade files In the mails folder.

resources/views/mails/testMail.blade.php

<!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>How to Send an Email in Laravel - TechvBlogs</title>
</head>
<body>
    <h1><a target="_blank" href="https://techvblogs.com/?ref=mailtrap">TechvBlogs</a></h1>

    <h3>User Detail:</h3>

    <h4>Name: {{ $data['name'] }}</h4>
    <h4>Email: {{ $data['email'] }}</h4>
</body>
</html>

Read Also: SPA Authentication using Laravel Sanctum and Vue.js

Step 5: Add Route

Let's create a web route for send testing an email

Route::get('/send-mail',function(){

    $data = [
        'name'=>'TechvBlogs',
        'email'=>'[email protected]'
    ];

    \Mail::to('[email protected]')->send(new \App\Mail\SendTestMail($data));

    return "Mail Sent Successfully!!";
});

Now, it's time to run our project.

php artisan serve

Open localhost:<PORT NUMBER>/send-mail in the browser.

How to Send an Email in Laravel - TechvBlogs

Thank you for reading this blog.

Read Also: Firebase Push Notification Laravel Tutorial

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

 
 

Comments (0)

Comment


Note: All Input Fields are required.