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
- git clone https://github.com/Kingsconsult/laravel_8_auth.git
- cd laravel_8_auth/
- composer install
- npm install
- cp .env.example .env
- php artisan key:generate
- Add your database config in the .env file (you can check my articles on how to achieve that)
- php artisan migrate
- php artisan serve (if the server opens up, http://127.0.0.1:8000, then we are good to go)
- Navigate to http://127.0.0.1:8000/projects
Step 2: Set up our email configuration in .env file
Step 3: Create our Gmail class
php artisan make:mail Gmail
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
-
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;
-
We need to pass data into our mailable class' constructor and set that data to public properties defined in the class
$this->details = $details;
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.
start our app with php artisan serve, and click on register at the top-right corner
fill your information
my 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
Top comments (11)
Hi, it's possible to get mails from gmail?
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.
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
Do you mean, if you can be viewing our gmail inbox mail on your Laravel app?
No, my GMAIL inbox.
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? :)
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
Thanks a lot for the article!
It really helps me.
But how about css stylesheets applied on the mail template?
I've designed my own with Bootstrap, and the style css is called from the CDN but on Gmail, there's no style.
Please help me
Excellent article. This was very thorough and immensely helpful. Much appreciated!
I need to send a link to email to different users to reset password, will they have any other article?
I tried with this article but my code stops and does not send the mail.
its a great article , i have a question
how can i appear the image in the mail box beside the sender email