loading...

How to send email in Laravel 8 downwards using Gmail

kingsconsult profile image Kingsconsult Updated on ・4 min read

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), you can use an existing app, in that case, just skip Step 1.
Click on my profile 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
  10. Navigate to http://127.0.0.1:8000/projects

Step 2: Set up our email configuration in .env file

.env file

Step 3: Create our Gmail class

php artisan make:mail Gmail

make mail
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');
    }
}
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
start our app with php artisan serve, and click on register at the top-right corner
localhost
fill your information
registration page
my inbox
inbox

You can get the complete code from the repo.
Follow me for more of my articles, you can leave comments, suggestions, and reactions

click the link to view my profile and follow me

Visit my other posts

Discussion

pic
Editor guide
Collapse
lengyelbalint profile image
lengyelbalint

Another great article! :)

Is it possible to add verification link like ResetPassword link to the mail after registration?

I allowed the Features::emailVerification(); line in config/fortify.php, but it did not solve my issue. :/

Can you help? :)

Collapse
kingsconsult profile image
Kingsconsult Author

While will you want to send the link, the link expires 60 minutes after generating, the person should just request forgot password instead.
But you can send virtually anything through the email

Collapse
mafalda2007 profile image
Mafalda

Hi, it's possible to get mails from gmail?

Collapse
kingsconsult profile image
Kingsconsult Author

Sorry, I don't really understand your question, I used Gmail and I was getting my mails from the app to my inbox.
If this does not answer your question, can you rephrase your question?
Thank you.

Collapse
mafalda2007 profile image
Mafalda

Well, I need to know if i can recive mails from GMAIL to my HelpDesk (done in Laravel 8). I can send them from my app, but i don´t know how to get them. Tks

Thread Thread
kingsconsult profile image
Kingsconsult Author

Do you mean, if you can be viewing our gmail inbox mail on your Laravel app?

Thread Thread
mafalda2007 profile image
Mafalda

No, my GMAIL inbox.