please-send-emails
v0.6.0
Published
This parses the csv file and send emails like happy birthday or anniversary accordingly.
Downloads
15
Readme
Please Send Emails
This parses the csv file and send emails like happy birthday or anniversary accordingly.
Prerequisites
Make sure you have the following items setup on your system:
- node
- npm or yarn
- Email account, of course!
Events/Templates
Currently following events and their templates are supported:
event:
EVENT_TYPES.BIRTHDAY
, template:happy-birthday.html
event:
EVENT_TYPES.ANNIVERSARY
, template:happy-anniversary.html
event:
EVENT_TYPES.GIFT_SELECTION_BIRTHDAY
, template:gift-selection-birthday.html
- by default, this email is sent 20 days before the birthday
event:
EVENT_TYPES.GIFT_SELECTION_ANNIVERSARY
, template:gift-selection-anniversary.html
- by default, this email is sent 20 days before the anniversary date
How to Setup?
Take a clone of this repository:
git clone https://github.com/mukarramishaq/please-send-emails.git
Rename
.env.sample
file to.env
and update the values to that of yours.Install dependencies:
yarn install #or npm install
Run it to send today's pending emails:
yarn please-send-emails
How to update users data
Update Users in src/assets/csvs/users.csv
- This file has 4 columns
name
full name in initial letters in capitalemail
email address of the userbirth_date
date of birth inyyyy-mm-dd
format onlyjoining_date
date of joining inyyyy-mm-dd
format only
- update this file or replace it with yours but with similar structure
How to update email templates or Events
Update email templates in src/assets/email-templates/
folder.
These are Handlebar
templates. All the variables are in handlebars
format. Update the content as you like. But if the updated template has new variables then you need to update its context object:
- Update the template context object (i.e an object that contains all variables data which are being used in the template) in file
src/context.ts
under object contextHandlers. In, thiscontextHandlers
object, key is value from enum EVENT_TYPES and value is a function. Whatever this function will return will be used as context for the email template of that specific event.
How to add new email templates
Following are the steps:
Add HTML template to
src/assets/email-templates/
folder under some unique nameRegister the event of this template in
src/types.ts
under EVENT_TYPES enumRegister this email template in src/emailTemplatesRegister.ts by adding a new object of
TemplateRegistry
.
Note: attachments in TemplateRegistery
is an array of Attachment objects. And filename
and path
attributes of Attachment
can use context data. So, we can dynamically decide what attachment to send with email.
Note: Every element which can use context data must use Handlebars
notation to access the context attributes. e.g. "Happy {{whatTh}} Anniversary". Here whatTh
will be an attribute of context object
- Register the context creator function of this template in file
src/context.ts
under object contextHandlers. In, thiscontextHandlers
object, key is value from enum EVENT_TYPES and value is a function. Whatever this function will return will be used as context for the email template of that specific event.
Setup for SMTP Configurations
This repository use nodemailer to send emails through SMTP transporter. Currently, this repository supports two types of authentication for SMTP:
- LOGIN
- It uses
Email Address
andPassword
to authenticate. It is less secure and for gmail, you'll have to allow less secure apps beforehand.
- It uses
- OAUTH2
- It uses
Client ID
,Client Secret
andRefresh Token
to authenticate
- It uses
Following ENV variables are necessary irrespective of the authentication type:
SMTP_HOST
e.g for gmail its value issmtp.gmail.com
.SMTP_PORT
e.g465
for secure and587
for not secure.SMTP_AUTH_TYPE
It can have one of the following values:LOGIN
OAUTH2
SMTP_AUTH_USER
this is an email address through this nodemailer will authenticate and send emails
Now if your authentication type is LOGIN
, uncomment and update the values of the following ENV variables:
SMTP_AUTH_LOGIN_PASS
This is password to email address
And if your authentication type is OAUTH2
, uncomment and update the values of the following ENV variables:
SMTP_AUTH_OAUTH2_CLIENT_ID
SMTP_AUTH_OAUTH2_CLIENT_SECRET
SMTP_AUTH_OAUTH2_REFRESH_TOKEN
Now to configure the sender and recipients name and emails, uncomment and update the following ENV variables:
EMAIL_FROM
i.e sender of this email notificationEMAIL_TO
a comma-separated list of recipients emails
There are two more optional variables EMAIL_CC
and EMAIL_BCC
to configure a list of recipients as CC
or BCC
. Both these variables accept a comma-separated list of emails.