0.0.1 • Published 1 year ago

aw-notifications v0.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

CC-Notifications

a package use to send email notifications by using mailgun, mailtrap, sendgrid and sms notifications by using twilio.

Installation

Install cc-notifications with npm

npm i cc-notifications
npm install cc-notifications

Enter the path of the folder where handlebars template are placed. \ Enter .handlebars template folder path: template_folder

Environment Variables

To run this package, you will need to add the following environment variables to your .env file

For Email \ EMAIL_FROM \ EMAIL_SMTP \ EMAIL_PORT \ EMAIL_USER \ EMAIL_PASSWORD

For SMS \ SMS_ACCOUNT_SID \ SMS_AUTH_TOKEN \ SMS_SERVICE_SID

Usage Overview

The given functions can be used in any .js file. The starter index.js file or any other.

Sending Email

const { sendMail } = require("cc-notifications");
sendMail({
  to: "test@mail.com",
  subject: "This is an Email",
  body: `<h1>this is an email.</h1>`,
}).then((res) => {
  //return email_id if success else return error message
});

or

const { sendMail } = require("cc-notifications");

async function email() {
  let res = await sendMail({
    to: "test@mail.com",
    subject: "This is an Email",
    body: `<h1>this is an email.</h1>`,
  });
  //res contains email_id if success or error message if fail.
}

email();

Sending SMS

const { sendSMS } = require("cc-notifications");
sendSMS({
  to: "+92300*******",
  body: "this is an text sms.",
}).then((res) => {
  //return msg_id if success else return error message
});

or

const { sendSMS } = require("cc-notifications");

async function sms() {
  let res = await sendSMS({
    to: "+92300*******",
    body: "this is an text sms.",
  });
  //res contains sms_id if success or error message if fail.
}

sms();

Options

email object constructor options:

PropsOptionDescriptionExample
torequiredrecipient email address or array of comma separated multiple recipient email addressesto: "reciver@mail.com"
bodyrequiredcustom raw html or handlebars template namebody: "<h1>Email</h1>"
subjectoptionalemail subject linesubject: "Email Subject Line"
ccoptionalcc recipient email address or array of comma separated multiple recipient email addressescc: ["reciver_one@mail.com","reciver_two@mail.com"]
bccoptionalbcc recipient email address or array of comma separated multiple recipient email addressesbcc: "reciver@mail.com"
contextrequired(handlebars) handlebars template attributessee more
attachmentoptionalsend attachment with an emailsee more
priorityoptionalpriority for sending email, I.e. high, normal (default) or lowpriority: "high"
replyTooptionalrecipient email address for default reply-toreplyTo: "Email Subject Line"
textoptionalplaintext message texttext: "Email Text Line"

sms object constructor options:

PropsOptionDescriptionExample
torequiredrecipient phone numberto: "+92300*******"
bodyrequiredsms body textbody: "Hello you got a new message."

Usage/Email Options

body html raw template

{
    body: `<div>
        <h1>welcome User</h1>
        <a href="#">Activate Email</a>
    </div>`,
}

email handlebar template

create and save the templates in project level cc-notifications_views folder with .handlebars extension and call name with extension

project-name/cc-notifications_views/template_name.handlebars

{
    body: "template_name.handlebars",
}
email handlebar template context

project-name/cc-notifications_views/template_name.handlebars

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <h1>Welcome {{ username }} !</h1>
    <p>{{ body }}</p>
    <a href="{{ link }}" target="_blank">GET START</a>\
</body>
</html>
{
    body: "template_name.handlebars",
    context: {
        username: "User Name",
        link: "www.url-link.com",
        body: "this is body paragraph.",
    },
}
single email attachments
{
    attachments: [
        {
            filename: "fileName.extension",
            path: "./folderName/fileName.extension",
        }
    ]
}

multiple email attachments

{
    attachments: [
        {
            filename: "fileName.extension",
            path: "./folderName/fileName.extension",
        },
        {
            filename: "fileName.extension",
            path: "./folderName/fileName.extension",
        },
        {
            filename: "fileName.extension",
            path: "./folderName/fileName.extension",
        },
    ]
}

Version Support

  • node ^8.17.0 | ^19.1.0

Tech Stack

Dependencies:

  • nodemailer - sending mails
  • nodemailer-express-handlebars - .handlebars template
  • twilio - sending sms
  • dotenv - ready .env file

Features

email support

  • mailtrap
  • sendgrid
  • mailgun

sms support

  • twilio