How to Send an Email in Laravel

How to Send an Email in Laravel

In this post we will give you information about How to Send an Email in Laravel. Hear we will give you detail about How to Send an Email in Laravel And how to use it also give you demo for it if it is necessary.

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 10. 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

Also see: 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 AppMail;

use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;

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 - onlinecode</title>
</head>
<body>
    <h1><a target="_blank" href="https://onlinecode.org/?ref=mailtrap">onlinecode</a></h1>

    <h3>User Detail:</h3>

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

Also see: 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'=>'onlinecode',
        'email'=>'[email protected]'
    ];

    Mail::to('[email protected]')->send(new AppMailSendTestMail($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.

Thank you for reading this blog.

Also see: 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.

 
 

Hope this code and post will helped you for implement How to Send an Email in Laravel. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US