-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend-email.js
More file actions
36 lines (28 loc) · 1.12 KB
/
send-email.js
File metadata and controls
36 lines (28 loc) · 1.12 KB
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
import { emailTemplates } from './email-template.js'
import dayjs from 'dayjs'
import transporter, { accountEmail } from '../config/nodemailer.js'
export const sendReminderEmail = async ({ to, type, subscription }) => {
if(!to || !type) throw new Error('Missing required parameters');
const template = emailTemplates.find((t) => t.label === type);
if(!template) throw new Error('Invalid email type');
const mailInfo = {
userName: subscription.user.name,
subscriptionName: subscription.name,
renewalDate: dayjs(subscription.renewalDate).format('MMM D, YYYY'),
planName: subscription.name,
price: `${subscription.currency} ${subscription.price} (${subscription.frequency})`,
paymentMethod: subscription.paymentMethod,
}
const message = template.generateBody(mailInfo);
const subject = template.generateSubject(mailInfo);
const mailOptions = {
from: accountEmail,
to: to,
subject: subject,
html: message,
}
transporter.sendMail(mailOptions, (error, info) => {
if(error) return console.log(error, 'Error sending email');
console.log('Email sent: ' + info.response);
})
}