implement notification mails in Laravel(SendGridAPI)

Tram Ho

I will be creating a contact form with laravel and notify the contents to the email, so I will share what I learned there. (Via WebAPI)

Get SendGrid APIKey

First, create a SendGrid account.

Issue an API key. The publishing page is difficult to understand, but it is located in Settings in the menu bar on the left side of My Page.

Click “CreateAPIKey” and go to the following screen, name the API key. Here I named it “laravel”. API Key Permissions is set to Restricted because it only sends emails.

If you only want to send mail, Mail Send is all you need. Next up is Create & View.

APIKey should be displayed. Copy it and don’t forget to backup it because it will only be displayed once.

Describe the API key, mail driver (SendGrid in this case), from address, and from name in the env file.

By default, SendGrid cannot be specified as a driver, but some people have created a package, so install it using composer.

If there is a problem, delete the driver.

Create Mailable

Finally the implementation part. Create Mailable class.

php artisan make: mail SendGridSample
A file called App/Mail/SendGridSample is created. Edit this

(Don’t forget to write down the driver’s use that I’m grateful for.)

Create a build method and write the process in it.

Since from is written in the env file, it does not need to be written here.

  • SendGridSample.php

Describe use of SENDGRID_API_KEY of env in service.php file.

  • service.php

Create Mail Template

A simple one that returns your name, email address, and the content of your inquiry.

Edit ContactController

  • ContactController.php

Now you can send the completion email of the inquiry.

Share the news now

Source : Viblo