Hello bro. This article I would like to introduce a few interesting things with sending mail in laravel.
The content is in this article
- Register for a sendgrid account and create a free 30-day sendgrid with 100 emails / day
- Send mail using sendgrid key
- Send mail with dynamic key sendgrid
- Email with inline image (inline image)
Let’s start !
Registering the sendgrid account creates the sendgrid key
Create an email account
- 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.
- 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
- Create project laravel
composer create-project --prefer-dist laravel/laravel sendmail
- Create .env file from .env.exemple
- Create controller to handle sending mail
php artisan make:controller MailController
- Add route in web.php file
Route::get('/send', ' [email protected] ');
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">sendMail</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$emailFrom</span> <span class="token operator">=</span> <span class="token double-quoted-string string">" <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> "</span> <span class="token punctuation">;</span> <span class="token variable">$nameFrom</span> <span class="token operator">=</span> <span class="token double-quoted-string string">"Name abc"</span> <span class="token punctuation">;</span> <span class="token variable">$subject</span> <span class="token operator">=</span> <span class="token double-quoted-string string">"Đây là tiêu đề!"</span> <span class="token punctuation">;</span> <span class="token variable">$emailTo</span> <span class="token operator">=</span> <span class="token double-quoted-string string">" <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> "</span> <span class="token punctuation">;</span> <span class="token variable">$nameTo</span> <span class="token operator">=</span> <span class="token double-quoted-string string">"Nguyễn Thịnh"</span> <span class="token punctuation">;</span> <span class="token variable">$template</span> <span class="token operator">=</span> <span class="token double-quoted-string string">"<h1>hello </h1>"</span> <span class="token punctuation">;</span> <span class="token variable">$key</span> <span class="token operator">=</span> <span class="token double-quoted-string string">"Add your Sendgrid Key here"</span> <span class="token punctuation">;</span> <span class="token variable">$email</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name"> SendGrid Mail Mail</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token variable">$email</span> <span class="token operator">-</span> <span class="token operator">></span> <span class="token function">setFrom</span> <span class="token punctuation">(</span> <span class="token variable">$emailFrom</span> <span class="token punctuation">,</span> <span class="token variable">$nameFrom</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token variable">$email</span> <span class="token operator">-</span> <span class="token operator">></span> <span class="token function">setSubject</span> <span class="token punctuation">(</span> <span class="token variable">$subject</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token variable">$email</span> <span class="token operator">-</span> <span class="token operator">></span> <span class="token function">addTo</span> <span class="token punctuation">(</span> <span class="token variable">$emailTo</span> <span class="token punctuation">,</span> <span class="token variable">$nameTo</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token variable">$email</span> <span class="token operator">-</span> <span class="token operator">></span> <span class="token function">addContent</span> <span class="token punctuation">(</span> <span class="token single-quoted-string string">'text/html'</span> <span class="token punctuation">,</span> <span class="token variable">$template</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token variable">$sendgrid</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name"> SendGrid</span> <span class="token punctuation">(</span> <span class="token variable">$key</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$sendgrid</span> <span class="token operator">-</span> <span class="token operator">></span> <span class="token function">send</span> <span class="token punctuation">(</span> <span class="token variable">$email</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token variable">$response</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> |
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.