please-send-emails v0.6.0
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.gitRename
.env.samplefile to.envand update the values to that of yours.Install dependencies:
yarn install #or npm installRun 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
namefull name in initial letters in capitalemailemail address of the userbirth_datedate of birth inyyyy-mm-ddformat onlyjoining_datedate of joining inyyyy-mm-ddformat 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.tsunder object contextHandlers. In, thiscontextHandlersobject, 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:
1. Add HTML template to src/assets/email-templates/ folder under some unique name
Register the event of this template in
src/types.tsunder 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.tsunder object contextHandlers. In, thiscontextHandlersobject, 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 AddressandPasswordto 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 SecretandRefresh Tokento authenticate
- It uses
Following ENV variables are necessary irrespective of the authentication type:
SMTP_HOSTe.g for gmail its value issmtp.gmail.com.SMTP_PORTe.g465for secure and587for not secure.SMTP_AUTH_TYPEIt can have one of the following values:LOGINOAUTH2
SMTP_AUTH_USERthis 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_PASSThis 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_IDSMTP_AUTH_OAUTH2_CLIENT_SECRETSMTP_AUTH_OAUTH2_REFRESH_TOKEN
Now to configure the sender and recipients name and emails, uncomment and update the following ENV variables:
EMAIL_FROMi.e sender of this email notificationEMAIL_TOa 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.