Send Laravel mail: use Sendgrid key – dynamic key – inline image

Tram Ho

Hello bro. This article I would like to introduce a few interesting things with sending mail in laravel.
The content is in this article

  1. Register for a sendgrid account and create a free 30-day sendgrid with 100 emails / day
  2. Send mail using sendgrid key
  3. Send mail with dynamic key sendgrid
  4. Email with inline image (inline image)
    Let’s start !

Registering the sendgrid account creates the sendgrid key

Create an email account

  1. You need to visit SendGrid to register an account. You need to enter the registration account information and continue to enter other information again, please enter correctly because the sendgrid very carefully checked, if the information is dishonest, it will not be possible to register.
  2. When you register successfully, a confirmation email will be sent to the email you just registered.

    Confirm email to complete.

Create sendgrid key

The Sendgrid API Key is an authentication string instead of your mailing account. On the Dashboard page of Sendgrid, select API keys -> in the right corner of the screen, select Create API key. Click on the General API key. The key chain is generated and you need to save it for use.

Send mail using sendgrid key

To demo this section, I created a laravel project

  1. Create project laravel composer create-project --prefer-dist laravel/laravel sendmail
  2. Create .env file from .env.exemple
  3. Create controller to handle sending mail php artisan make:controller MailController
  4. Add route in web.php file Route::get('/send', ' [email protected] ');
  5. Write the function sendMail () in MailController
    At this point, run the server and test call route to see the sendMail function, ok, then I started writing the send mail section.
    Send Mail
    First of all we need to install the library https://github.com/sendgrid/sendgrid-php to send mail.
    This library helps us send mail with the sendgrid key that supports inline images and even uses the dynamic sendgrid key => 1 in 3, it’s so good.
  • First you need to install by command via Composer.
    sudo composer require sendgrid/sendgrid
    After running the command, check the sendgrid version in the composer.json file. After installing the library, we need to write the send code tomorrow at the sendMail function.

Explain the code:

  • $ emailFrom, $ nameFrom: This is the name the email will be displayed in the email sent.
  • $ subject: email subject
  • $ emailTo, $ nameTo: email and recipient name
  • $ template: email content sent here I am to format text / html is able to send text and html tags will also display
  • $ key: the sendgrid key I registered above.
    So sent mail already.

Send mail with dynamic key sendgrid

About sending dynamic mail key. We just need to change the value of the variable $ key, then the generated $ email will depend on that key, unlike other mailing methods, we will have to reset the key in config.

Email with inline image (inline image)

The library on https://github.com/sendgrid/sendgrid-php has built inline image for us to use CID. The inline image will help the sending images can be more suitable for more browsers. differs initially due to the use of CID.
1 image has been sent with the link as below

Thank you all for watching, in the next part, I will write more about some small techniques for sending mail in laravel, and follow along.

Share the news now

Source : Viblo