cc-notifications v1.1.8
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
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 weather it is 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: "index.handlebars",
}).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: "index.handlebars",
});
//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:
to
- (required) recipient email address or comma separated multiple recipient email addresses or array of multiple recipient email addressescc
- (optional) cc recipient email address or comma separated multiple recipient email addresses or array of multiple recipient email addressesbcc
- (optional)bcc recipient email address or comma separated multiple recipient email addresses or array of multiple recipient email addressessubject
- (optional) email subject linebody
- (required) custom raw html or handlebars template namecontext
- (required with handlebar template) handlebars template attributesattachment
- (optional) to send attachment with an emailpriority
- (optional) priority for sending email, I.e.high
,normal
(default) orlow
.replyTo
- (optional) recipient email address for default reply-totext
- (optional) plaintext message text
sms object constructor options:
to
- recipient phone numberbody
- sms body text
Usage/Email Options
single email recipient
{
to: "reciver@mail.com",
}
multiple email recipients
{
to: "reciver_one@mail.com, reciver_two@mail.com",
}
{
to: [
"reciver_one@mail.com",
"reciver_two@mail.com"
],
}
cc email recipients
{
cc: "test_cc@mail.com",
}
bcc email recipients
{
bcc: "test_bcc@mail.com",
}
email subject line
{
subject: "Email Subject Line",
}
body html raw template
{
body: `<div>
<h1>welcome User</h1>
<a href="#">Activate Email</a>
</div>`,
}
email handlebar template
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",
},
]
}
email priority
{
priority: "high",
}
email reply-to recipient
{
replyTo: "Email Subject Line",
}
email text line
{
text: "Email Text Line",
}
Usage SMS Options
sms recipient
{
to: "+92300*******"
}
sms body text
{
body: "Hello you got a new message."
}
let msg = "Hello you got a new message."
{
body: msg
}
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
Mailtrap
- First navigate to the mailtrap site https://mailtrap.io.
- Next sign up/sign in to make an account so that you can test you emails.
- After making your account, on the left side you will find a list of options. You can choose either home or sandbox.
- Once you choose the options, you find the inboxes screen, under you projects tab select you project.
- Click on the project, you will see the dashboard area where you will be shown the Integrations dropdown.Click on the dropdown and select node (nodemailer) from the multiple options.
- Once you select the option, you will see the credentials, so copy them and save in
.env
file.
host: "smtp.mailtrap.io",
port: 2525,
auth: {
user: "4############4",
pass: "6############1"
}
Mailgun
- First sign up/sig in mailgun account fom official website https://www.mailgun.com.
- After success sign up/sign in, head to the left hand section and choose the Sending option.
- After selecting the sending option, you will find more sub-options and from those select the Domains.
- Select the domain by click on domain name, now you have to choose SMTP to send mails.
- After selecting the SMTP section, you will be provided with you SMTP settings.
- Now, you will see the credentials, so copy them and save in
.env
file. - Before you can start sending mails, you would also need a verified account. https://help.mailgun.com/hc/en-us/articles/217531258
- Once you have a verified account, you can send email.
SMTP hostname: smtp.mailgun.org
Port: 587
Username: postmaster@sandbox#############.mailgun.org
Default password: ####################
Sendgrid
- First sign up/sign in sendgrid account fom official website https://sendgrid.com.
- After success sign up/sign in, head to the left hand section and choose the Email API option.
- After selecting the option, you will find more sub-options and from those select the Integration Guide.
- now you have to choose SMTP to send mails.
- After selecting the SMTP section, a new page is populate. Enter you API key name of your choice and click Create Key.
- Now, you will see the credentials, so copy them and save in
.env
file.
Server: smtp.sendgrid.net
Ports: 25, 587 (for unencrypted/TLS connections)
465 (for SSL connections)
Username: ###################
Password: ###################
Twilio
- First visits official website https://www.twilio.com/ and sign up or sign in.
- After success sign up/sign in, navigate to the console.
- From left hand section, choose the Messaging option.
- After selecting the option, you will find more sub-options and from those select the Try it Out and Get Set Up.
- From here you will find your credentials Account SID and Auth Token.
- After that, select Services under Messaging.
- Click on Create Messaging Service and provide details and create service.
- Now, Messaging Services SID will be available.
- Now, you will see the credentials, so copy them and save in
.env
file.
accountSid = #####################;
authToken = ####################;
messagingServiceSid: ####################,