aw-notifications v0.0.1
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:
Props | Option | Description | Example |
---|---|---|---|
to | required | recipient email address or array of comma separated multiple recipient email addresses | to: "reciver@mail.com" |
body | required | custom raw html or handlebars template name | body: "<h1>Email</h1>" |
subject | optional | email subject line | subject: "Email Subject Line" |
cc | optional | cc recipient email address or array of comma separated multiple recipient email addresses | cc: ["reciver_one@mail.com","reciver_two@mail.com"] |
bcc | optional | bcc recipient email address or array of comma separated multiple recipient email addresses | bcc: "reciver@mail.com" |
context | required | (handlebars) handlebars template attributes | see more |
attachment | optional | send attachment with an email | see more |
priority | optional | priority for sending email, I.e. high , normal (default) or low | priority: "high" |
replyTo | optional | recipient email address for default reply-to | replyTo: "Email Subject Line" |
text | optional | plaintext message text | text: "Email Text Line" |
sms object constructor options:
Props | Option | Description | Example |
---|---|---|---|
to | required | recipient phone number | to: "+92300*******" |
body | required | sms body text | body: "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
1 year ago