2.2.0 • Published 1 year ago
@emailjs/nodejs v2.2.0
Official EmailJS SDK for Node.js
SDK for EmailJS.com customers. \ Use you EmailJS account for sending emails.
Disclaimer
This is a NodeJS-only version, otherwise use
Links
Intro
EmailJS helps to send emails directly from your code. No large knowledge is required – just connect EmailJS to one of the supported email services, create an email template, and use our SDK to trigger an email.
Usage
Install EmailJS SDK using npm:
$ npm install @emailjs/nodejs
Note: By default, API requests are disabled for non-browser applications. You need to activate them through Account:Security.
FAQ
API calls are disabled for non-browser applications
You need to activate API requests through Account:Security.
Examples
ECMAScript modules
send email
import emailjs from '@emailjs/nodejs';
const templateParams = {
name: 'James',
notes: 'Check this out!',
};
emailjs
.send('<YOUR_SERVICE_ID>', '<YOUR_TEMPLATE_ID>', templateParams, {
publicKey: '<YOUR_PUBLIC_KEY>',
privateKey: '<YOUR_PRIVATE_KEY>', // optional, highly recommended for security reasons
})
.then(
(response) => {
console.log('SUCCESS!', response.status, response.text);
},
(err) => {
console.log('FAILED...', err);
},
);
init (optional)
import emailjs from '@emailjs/nodejs';
// set Public Key as global settings
emailjs.init({
publicKey: '<YOUR_PUBLIC_KEY>',
privateKey: '<YOUR_PRIVATE_KEY>', // optional, highly recommended for security reasons
});
emailjs.send('<YOUR_SERVICE_ID>', '<YOUR_TEMPLATE_ID>').then(
(response) => {
console.log('SUCCESS!', response.status, response.text);
},
(err) => {
console.log('FAILED...', err);
},
);
await/async with EmailJS error handler
import emailjs, { EmailJSResponseStatus } from '@emailjs/nodejs';
try {
await emailjs.send(
'<YOUR_SERVICE_ID>',
'<YOUR_TEMPLATE_ID>',
{},
{
publicKey: '<YOUR_PUBLIC_KEY>',
privateKey: '<YOUR_PRIVATE_KEY>', // optional, highly recommended for security reasons
},
);
console.log('SUCCESS!');
} catch (err) {
if (err instanceof EmailJSResponseStatus) {
console.log('EMAILJS FAILED...', err);
return;
}
console.log('ERROR', err);
}
CommonJS modules
send email
const emailjs = require('@emailjs/nodejs');
var templateParams = {
name: 'James',
notes: 'Check this out!',
};
emailjs
.send('<YOUR_SERVICE_ID>', '<YOUR_TEMPLATE_ID>', templateParams, {
publicKey: '<YOUR_PUBLIC_KEY>',
privateKey: '<YOUR_PRIVATE_KEY>', // optional, highly recommended for security reasons
})
.then(
function (response) {
console.log('SUCCESS!', response.status, response.text);
},
function (err) {
console.log('FAILED...', err);
},
);