How to send email in Laravel 8 downwards using Gmail
Tags: php, laravel, webdev, beginners
Published: 2020-10-09 20:54:42

Hello guys, today we are going to be talking about sending an email with our laravel 8 app, this is also applicable to laravel 6 and 7 apps. In this tutorial, we are going to be using an existing app from my previous article [Laravel 8 Auth (Registration and Login)](https://dev.to/kingsconsult/laravel-8-auth-registration-and-login-32jl), you can use an existing app, in that case, just skip Step 1.
Click on my [profile](https://dev.to/kingsconsult) to follow me to get more updates.
### **Step 1: Setup the app**
1. git clone https://github.com/Kingsconsult/laravel_8_auth.git
2. cd laravel_8_auth/
3. composer install
4. npm install
5. cp .env.example .env
6. php artisan key:generate
7. Add your database config in the .env file (you can check my articles on how to achieve that)
8. php artisan migrate
9. php artisan serve (if the server opens up, http://127.0.0.1:8000,  then we are good to go)
![localhost](https://res.cloudinary.com/kingsconsult/image/upload/v1600705305/laravel%208%20modal/4_pp7r76.png)
10. Navigate to http://127.0.0.1:8000/projects

### **Step 2: Set up our email configuration in .env file**
![.env file](https://res.cloudinary.com/kingsconsult/image/upload/v1602271713/laravel%208%20gmail/2_wd9nwz.png)
### **Step 3: Create our Gmail class**
> php artisan make:mail Gmail

![make mail](https://res.cloudinary.com/kingsconsult/image/upload/v1602271713/laravel%208%20gmail/1_c30tgq.png)
This will create a folder in the app directory call Mail that will contain a Gmail.php file
### **Step 4: Write our mailables**
Mailables are the class that defines how our mail is delivered and also the properties of the mail been delivered.
Go to **app/Mail/Gmail.php** and configure as follows
1. We will need to pass some details to the view, in order to achieve that we need to define a public property in our mailable class, so we include the code below
> public $details;

2. We need to pass data into our mailable class' constructor and set that data to public properties defined in the class
> $this->details = $details;

3. We need to configure the view, this is done in the build method of our mailable class, so we have some set of mail properties (methods) we can use eg. subject, view, from, with, attach, attachFromStorage, attachData, etc. the name of the methods explains what they are used for.
We are going to be using only **subject**, **view**, **from** in our case, but feel free to experiment with others, our **Gmail.com** now looks like this
```
<?php

namespace App\Mail;

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

class Gmail extends Mailable
{
    use Queueable, SerializesModels;

    public $details;


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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Mail from Laravel 8 Gmail')
            ->view('emails.gmail')
            ->from('Kingsconsult001@gmail.com');
    }
}
```
### **Step 5: Create our gmail view**
In the **build** method of our mailable class, the view method, we specify that the mailable class should use **gmail** blade file found in the **emails** folder in **resources**, so we need to create that and write our view. So create a folder called **emails** in the resources directory and then create a file called **gmail.blade.php** inside.
In our gmail.blade.php, we use a typical HTML page, in the body, we are going to have a title (**h1** tag) and the body (**p** tag) of for our mail, you can also add a signature that will appear in all your mail, in our case, I just add **Thank you**.
```
<!DOCTYPE html>
<html>
<head>
    <title>kings</title>
</head>
<body>
    <h1>{{ $details['title'] }}</h1>
    <p>{{ $details['body'] }}</p>

    <p>Thank you</p>
</body>
</html>
```
### **Step 6: Send the mail**
Go to anywhere you want to send the mail from and call the Gmail class. In our case, we want to send a mail after the user have successfully registered so, we are going to edit our **CreateNewUser** class in **App/Actions/Fortify/CreateNewUser.php**. 
Add Mail facade **use Illuminate\Support\Facades\Mail;** and the path of the Gmail class at the top, **use App\Mail\Gmail;**.
Create an instance of Mail class that will send mail using an instance of Gmail which will contain the details of our mail.
![CreateNewUser file](https://res.cloudinary.com/kingsconsult/image/upload/v1602274361/laravel%208%20gmail/3_u3yfrg.png)
start our app with **php artisan serve**, and click on register at the top-right corner
![localhost](https://res.cloudinary.com/kingsconsult/image/upload/v1602275187/laravel%208%20gmail/4_fiftn6.png)
**fill your information**
![registration page](https://res.cloudinary.com/kingsconsult/image/upload/v1600318471/10_rqgzrc.png)
**my inbox**
![inbox](https://res.cloudinary.com/kingsconsult/image/upload/v1602275590/laravel%208%20gmail/5_rfhvle.png)

You can get the complete code from the [repo](https://github.com/Kingsconsult/laravel_8_gmail).
Follow me for more of my articles, you can leave comments, suggestions, and reactions

 [click the link to view my profile and follow me] (https://dev.to/kingsconsult)

Visit my other posts 
{% link https://dev.to/kingsconsult/laravel-8-crud-bi9 %}
{% link https://dev.to/kingsconsult/how-to-upload-data-with-excel-sheet-in-laravel-8-and-laravel-7-3515 %}