Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add email service
  • Loading branch information
mekzy-o committed Sep 20, 2020
1 parent 1de2e1a commit e7ec90a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .env.example
Expand Up @@ -6,3 +6,10 @@ REDIS_URL=redis://127.0.0.1:6379

DB_URL=XXXXXXXXXXXXXX
DB_URL_TEST=XXXXXXXXXXXXXX

host=smtp.gmail.com
gmailPort=XXXXXX
username=XXXXXXXXXXX
password=XXXXXXXXXXX
secure: false
defaultSender=EDUSTRIPE
76 changes: 76 additions & 0 deletions app/services/EmailServices.js
@@ -0,0 +1,76 @@
import fs from 'fs';
import ejs from 'ejs';
import util from 'util';
import path from 'path';
import 'dotenv/config';
import nodemailer from 'nodemailer';
const readFileAsync = util.promisify(fs.readFile);
const templatesDirectory = path.join(__dirname, './../mail/templates');

export default class EmailService {
constructor() {
this.transporter = nodemailer.createTransport({
service: 'gmail',
port: process.env.gmailPort,
secure: false,
auth: {
user: process.env.username,
pass: process.env.password,
},
tls: {
rejectUnauthorized: false,
},
});
}

async init(fileName) {
this.mailTemplates = await readFileAsync(
path.join(templatesDirectory, `${fileName}.html`),
'utf-8'
);
}

sendEmail(options, data) {
let { recipients, from, subject, text, html } = options;

data = data || {};

if (!(recipients && subject && html))
throw Error('Provide required options');
let to = recipients;
if (Array.isArray(to)) {
to = to.join(',');
}

html = ejs.render(html, data);

options.from = from || process.env.defaultSender;
options.text = text || html;
options.html = html;
options.to = to;

// send mail with defined transport object
this.transporter.sendMail(options, (error) => {
if (error) {
return console.log(error);
}

console.log('Message sent!');
});
}

async sendTestMail(firstname, messageFile) {
await this.init(messageFile);
const options = {
//Array of recipient(s)
recipients: ['babajide.esho@edustripe.com'],
subject: 'Test Mail',
html: this.mailTemplates,
};

const data = {
firstname,
};
this.sendEmail(options, data);
}
}
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -34,6 +34,7 @@
"helmet": "^3.22.0",
"jsonwebtoken": "^8.5.1",
"morgan": "^1.10.0",
"nodemailer": "^6.4.11",
"pg": "^8.0.0",
"redis": "^3.0.2",
"sequelize": "^5.21.5",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -4063,6 +4063,11 @@ node-releases@^1.1.58:
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-1.1.58.tgz#8ee20eef30fa60e52755fcc0942def5a734fe935"
integrity sha512-NxBudgVKiRh/2aPWMgPR7bPTX0VPmGx5QBwCtdHitnqFE5/O8DeBXuIMH1nwNnw/aMo6AjOrpsHzfY3UbUJ7yg==

nodemailer@^6.4.11:
version "6.4.11"
resolved "https://registry.yarnpkg.com/nodemailer/-/nodemailer-6.4.11.tgz#1f00b4ffd106403f17c03f3d43d5945b2677046c"
integrity sha512-BVZBDi+aJV4O38rxsUh164Dk1NCqgh6Cm0rQSb9SK/DHGll/DrCMnycVDD7msJgZCnmVa8ASo8EZzR7jsgTukQ==

nodemon@^2.0.2:
version "2.0.4"
resolved "https://registry.yarnpkg.com/nodemon/-/nodemon-2.0.4.tgz#55b09319eb488d6394aa9818148c0c2d1c04c416"
Expand Down

0 comments on commit e7ec90a

Please sign in to comment.