-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
96 lines (79 loc) · 2.72 KB
/
script.js
File metadata and controls
96 lines (79 loc) · 2.72 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require('dotenv').config()
const nodemailer = require('nodemailer')
const path = require('path')
const fs = require('fs')
// initialize nodemailer
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.MAIL_USER,
pass: process.env.MAIL_PASS,
},
})
// point to the template folder
const handlebarOptions = {
viewEngine: {
partialsDir: path.resolve('./template/'),
defaultLayout: false,
},
viewPath: path.resolve('./template/'),
}
const getMailOptions = (recipientMail, context) => ({
from: '"ISAG Laboratory" <isag@kmitl.ac.th>', // sender address
template: 'isag', // the name of the template file, i.e., email.handlebars
to: recipientMail,
replyTo: context['อีเมลผู้รับผิดชอบ'],
subject: `ISAG Warm Welcome 2025 - เชิญชวนเข้าร่วมงาน`,
context: {
...context,
},
})
const sendMail = async (mailOptions) => {
const { default: hbs } = await import('nodemailer-express-handlebars')
// use a template file with nodemailer
transporter.use('compile', hbs(handlebarOptions))
try {
await transporter.sendMail(mailOptions)
} catch (error) {
console.log(
`Nodemailer error sending email to ${mailOptions['ชื่อเล่น']}`,
error
)
}
}
const raw = fs.readFileSync('./mail.csv', 'utf8')
const readCSV = async () => {
const { default: csv } = await import('neat-csv')
const data = await csv(raw)
return data
}
// Send every mail AT ONCE (will get flagged as SPAM)
const sendAllMail = async () => {
const recipients = await readCSV()
console.log('========== Script Running... ==========')
recipients.forEach((recipient) => {
sendMail(getMailOptions(recipient['อีเมล'], recipient))
console.log(
`Sent to: ${recipient['ชื่อ']} ${
recipient['นามสกุล']
} at ${new Date().toLocaleTimeString()}`
)
})
}
// NOTE: You could try rate limiting to avoid being flagged as spam, but I'm not sure what's the right setting for that.
// Why is this message in spam? Lots of messages from kmitl.ac.th were identified as spam in the past.
// const sendAllMail = async () => {
// const recipients = await readCSV()
// console.log('========== Script Running... ==========')
// for (const recipient of recipients) {
// await sendMail(getMailOptions(recipient['อีเมล'], recipient))
// console.log(
// `Sent to: ${recipient['ชื่อ']} ${
// recipient['นามสกุล']
// } at ${new Date().toLocaleTimeString()}`
// )
// // rate limit to avoid spam call out
// await new Promise((resolve) => setTimeout(resolve, 1000)) // 1 email/second
// }
// }
sendAllMail()