Please welcome the new Mailjet official NodeJS API wrapper!
Mailjet is an Email Service Provider (ESP). Visit the website and get comfortable!
Every code examples can be find on the Mailjet Documentation
(Please refer to the Mailjet Documentation Repository to contribute to the documentation examples)
first, create a project folder
mkdir mailjet-project && cd $_
if you want to get a global installation, you can add -g
npm install node-mailjet
To authenticate, go get your API key, and API secret here, open your favorite text editor and import the mailjet module
var Mailjet = require('node-mailjet').connect('api key', 'api secret');
Additional connection options may be passed as the third argument. The supported values are:
proxyUrl
: HTTP proxy URL to send the API requests throughtimeout
: API request timeout in millisecondsurl
(default:api.mailjet.com
): Base Mailjet API URLversion
(default: v3): API version to use in the URLperform_api_call
(default: true): controls if the must call must be performed to Mailjet API or not (dry run)
// The third argument (the object) is not mandatory. Each configuration key is also optional
const mailjet = require ('node-mailjet')
.connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE, {
url: 'api.mailjet.com', // default is the API url
version: 'v3.1', // default is '/v3'
perform_api_call: true // used for tests. default is true
})
On top of that, you can also pass those options locally to a request:
// the second argument (the object) is not mandatory. Each configuration key is also optional
const request = mailjet
.post("send", {
url: 'api.mailjet.com', version: 'v3', perform_api_call: false
})
.request({
FromEmail: '[email protected]',
FromName: 'Mailjet Pilot',
Subject: 'Hello world Mailjet!',
'Text-part': 'Hello World',
Recipients: [{'Email': '[email protected]'}]
})
The proxy URL is passed directly to superagent-proxy.
echo 'export MJ_APIKEY_PUBLIC=MY_API_KEY' >> ~/.zshrc
echo 'export MJ_APIKEY_PRIVATE=MY_API_SECRET' >> ~/.zshrc
source ~/.zshrc
replace zshrc
with bash_profile
if you are simply using bash
var apiKey = process.env.MJ_APIKEY_PUBLIC,
apiSecret = process.env.MJ_APIKEY_PRIVATE;
// GET resource
var user = Mailjet.get('user');
// POST resource
var sender = Mailjet.post('sender');
user.request(function (error, response, body) {
if (error)
console.log ('Oops, something went wrong ' + response.statusCode);
else
console.log (body);
});
user.request()
.then(function (result) {
// do something with the result
// result structure is {response: {...}, body: {...}}
})
.catch(function (reason) {
// handle the rejection reason
console.log(reason.statusCode)
})
sender.request({ Email: '[email protected]' })
.then(handleData)
.catch(handleError);
var getContacts = Mailjet.get('contact');
getContacts.request({Limit: 3}, handleContacts);
getContacts.id(2).request(handleSingleContact)
var postContact = Mailjet.post('contact');
postContact.action('managemanycontacts').request({
ContactLists: MyContactListsArray,
Contacts: MyContactsArray,
}, handlePostResponse)
var sendEmail = Mailjet.post('send');
var emailData = {
'FromEmail': '[email protected]',
'FromName': 'My Name',
'Subject': 'Test with the NodeJS Mailjet wrapper',
'Text-part': 'Hello NodeJs !',
'Recipients': [{'Email': '[email protected]'}],
'Attachments': [{
"Content-Type": "text-plain",
"Filename": "test.txt",
"Content": "VGhpcyBpcyB5b3VyIGF0dGFjaGVkIGZpbGUhISEK", // Base64 for "This is your attached file!!!"
}]
}
sendEmail
.request(emailData)
.then(handlePostResponse)
.catch(handleError);
var emailData = {
'FromEmail': '[email protected]',
'FromName': 'Mailjet Pilot',
'Subject': 'Hello world Mailjet!',
'Text-part': 'Hello world!',
'Recipients': [{'Email': '[email protected]'}],
};
var emailData2 = {
'FromEmail': '[email protected]',
'FromName': 'Mailjet Pilot',
'Subject': 'Hello world Mailjet!',
'Text-part': 'This is another Email',
'Recipients': [{'Email': '[email protected]'}],
};
sendEmail
.request(emailData)
.then(handleData)
.catch(handleError);
sendEmail
.request(emailData2)
.then(handleData)
.catch(handleError);
var mailjet = require ('./mailjet-client')
.connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
function handleError (err) {
throw new Error(err.ErrorMessage);
}
function newContact (email) {
mailjet.post('contact')
.request({Email: email})
.catch(handleError);
}
function testEmail (text) {
email = {};
email.FromName = 'Your Name';
email.FromEmail = 'Your Sender Address';
email.Subject = 'Test Email';
email.Recipients = [{Email: 'Your email'}];
email['Text-Part'] = text;
mailjet.post('send')
.request(email)
.catch(handleError);
}
testEmail('Hello World!');
In mailjet-client v4 we have introduced new token authentication method for the sms api. You can generate token from here
var Mailjet = require('node-mailjet').connect('api token');
Additional connection options may be passed as a second argument. The supported values are:
proxyUrl
: HTTP proxy URL to send the API requests throughtimeout
: API request timeout in millisecondsurl
(default:api.mailjet.com
): Base Mailjet API URLversion
(default: v3): API version to use in the URLperform_api_call
(default: true): controls if the must call must be performed to Mailjet API or not (dry run)
// The second argument (the object) is not mandatory. Each configuration key is also optional
const mailjet = require ('node-mailjet')
.connect(process.env.MJ_API_TOKEN, {
url: 'api.mailjet.com', // default is the API url
version: 'v4', // default is '/v3'
perform_api_call: true // used for tests. default is true
})
We kept all other functionality unchanged
echo 'export MJ_API_TOKEN=MY_API_TOKEN' >> ~/.zshrc
source ~/.zshrc
replace zshrc
with bash_profile
if you are simply using bash
var apiToken = process.env.MJ_API_TOKEN;
// GET resource
var sms = Mailjet.get('sms');
// POST resource
var sendSms = Mailjet.post('sms-send');
var smsSend = Mailjet.post('sms-send');
var smsData = {
'Text': 'Have a nice SMS flight with Mailjet !',
'To': '+33600000000',
'From': 'MJPilot'
}
smsSend
.request(smsData)
.then(handlePostResponse)
.catch(handleError);
npm test
Officially supported Node.js versions:
v0.12.0(deprecated)- v4.1
- v4.0
- v5.0.0
- v6.11.1
Mailjet loves developers. You can be part of this project!
This wrapper is a great introduction to the open source world, check out the code!
Feel free to ask anything, and contribute:
- Fork the project.
- Create a new branch.
- Implement your feature or bug fix.
- Add documentation to it.
- Commit, push, open a pull request and voila.
TODO:
- Extend Error class to create Api errors