Trigger sendgrid email template using meteor

I use sendgrid to send email. I want to send an email template to users. Below code just sends a simple text email instead of defining part of the headers and using the template id.

if (Meteor.isServer) { Email.send({ from: " from@mailinator.com ", to: " abc@mail.com ", subject: "Subject", text: "Here is some text", headers: {"X-SMTPAPI": { "filters" : { "template" : { "settings" : { "enable" : 1, "Content-Type" : "text/html", "template_id": "3fca3640-b47a-4f65-8693-1ba705b9e70e" } } } } } }); } 

Your help would be greatly appreciated.

The best

+5
source share
1 answer

You have different options for sending transactional SendGrid templates.

1) Using the SendGrid SMPT API

In this case, we can use the Meteor email package (as you tried).

To add an email package to the meteor, we need to enter the sale:

 meteor add email 

In this case, according to the SendGrid Docs :

The text property is replaced by <% body%> of the text template, and html is replaced by <% body%> of the HTML template. If the text property is present, but not html , then the received email will contain only the text version of the template, and not the HTML version.

So, in your code you also need to provide the http property, that is all.

This could be your server code :

 // Send via the SendGrid SMTP API, using meteor email package Email.send({ from: Meteor.settings.sendgrid.sender_email, to: userEmail, subject: "your template subject here", text: "template plain text here", html: "template body content here", headers: { 'X-SMTPAPI': { "filters": { "templates": { "settings": { "enable": 1, "template_id": 'c040acdc-f938-422a-bf67-044f85f5aa03' } } } } } }); 

2) Using the SendGrid v3 web interface

You can use the meteor http package to use SendGrid Web API v3 ( here docs ). In this case, we can use the Meteor http package.

To add the Meteor http package type to the shell:

 meteor add http 

Then in server code you can use

 // Send via the SendGrid Web API v3, using meteor http package var endpoint, options, result; endpoint = 'https://api.sendgrid.com/v3/mail/send'; options = { headers: { "Authorization": `Bearer ${Meteor.settings.sendgrid.api_key}`, "Content-Type": "application/json" }, data: { personalizations: [ { to: [ { email: userEmail } ], subject: 'the template subject' } ], from: { email: Meteor.settings.sendgrid.sender_email }, content: [ { type: "text/html", value: "your body content here" } ], template_id: 'c040acdc-f938-422a-bf67-044f85f5aa03' } }; result = HTTP.post(endpoint, options); 
+3
source

Source: https://habr.com/ru/post/1273256/


All Articles