2

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": 'myemail@domain.com',"Name": 'Robert'},
      "To": [{"Email": 'theiremail@domain.com'}],
      "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

  • You are not sending the API keys in the request. Did you obtain public and private security keys from mailjet? You need to get those, and they need to be in the request. Your `mailjetparams` actually look good, (Compared to the CURL information) but you need authorization information to be sent. It might look like this: `"muteHttpExceptions": true, "headers": { "Authorization": "Bearer " + $MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE }` But I'm just guessing. I don't know the exact syntax for the authorization. – Alan Wells Aug 01 '18 at 13:58

3 Answers3

1

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": "pilot@mailjet.com",
                    "Name": "Mailjet Pilot"
                },
                "To": [
                    {
                        "Email": "passenger1@mailjet.com",
                        "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.

Alan Wells
  • 30,746
  • 15
  • 104
  • 152
0

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

ScottMcC
  • 4,094
  • 1
  • 27
  • 35
  • Thank you a lot for your answer ScottMcC. I will check the link out for the documentation of GAS. However, concerning the usage of the Mailjet's api, the problem is that all the codes found on internet use Mailjet packages, which are not available on GAS. :/ – Clément Vaes Aug 01 '18 at 10:13
  • The API won't be supported in GAS by default but if you either set up the library file or instead make the appropriate `GET` or `POST` requests to the MailJet's API you should be able to get it to work – ScottMcC Aug 01 '18 at 13:57
0

you have to do some modifications.

  1. Add authorization information
  2. Make sure if the email exists 'myemail@domain.com'

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);