Reputation: 65
I would like to send mails via Google Apps Script using Mailjet. The problem is that there is no documentation at all on how to use this API with GAS.
Does any of you know some documentation somewhere about this use of Mailjet, or does any of you know a website to send mails like Sendgrid or Mailjet for which we can find documentation for the use of the API in GAS?
I tried the following code to send a basic email with JetMail but I can't make it work:
var mailjeturl = "https://api.mailjet.com/v3.1/send";
var mailjetparams = {
"Messages":[{
"From": {"Email": '[email protected]',"Name": 'Robert'},
"To": [{"Email": '[email protected]'}],
"Subject": 'subject',
"HTMLPart": 'this message',
}
var mailjetoptions = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(mailjetparams)
};
var response = JSON.parse(UrlFetchApp.fetch(mailjeturl, mailjetoptions))
I actually don't know where to write my keys.
Thank you in advance for your answers,
Clank
Upvotes: 2
Views: 745
Reputation: 304
you have to do some modifications.
I've put the right username and password of mailjet and added the noreply domain of my company and it works!
var encoding = Utilities.base64Encode(user + ":" + userPwd);
var mailjetUrl = "https://api.mailjet.com/v3.1/send";
var mailjetParams = {
"Messages":[{
"From": {"Email": '...'},
"To": [{"Email": '...'}],
"Subject": 'subject',
"HTMLPart": 'message'}]
}
var mailjetOptions = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(mailjetParams),
"headers": {"Authorization": "Basic " + encoding},
}
var response = JSON.parse(UrlFetchApp.fetch(mailjetUrl, mailjetOptions));
Logger.log(response);
Upvotes: 0
Reputation: 31300
The CURL example at mailjet looks like this:
curl -s \
-X POST \
--user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" \
https://api.mailjet.com/v3.1/send \
-H 'Content-Type: application/json' \
-d '{
"Messages":[
{
"From": {
"Email": "[email protected]",
"Name": "Mailjet Pilot"
},
"To": [
{
"Email": "[email protected]",
"Name": "passenger 1"
}
],
"Subject": "Your email flight plan!",
"TextPart": "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!",
"HTMLPart": "<h3>Dear passenger 1, welcome to Mailjet!</h3><br/>May the delivery force be with you!"
}
]
}'
You are missing the:
--user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE"
part.
You can see the following post at SO:
StackOverflow - How to use UrlFetchApp with credentials? Google Scripts
But mailjet may have a specific syntax.
Upvotes: 1
Reputation: 4460
If you're looking to interact with External API's using GAS, have a look at some of the documentation here:
https://developers.google.com/apps-script/guides/services/external
As for API interaction with MailJet, I would say look at the ES2015 Javascript wrapper as a starting point and see if it fits in with GAS. See here:
https://github.com/mailjet/mailjet-apiv3-nodejs-es2015
Upvotes: 0