Instructions on how to send mail in Laravel through a mini project

Tram Ho

Hello everyone, after a long time of absence, I have come back. On the occasion that I am working on a project at the school and including the email function, in this article I will guide you to create the email sending function using the Laravel application.

My idea is to create a mini project with basic functions. For each function we will email the user with information about that action.

1. Create a Laravel project

To perform the basic function, it is imperative for us to have a laravel project, right? =)) Here I use the laravel command to create the project.

2. Migration

After successfully initializing the project, we will create a tasks table in the database (Database) using migration

After creating the migration, we will get a migration file like this. Our job now is to add the columns we want to add to the function up() as shown below:

Here, I just need the id and name columns for this table.

3. Model

Similar to the model, after running the above command, the file will appear as below.

Unnecessary attributes, I will not write in because in this case for me, just fillable is enough ?

4. Routing

Before creating the route, we have to create the controller first.

Once we have the controller, our next task is to initialize the route ?

5. Create a CRUD function

At this stage, I will divide it into 2 parts:

  • Creating Functions
  • Create Views
Functions

We have a TaskController consisting of the following functions:

View

We will create 2 views. It is app.blade.php to contain the layout of the interface and tasks.blade.php containing the new data table and form.

Then, the complete interface we get will be:

The interface is a bit stupid, please ignore it =)

6. Send Email

Now comes our main function of emailing.

B1. Config file .env

Because I use Gmail to send, I’ll configure it according to Gmail. You find the content and correct as below:

When using Gmail:

  • MAIL_DRIVER (mail delivery method) is smtp.
  • MAIL_HOST (host) of Gmail is smtp.gmail.com .
  • MAIL_PORT (port connected to Gmail) is 587.
  • MAIL_USERNAME is your own gmail.
  • MAIL_PASSWORD is your gmail password.

When using a password, you need to pay attention to a few things:

If your Gmail has two-factor Authentication turned on, then you need to create a one-time gmail application password to be able to use it, if you enter the gmail password, it cannot be used in the field. this match. I don’t encourage you to drop this advanced security layer. (At the end of this article, I will show you how to get an application password of gmail)

So, the config steps have been completed.

B2. Treatment

I will email you when I create or delete a task.

B2.1. Create a job

First I will create a job to use to get the job every time the controller prepares to return to the result. Here is the job email:

After running the above command, I will have a file that is app/Jobs/SendEmail.php

Inside this file will be tasked with receiving information including $message và $users where $message is the message and $users is the range of users that will send from the controller. Next is to perform the job of sending email to users with the content of the email will be received again when calling Notification .

B2.2. Create Mailable

B2.3. Create template for mail

I will use a blade.php file to create the interface for the email I will send

You can customize this template yourself or you can use it as it is here =)))

B2.4. Get data from the Controller

In function store() of Controller, I will fix as follows:

Here I add two variables, $users and $message to retrieve all users in the database and create messages for actions, and then send both into the SendEmail job to send mail. The same goes for function delete()

B3. Run the project

The work is done and now let’s run the program together 1 time. And this is my accomplishment.

Instructions for obtaining Gmail application passwords

As I said above, if you use Gmail’s 2-layer security, you need to have an application password to use this emailing function. So here I will guide you to get the password of gmail application.

First, you access the Google account management page here . After accessing and logging in, you will see the following screen:

In the left column, select Security .

Here, you can see the Sign in to Google section with 2-Step Verification turned on, which means your account is protected with advanced security.

Next you choose the Application Password , this time Google will ask you to enter the password again. After entering the password again, you will see this screen.

You will now select Apps and Devices , then click Create .

After Generating, you will receive a string of 16 characters, which is your application password . You use this password to paste in the MAIL_PASSWORD section of the project above.

summary

That’s it, I have just guided you through the function of sending email through using Job, Mailable of Laravel with a small project. I still got a little bit skewed, so I hope you guys ignore it. =))) If you succeed, do not regret giving me an upvote ? (kamsamita)

Reference: https://laravel.com/docs/6.x/mail

Good luck !
Share the news now

Source : Viblo