Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add email service
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters