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
|
import sgMail from "@sendgrid/mail";
import sgHelpers from "@sendgrid/helpers";
import { ExpressHandlebars } from "express-handlebars";
import nodemailer, { Transporter } from "nodemailer";
import { GathioConfig, getConfig } from "./config.js";
import SMTPTransport from "nodemailer/lib/smtp-transport/index.js";
import { exitWithError } from "./process.js";
const config = getConfig();
type EmailTemplateName =
| "addEventAttendee"
| "addEventComment"
| "createEvent"
| "createEventGroup"
| "createEventMagicLink"
| "deleteEvent"
| "editEvent"
| "eventGroupUpdated"
| "removeEventAttendee"
| "subscribed"
| "unattendEvent";
export class EmailService {
nodemailerTransporter: Transporter | undefined = undefined;
sgMail: typeof sgMail | undefined = undefined;
hbs: ExpressHandlebars
public constructor(config: GathioConfig, hbs: ExpressHandlebars) {
this.hbs = hbs;
switch (config.general.mail_service) {
case "sendgrid": {
if (!config.sendgrid?.api_key) {
return exitWithError(
"Sendgrid is configured as the email service, but no API key is provided. Please provide an API key in the config file.",
);
}
this.sgMail = sgMail;
this.sgMail.setApiKey(config.sendgrid.api_key);
console.log("Sendgrid is ready to send emails.");
break;
}
case "nodemailer": {
if (config.nodemailer?.smtp_url) {
this.nodemailerTransporter = nodemailer.createTransport(
config.nodemailer?.smtp_url,
);
} else {
if (
!config.nodemailer?.smtp_server ||
!config.nodemailer?.smtp_port
) {
return exitWithError(
"Nodemailer is configured as the email service, but not all required fields are provided. Please provide all required fields in the config file.",
);
}
const nodemailerConfig = {
host: config.nodemailer?.smtp_server,
port: Number(config.nodemailer?.smtp_port) || 587,
tls: {
// do not fail on invalid certs
rejectUnauthorized: false,
},
} as SMTPTransport.Options;
if (config.nodemailer?.smtp_username) {
nodemailerConfig.auth = {
user: config.nodemailer?.smtp_username,
pass: config.nodemailer?.smtp_password,
};
}
this.nodemailerTransporter =
nodemailer.createTransport(nodemailerConfig);
}
}
}
}
public async verify(): Promise<boolean> {
if (this.nodemailerTransporter) {
const nodemailerVerified = await this.nodemailerTransporter.verify();
if (nodemailerVerified) {
console.log("Nodemailer is ready to send emails.");
return true;
} else {
return exitWithError(
"Error verifying Nodemailer transporter. Please check your Nodemailer configuration.",
);
}
}
return true;
}
public async sendEmail({
to,
bcc,
subject,
text,
html,
}: {
to: string | string[];
bcc?: string | string[];
subject: string;
text: string;
html?: string;
}): Promise<boolean> {
if (this.sgMail) {
try {
await this.sgMail.send({
to,
bcc,
from: config.general.email,
subject,
text,
html,
});
return true;
} catch (e: unknown | sgHelpers.classes.ResponseError) {
if (e instanceof sgHelpers.classes.ResponseError) {
console.error('sendgrid error', e.response.body);
} else {
console.error('sendgrid error', e);
}
return false;
}
} else if (this.nodemailerTransporter) {
try {
await this.nodemailerTransporter.sendMail({
from: config.general.email,
to,
bcc,
subject,
text,
html,
});
return true;
} catch (e) {
console.error(e);
return false;
}
} else {
// no mailer, so noop
return true;
}
}
public async sendEmailFromTemplate({
to,
bcc = "",
subject,
templateName,
templateData = {}
}: {
to: string | string[];
bcc?: string | string[] | undefined;
subject: string;
templateName: EmailTemplateName;
templateData?: object;
},
): Promise<boolean> {
const [html, text] = await Promise.all([
this.hbs.renderView(
`./views/emails/${templateName}/${templateName}Html.handlebars`,
{
domain: config.general.domain,
contactEmail: config.general.email,
siteName: config.general.site_name,
mailService: config.general.mail_service,
siteLogo: config.general.email_logo_url,
isFederated: config.general.is_federated || true,
cache: true,
layout: "email.handlebars",
...templateData,
}
),
this.hbs.renderView(
`./views/emails/${templateName}/${templateName}Text.handlebars`,
{
domain: config.general.domain,
contactEmail: config.general.email,
siteName: config.general.site_name,
mailService: config.general.mail_service,
siteLogo: config.general.email_logo_url,
isFederated: config.general.is_federated || true,
cache: true,
layout: "email.handlebars",
...templateData,
}
),
]);
return this.sendEmail({
to,
bcc,
subject: `${config.general.site_name}: ${subject}`,
text,
html
});
}
}
|