@mirrorprod/ep-node-client
v1.0.3
Published
Javascript client to send mails using Electric Pigeon service
Downloads
2
Readme
Electric Pigeon node client
Javascript client to send mails using Electric Pigeon service
Installation
# using yarn
yarn add @mirrorprod/ep-node-client
# using npm
npm install @mirrorprod/ep-node-client --save
How to use
To use it just set the api key save it in an env file, it'll take just 3 minutes, so please, if someone stole this key we have to update many projects.
To send the mail just call the sendMail
method using and object like this:
interface MailData {
to: string;
fromEmail: string;
fromName: string;
subject: string;
mailBody: string;
replyTo?: string;
attachments?: {
fileName?: string;
contentBody: string; // the file in base64 format
contentType: string;
}[];
}
import { electricPigeonClient } from "@mirrorprod/ep-node-client";
electricPigeonClient.setApiKey(proces.env.MAIL_API_KEY);
try {
await electricPigeonClient.sendMail({
fromEmail: "[email protected]", // this must be a verified email
fromName: "name",
to: "[email protected]",
subject: "subject",
mailBody: "mail body",
replyTo: "[email protected]",
attachments: [
{
fileName: "filename",
contentBody: "base64 of the file",
contentType: "application/pdf",
},
],
});
} catch (err) {
console.log(err);
}
If you are using it in a NextJS API here a complete snippet
import { NextApiRequest, NextApiResponse } from "next";
import { electricPigeonClient } from "@mirrorprod/ep-node-client";
const sendMail = async (req: NextApiRequest, res: NextApiResponse) => {
const apiKey = process.env.MAIL_API_KEY || "";
electricPigeonClient.setApiKey(apiKey);
try {
await electricPigeonClient.sendMail({
fromEmail: "[email protected]", // this must be a verified email
fromName: "name",
to: "[email protected]",
subject: "subject",
mailBody: "mail body",
replyTo: "[email protected]",
attachments: [
{
fileName: "filename",
contentBody: "base64 of the file",
contentType: "application/pdf",
},
],
});
return res.json({
status: 200,
});
} catch (error: any) {
console.error(error);
res.statusCode = 400;
res.json({ error: error?.message });
}
};
export default sendMail;