Send an email with MailJet

Hello everyone,

I need a little help to send an email with MailJet.
I have an ‘api key’, I have a ‘secret key’ and I have installed ‘node-mailjet’.

meteor npm install node-mailjet@3.2.1.

I have also initialized a mailjet object from these keys.

MJ_APIKEY_PUBLIC ='98.....';
MJ_APIKEY_PRIVATE ='f0.....';
const mailjet =require('node-mailjet').connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE);

But I can not find anywhere the syntax that allows to send an email.
Can someone help me, or tell me where I can find clear documentation on sending mail by javascript API with Mailjet

Thank you for your answers
YC

Sure.

import mailjet from "node-mailjet";

const mailjetClient = mailjet.connect(
  Meteor.settings.mailjet.APIKey,
  Meteor.settings.mailjet.secret
);

export async function sendEmail({
  from,
  subject,
  body,
  personalisations,
  payload,
  recipient,
  emailId,
  replyTo,
}) {
  async function sendEmail() {
    try {
      await mailjetClient.post("send").request({
        FromEmail: "your@email.com",
        FromName: from,
        Subject: subject,
        Vars: personalisations,
        Recipients: [{ Email: recipient }],
        "Mj-CustomID": emailId,
        Headers: { "Reply-To": replyTo },
        "Mj-EventPayLoad": payload
      });
      // I log the status of the newly sent out email in my db here...
    } catch (err) {
      console.error(err);
    }
  }

  sendEmail();
}

You can call the above function within a method or wherever (ensure it is server only)

Thank you for this quick response
I still have a problem:

server side

import mailjet from "node-mailjet";

APIKey ='664...';
secret ='939...';
const mailjetClient = mailjet.connect(
    Meteor.settings.mailjet.APIKey,
    Meteor.settings.mailjet.secret
);

And here I have the following error:

TypeError: Cannot read property ‘APIKey’ of undefined
at main.js (server/main.js:8:29)

And yet I did the ‘meteor npm install node-mailjet’ and I found the directory ‘node-mailjet’ in the directory node_modules of my application.

Did I miss something ?

You need to provide a settings file containing (at least):

{
  "mailjet": {
    "APIKey": "664...",
    "secret": " 939..."
  }
}

What @robfallows said. Alternatively you could adjust your code snippet to:

import mailjet from "node-mailjet";

APIKey ='664...';
secret ='939...';
const mailjetClient = mailjet.connect(
    APIKey,
    secret,
);

Which is simpler but less maintainable - especially if you ever separate production/staging/development accounts (which I would recommend).

Thank you very much
Everything is OK

1 Like

Hello,

It works and I can send mails by calling the function on the server.
But, is it possible to retrieve the result of sending (error or ok) on the client side, while it is an asynchronous processing ?

Thank you for your reply
Yvan COYAUD

You could do this either by initiating the server process using Meteor.call and getting the result in the callback, or by using the pub/sub API to send a result to a client-side subscription.

In either case, you would use throw new Meteor.Error(...) to send an error status back to the client (although you could send an error message without throwing an error, if you wanted to).

1 Like