-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailHandler.js
49 lines (43 loc) · 1.46 KB
/
mailHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const nodemailer = require('nodemailer');
const creds = require('./client_secret.json');
const fs = require('fs');
const path = require('path');
// Use dotenv to get variables from .env
const dotenv = require('dotenv');
dotenv.config();
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: 'OAuth2',
user: process.env.EMAIL,
serviceClient: creds.client_id,
privateKey: creds.private_key,
},
});
async function mailHandler(interest) {
// Choose what tamplate to use
const templateName = interest.english
? './mailTemplate_engelsk'
: './mailTemplate_norsk';
// Get the HTML from the template then,
// Replace elements in the template to make it 'DYNAMIC AF'
const html = fs
.readFileSync(path.resolve(__dirname, templateName), 'utf8')
.replace('<COMPANYNAME>', interest.companyName)
.replace('<YEAR>', process.env.YEAR)
.replace(
'<DATO>',
new Date().toLocaleString('no', { hour12: false })
);
// Returns a promise from Nodemailer
return transporter.sendMail({
from: `"itDAGENE ${process.env.YEAR}" <${process.env.EMAIL}>`,
to: `${interest.contactPerson} - ${interest.companyName} <${interest.contactEmail}>`,
subject: `itDAGENE ${process.env.YEAR}: ${interest.companyName}`,
text: `itDAGENE ${process.env.YEAR}: ${interest.companyName} confirmation email.`,
html: html,
});
}
module.exports = mailHandler;