-
Notifications
You must be signed in to change notification settings - Fork 0
/
emailer.js
209 lines (182 loc) · 6.18 KB
/
emailer.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const e = require('connect-flash');
const { log } = require('console');
const fs = require('fs');
const nodemailer = require('nodemailer');
const emailConfig = {
host: 'smtp.zoho.eu',
port: '465',
secure: true,
auth: {
user: 'mailer@probooksaccounting.co.uk',
pass: 'ZAmBWUPFDEwz',
},
};
const sender = '"ProBooks Accounting" <mailer@probooksaccounting.co.uk>'
const receiver = 'info@nlcsunderland.uk';
const emailFooter = "\n\n\n\nThank you for using our services!\n\nIf you have any doubts using our services, please reply to this email\n\n\n\n Probooks Accounting © - Alpha Media Productions Ltd."
async function createAndEmail(fileType, subject, message) {
const fileName = `${fileType}.csv`;
const backupFilename = `${fileType}_backup.csv`;
fs.copyFileSync(fileName, backupFilename);
const transporter = nodemailer.createTransport(emailConfig);
const mailOptions = {
from: sender,
to: receiver,
subject: subject,
text: `Find attached the ${message}.` + emailFooter,
attachments: [
{
filename: backupFilename,
path: backupFilename,
},
],
};
try {
const info = await transporter.sendMail(mailOptions);
console.log('Email sent successfully!', info.response);
} catch (error) {
console.error('Error sending email: ', error);
}
fs.unlinkSync(backupFilename);
fs.unlinkSync(fileName);
}
async function sendStatementByEmail(pdfPath) {
const transporter = nodemailer.createTransport(emailConfig);
const mailOptions = {
from: sender,
to: receiver,
subject: 'Statement of Donations',
text: 'Find attached the statement of donations.' + emailFooter,
attachments: [
{
filename: pdfPath,
path: `./${pdfPath}`,
},
],
};
try {
const info = await transporter.sendMail(mailOptions);
log("Statement sent: " + pdfPath + info.response);
fs.unlinkSync(pdfPath);
} catch (error) {
log("'Error sending email: " + error)
}
}
async function sendTransactionsEmail(pdfPath, receiverEmail) {
const transporter = nodemailer.createTransport(emailConfig);
const mailOptions = {
from: sender,
to: receiverEmail,
subject: 'Statement of Transactions',
text: 'As requested, please find attached the statement of transactions.' + emailFooter,
attachments: [
{
filename: pdfPath,
path: `./${pdfPath}`,
},
],
};
try {
const info = await transporter.sendMail(mailOptions);
log("Transactions email sent: " + pdfPath + info.response);
fs.unlinkSync(pdfPath);
} catch (error) {
log("'Error sending email: " + error)
}
}
async function createAndEmailDBBackup() {
const dbFilename = 'db.sqlite';
const backupFilename = 'db_backup.sqlite';
fs.copyFileSync(dbFilename, backupFilename);
const transporter = nodemailer.createTransport(emailConfig);
const mailOptions = {
from: sender,
to: 'albinm65@gmail.com',
subject: 'ProBooks Accounting - Database Backup',
text: 'Find attached the database backup.' + emailFooter,
attachments: [
{
filename: backupFilename,
path: backupFilename,
},
],
};
try {
const info = await transporter.sendMail(mailOptions);
console.log('Email sent successfully!', info.response);
} catch (error) {
console.error('Error sending email:', error);
}
fs.unlinkSync(backupFilename);
}
async function emailMemberForUpdate (row) {
const transporter = nodemailer.createTransport(emailConfig);
link = "https://probooksaccounting.co.uk/edit-member/"+row.id;
const mailOptions = {
from: sender,
to: row.email,
subject: 'ProBooks Accounting - Update Member Details',
text: 'Dear ' + row.first_name + ' ' + row.surname + ',\n\nPlease click on the link below to check your details stored by NewLife Church Sunderland, and update any details that are not correct.\n ' + link + emailFooter,
};
try {
const info = await transporter.sendMail(mailOptions);
console.log('Email sent successfully!', info.response);
} catch (error) {
console.error('Error sending email:', error);
}
}
async function sendUpdateSuggestionEmail(suggestion, user) {
const transporter = nodemailer.createTransport(emailConfig);
const mailOptions = {
from: sender,
to: "albinm65@gmail.com",
subject: 'Update Suggestion from User',
text: user + ' Has suggested this update to the software: ' + suggestion + ". Please respond!" + emailFooter,
};
try {
const info = await transporter.sendMail(mailOptions);
log("Update suggestion email sent by: " + user + ' ' + info.response);
} catch (error) {
log("'Error sending email: " + error)
}
}
async function sendNewUserAddedEmail(user) {
const transporter = nodemailer.createTransport(emailConfig);
const mailOptions = {
from: sender,
to: "albinm65@gmail.com",
subject: 'New User Added',
text: user + ' Has been added. ' + emailFooter,
};
try {
const info = await transporter.sendMail(mailOptions);
log("New user email sent:" + ' ' + info.response);
} catch (error) {
log("'Error sending email: " + error)
}
}
async function sendDonationReceivedEmail(member, donation) {
const transporter = nodemailer.createTransport(emailConfig);
const mailOptions = {
from: sender,
to: member.email,
subject: 'Your Donation to NewLife Church Sunderland',
text: 'Dear ' + member.first_name + ' ' + member.surname + ',\n\nYour donation to NewLife Church Sunderland has been acknowledged by the treasurer. Here are the details: \n\nDonation Amount: £' + donation.amount + '\nFund: ' + donation.fund + '\nDated: ' + donation.date + '\n\nLet each man give according as he has determined in his heart, not grudgingly or under compulsion, for God loves a cheerful giver. - 2 Corinthians 9:7' + emailFooter,
};
try {
const info = await transporter.sendMail(mailOptions);
log("New donation email sent to: " + member.email + ' ' + info.response);
} catch (error) {
log("'Error sending email: " + error)
}
}
module.exports = {
createAndEmail,
sendStatementByEmail,
createAndEmailDBBackup,
emailMemberForUpdate,
sendTransactionsEmail,
sendUpdateSuggestionEmail,
sendNewUserAddedEmail,
sendDonationReceivedEmail
};