]>
Commit | Line | Data |
---|---|---|
d95d1559 C |
1 | import { readFileSync } from 'fs-extra' |
2 | import { merge } from 'lodash' | |
ecb4e35f | 3 | import { createTransport, Transporter } from 'nodemailer' |
d95d1559 C |
4 | import { join } from 'path' |
5 | import { VideoChannelModel } from '@server/models/video/video-channel' | |
6 | import { MVideoBlacklistLightVideo, MVideoBlacklistVideo } from '@server/types/models/video/video-blacklist' | |
7 | import { MVideoImport, MVideoImportVideo } from '@server/types/models/video/video-import' | |
8 | import { Abuse, EmailPayload } from '@shared/models' | |
9 | import { SendEmailOptions } from '../../shared/models/server/emailer.model' | |
df4c603d | 10 | import { isTestInstance, root } from '../helpers/core-utils' |
05e67d62 | 11 | import { bunyanLogger, logger } from '../helpers/logger' |
4c1c1709 | 12 | import { CONFIG, isEmailEnabled } from '../initializers/config' |
6dd9de95 | 13 | import { WEBSERVER } from '../initializers/constants' |
d95d1559 C |
14 | import { MAbuseFull, MActorFollowActors, MActorFollowFull, MUser } from '../types/models' |
15 | import { MCommentOwnerVideo, MVideo, MVideoAccountLight } from '../types/models/video' | |
16 | import { JobQueue } from './job-queue' | |
17 | ||
df4c603d | 18 | const Email = require('email-templates') |
dee77e76 | 19 | |
ecb4e35f C |
20 | class Emailer { |
21 | ||
22 | private static instance: Emailer | |
23 | private initialized = false | |
24 | private transporter: Transporter | |
25 | ||
a1587156 C |
26 | private constructor () { |
27 | } | |
ecb4e35f C |
28 | |
29 | init () { | |
30 | // Already initialized | |
31 | if (this.initialized === true) return | |
32 | this.initialized = true | |
33 | ||
887e1a03 | 34 | if (isEmailEnabled()) { |
ed3f089c IB |
35 | if (CONFIG.SMTP.TRANSPORT === 'smtp') { |
36 | logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT) | |
37 | ||
38 | let tls | |
39 | if (CONFIG.SMTP.CA_FILE) { | |
40 | tls = { | |
41 | ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ] | |
42 | } | |
ecb4e35f | 43 | } |
ecb4e35f | 44 | |
ed3f089c IB |
45 | let auth |
46 | if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) { | |
47 | auth = { | |
48 | user: CONFIG.SMTP.USERNAME, | |
49 | pass: CONFIG.SMTP.PASSWORD | |
50 | } | |
f076daa7 | 51 | } |
f076daa7 | 52 | |
ed3f089c IB |
53 | this.transporter = createTransport({ |
54 | host: CONFIG.SMTP.HOSTNAME, | |
55 | port: CONFIG.SMTP.PORT, | |
56 | secure: CONFIG.SMTP.TLS, | |
57 | debug: CONFIG.LOG.LEVEL === 'debug', | |
58 | logger: bunyanLogger as any, | |
59 | ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS, | |
60 | tls, | |
61 | auth | |
62 | }) | |
63 | } else { // sendmail | |
64 | logger.info('Using sendmail to send emails') | |
65 | ||
66 | this.transporter = createTransport({ | |
67 | sendmail: true, | |
68 | newline: 'unix', | |
9afa0901 | 69 | path: CONFIG.SMTP.SENDMAIL |
ed3f089c IB |
70 | }) |
71 | } | |
ecb4e35f C |
72 | } else { |
73 | if (!isTestInstance()) { | |
74 | logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!') | |
75 | } | |
76 | } | |
77 | } | |
78 | ||
d3e56c0c | 79 | static isEnabled () { |
ed3f089c IB |
80 | if (CONFIG.SMTP.TRANSPORT === 'sendmail') { |
81 | return !!CONFIG.SMTP.SENDMAIL | |
82 | } else if (CONFIG.SMTP.TRANSPORT === 'smtp') { | |
83 | return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT | |
84 | } else { | |
85 | return false | |
86 | } | |
3b3b1820 C |
87 | } |
88 | ||
ecb4e35f | 89 | async checkConnectionOrDie () { |
ed3f089c | 90 | if (!this.transporter || CONFIG.SMTP.TRANSPORT !== 'smtp') return |
ecb4e35f | 91 | |
3d3441d6 C |
92 | logger.info('Testing SMTP server...') |
93 | ||
ecb4e35f C |
94 | try { |
95 | const success = await this.transporter.verify() | |
96 | if (success !== true) this.dieOnConnectionFailure() | |
97 | ||
98 | logger.info('Successfully connected to SMTP server.') | |
99 | } catch (err) { | |
100 | this.dieOnConnectionFailure(err) | |
101 | } | |
102 | } | |
103 | ||
453e83ea | 104 | addNewVideoFromSubscriberNotification (to: string[], video: MVideoAccountLight) { |
cef534ed | 105 | const channelName = video.VideoChannel.getDisplayName() |
6dd9de95 | 106 | const videoUrl = WEBSERVER.URL + video.getWatchStaticPath() |
cef534ed | 107 | |
ecb4e35f | 108 | const emailPayload: EmailPayload = { |
cef534ed | 109 | to, |
df4c603d RK |
110 | subject: channelName + ' just published a new video', |
111 | text: `Your subscription ${channelName} just published a new video: "${video.name}".`, | |
112 | locals: { | |
113 | title: 'New content ', | |
114 | action: { | |
115 | text: 'View video', | |
116 | url: videoUrl | |
117 | } | |
118 | } | |
ecb4e35f C |
119 | } |
120 | ||
121 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
122 | } | |
123 | ||
8424c402 | 124 | addNewFollowNotification (to: string[], actorFollow: MActorFollowFull, followType: 'account' | 'channel') { |
f7cc67b4 C |
125 | const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName() |
126 | ||
f7cc67b4 | 127 | const emailPayload: EmailPayload = { |
df4c603d | 128 | template: 'follower-on-channel', |
f7cc67b4 | 129 | to, |
df4c603d RK |
130 | subject: `New follower on your channel ${followingName}`, |
131 | locals: { | |
132 | followerName: actorFollow.ActorFollower.Account.getDisplayName(), | |
133 | followerUrl: actorFollow.ActorFollower.url, | |
134 | followingName, | |
135 | followingUrl: actorFollow.ActorFollowing.url, | |
136 | followType | |
137 | } | |
f7cc67b4 C |
138 | } |
139 | ||
140 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
141 | } | |
142 | ||
453e83ea | 143 | addNewInstanceFollowerNotification (to: string[], actorFollow: MActorFollowActors) { |
883993c8 C |
144 | const awaitingApproval = actorFollow.state === 'pending' ? ' awaiting manual approval.' : '' |
145 | ||
883993c8 C |
146 | const emailPayload: EmailPayload = { |
147 | to, | |
df4c603d RK |
148 | subject: 'New instance follower', |
149 | text: `Your instance has a new follower: ${actorFollow.ActorFollower.url}${awaitingApproval}.`, | |
150 | locals: { | |
151 | title: 'New instance follower', | |
152 | action: { | |
153 | text: 'Review followers', | |
154 | url: WEBSERVER.URL + '/admin/follows/followers-list' | |
155 | } | |
156 | } | |
883993c8 C |
157 | } |
158 | ||
159 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
160 | } | |
161 | ||
8424c402 | 162 | addAutoInstanceFollowingNotification (to: string[], actorFollow: MActorFollowActors) { |
df4c603d | 163 | const instanceUrl = actorFollow.ActorFollowing.url |
8424c402 C |
164 | const emailPayload: EmailPayload = { |
165 | to, | |
df4c603d RK |
166 | subject: 'Auto instance following', |
167 | text: `Your instance automatically followed a new instance: <a href="${instanceUrl}">${instanceUrl}</a>.` | |
8424c402 C |
168 | } |
169 | ||
170 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
171 | } | |
172 | ||
453e83ea | 173 | myVideoPublishedNotification (to: string[], video: MVideo) { |
6dd9de95 | 174 | const videoUrl = WEBSERVER.URL + video.getWatchStaticPath() |
dc133480 | 175 | |
dc133480 C |
176 | const emailPayload: EmailPayload = { |
177 | to, | |
df4c603d RK |
178 | subject: `Your video ${video.name} has been published`, |
179 | text: `Your video "${video.name}" has been published.`, | |
180 | locals: { | |
181 | title: 'You video is live', | |
182 | action: { | |
183 | text: 'View video', | |
184 | url: videoUrl | |
185 | } | |
186 | } | |
dc133480 C |
187 | } |
188 | ||
189 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
190 | } | |
191 | ||
453e83ea | 192 | myVideoImportSuccessNotification (to: string[], videoImport: MVideoImportVideo) { |
6dd9de95 | 193 | const videoUrl = WEBSERVER.URL + videoImport.Video.getWatchStaticPath() |
dc133480 | 194 | |
dc133480 C |
195 | const emailPayload: EmailPayload = { |
196 | to, | |
df4c603d RK |
197 | subject: `Your video import ${videoImport.getTargetIdentifier()} is complete`, |
198 | text: `Your video "${videoImport.getTargetIdentifier()}" just finished importing.`, | |
199 | locals: { | |
200 | title: 'Import complete', | |
201 | action: { | |
202 | text: 'View video', | |
203 | url: videoUrl | |
204 | } | |
205 | } | |
dc133480 C |
206 | } |
207 | ||
208 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
209 | } | |
210 | ||
453e83ea | 211 | myVideoImportErrorNotification (to: string[], videoImport: MVideoImport) { |
6dd9de95 | 212 | const importUrl = WEBSERVER.URL + '/my-account/video-imports' |
dc133480 | 213 | |
df4c603d RK |
214 | const text = |
215 | `Your video import "${videoImport.getTargetIdentifier()}" encountered an error.` + | |
a1587156 | 216 | '\n\n' + |
df4c603d | 217 | `See your videos import dashboard for more information: <a href="${importUrl}">${importUrl}</a>.` |
dc133480 C |
218 | |
219 | const emailPayload: EmailPayload = { | |
220 | to, | |
df4c603d RK |
221 | subject: `Your video import "${videoImport.getTargetIdentifier()}" encountered an error`, |
222 | text, | |
223 | locals: { | |
224 | title: 'Import failed', | |
225 | action: { | |
226 | text: 'Review imports', | |
227 | url: importUrl | |
228 | } | |
229 | } | |
dc133480 C |
230 | } |
231 | ||
232 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
233 | } | |
234 | ||
453e83ea | 235 | addNewCommentOnMyVideoNotification (to: string[], comment: MCommentOwnerVideo) { |
cef534ed | 236 | const video = comment.Video |
df4c603d | 237 | const videoUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath() |
6dd9de95 | 238 | const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath() |
cef534ed | 239 | |
d9eaee39 | 240 | const emailPayload: EmailPayload = { |
df4c603d | 241 | template: 'video-comment-new', |
cef534ed | 242 | to, |
df4c603d RK |
243 | subject: 'New comment on your video ' + video.name, |
244 | locals: { | |
245 | accountName: comment.Account.getDisplayName(), | |
246 | accountUrl: comment.Account.Actor.url, | |
247 | comment, | |
248 | video, | |
249 | videoUrl, | |
250 | action: { | |
251 | text: 'View comment', | |
252 | url: commentUrl | |
253 | } | |
254 | } | |
d9eaee39 JM |
255 | } |
256 | ||
257 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
258 | } | |
259 | ||
453e83ea | 260 | addNewCommentMentionNotification (to: string[], comment: MCommentOwnerVideo) { |
f7cc67b4 C |
261 | const accountName = comment.Account.getDisplayName() |
262 | const video = comment.Video | |
df4c603d | 263 | const videoUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath() |
6dd9de95 | 264 | const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath() |
f7cc67b4 | 265 | |
f7cc67b4 | 266 | const emailPayload: EmailPayload = { |
df4c603d | 267 | template: 'video-comment-mention', |
f7cc67b4 | 268 | to, |
df4c603d RK |
269 | subject: 'Mention on video ' + video.name, |
270 | locals: { | |
271 | comment, | |
272 | video, | |
273 | videoUrl, | |
274 | accountName, | |
275 | action: { | |
276 | text: 'View comment', | |
277 | url: commentUrl | |
278 | } | |
279 | } | |
f7cc67b4 C |
280 | } |
281 | ||
282 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
283 | } | |
284 | ||
d95d1559 C |
285 | addAbuseModeratorsNotification (to: string[], parameters: { |
286 | abuse: Abuse | |
287 | abuseInstance: MAbuseFull | |
df4c603d RK |
288 | reporter: string |
289 | }) { | |
d95d1559 | 290 | const { abuse, abuseInstance, reporter } = parameters |
ba75d268 | 291 | |
d95d1559 C |
292 | const action = { |
293 | text: 'View report #' + abuse.id, | |
294 | url: WEBSERVER.URL + '/admin/moderation/abuses/list?search=%23' + abuse.id | |
295 | } | |
296 | ||
297 | let emailPayload: EmailPayload | |
298 | ||
299 | if (abuseInstance.VideoAbuse) { | |
300 | const video = abuseInstance.VideoAbuse.Video | |
301 | const videoUrl = WEBSERVER.URL + video.getWatchStaticPath() | |
302 | ||
303 | emailPayload = { | |
304 | template: 'video-abuse-new', | |
305 | to, | |
306 | subject: `New video abuse report from ${reporter}`, | |
307 | locals: { | |
308 | videoUrl, | |
309 | isLocal: video.remote === false, | |
310 | videoCreatedAt: new Date(video.createdAt).toLocaleString(), | |
311 | videoPublishedAt: new Date(video.publishedAt).toLocaleString(), | |
312 | videoName: video.name, | |
313 | reason: abuse.reason, | |
cfde28ba C |
314 | videoChannel: abuse.video.channel, |
315 | reporter, | |
d95d1559 C |
316 | action |
317 | } | |
318 | } | |
319 | } else if (abuseInstance.VideoCommentAbuse) { | |
320 | const comment = abuseInstance.VideoCommentAbuse.VideoComment | |
321 | const commentUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath() + ';threadId=' + comment.getThreadId() | |
322 | ||
323 | emailPayload = { | |
4f32032f | 324 | template: 'video-comment-abuse-new', |
d95d1559 C |
325 | to, |
326 | subject: `New comment abuse report from ${reporter}`, | |
327 | locals: { | |
328 | commentUrl, | |
310b5219 | 329 | videoName: comment.Video.name, |
d95d1559 C |
330 | isLocal: comment.isOwned(), |
331 | commentCreatedAt: new Date(comment.createdAt).toLocaleString(), | |
332 | reason: abuse.reason, | |
333 | flaggedAccount: abuseInstance.FlaggedAccount.getDisplayName(), | |
cfde28ba | 334 | reporter, |
d95d1559 C |
335 | action |
336 | } | |
337 | } | |
338 | } else { | |
339 | const account = abuseInstance.FlaggedAccount | |
340 | const accountUrl = account.getClientUrl() | |
341 | ||
342 | emailPayload = { | |
343 | template: 'account-abuse-new', | |
344 | to, | |
345 | subject: `New account abuse report from ${reporter}`, | |
346 | locals: { | |
347 | accountUrl, | |
348 | accountDisplayName: account.getDisplayName(), | |
349 | isLocal: account.isOwned(), | |
350 | reason: abuse.reason, | |
cfde28ba | 351 | reporter, |
d95d1559 | 352 | action |
df4c603d RK |
353 | } |
354 | } | |
ba75d268 C |
355 | } |
356 | ||
357 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
358 | } | |
359 | ||
df4c603d | 360 | async addVideoAutoBlacklistModeratorsNotification (to: string[], videoBlacklist: MVideoBlacklistLightVideo) { |
6dd9de95 | 361 | const VIDEO_AUTO_BLACKLIST_URL = WEBSERVER.URL + '/admin/moderation/video-auto-blacklist/list' |
8424c402 | 362 | const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath() |
df4c603d | 363 | const channel = (await VideoChannelModel.loadByIdAndPopulateAccount(videoBlacklist.Video.channelId)).toFormattedSummaryJSON() |
7ccddd7b JM |
364 | |
365 | const emailPayload: EmailPayload = { | |
df4c603d | 366 | template: 'video-auto-blacklist-new', |
7ccddd7b | 367 | to, |
df4c603d RK |
368 | subject: 'A new video is pending moderation', |
369 | locals: { | |
370 | channel, | |
371 | videoUrl, | |
372 | videoName: videoBlacklist.Video.name, | |
373 | action: { | |
374 | text: 'Review autoblacklist', | |
375 | url: VIDEO_AUTO_BLACKLIST_URL | |
376 | } | |
377 | } | |
7ccddd7b JM |
378 | } |
379 | ||
380 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
381 | } | |
382 | ||
453e83ea | 383 | addNewUserRegistrationNotification (to: string[], user: MUser) { |
f7cc67b4 | 384 | const emailPayload: EmailPayload = { |
df4c603d | 385 | template: 'user-registered', |
f7cc67b4 | 386 | to, |
df4c603d RK |
387 | subject: `a new user registered on ${WEBSERVER.HOST}: ${user.username}`, |
388 | locals: { | |
389 | user | |
390 | } | |
f7cc67b4 C |
391 | } |
392 | ||
393 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
394 | } | |
395 | ||
453e83ea | 396 | addVideoBlacklistNotification (to: string[], videoBlacklist: MVideoBlacklistVideo) { |
cef534ed | 397 | const videoName = videoBlacklist.Video.name |
6dd9de95 | 398 | const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath() |
26b7305a | 399 | |
cef534ed | 400 | const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : '' |
6dd9de95 | 401 | const blockedString = `Your video ${videoName} (${videoUrl} on ${WEBSERVER.HOST} has been blacklisted${reasonString}.` |
26b7305a | 402 | |
26b7305a | 403 | const emailPayload: EmailPayload = { |
cef534ed | 404 | to, |
df4c603d RK |
405 | subject: `Video ${videoName} blacklisted`, |
406 | text: blockedString, | |
407 | locals: { | |
408 | title: 'Your video was blacklisted' | |
409 | } | |
26b7305a C |
410 | } |
411 | ||
412 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
413 | } | |
414 | ||
453e83ea | 415 | addVideoUnblacklistNotification (to: string[], video: MVideo) { |
6dd9de95 | 416 | const videoUrl = WEBSERVER.URL + video.getWatchStaticPath() |
26b7305a | 417 | |
26b7305a | 418 | const emailPayload: EmailPayload = { |
cef534ed | 419 | to, |
df4c603d RK |
420 | subject: `Video ${video.name} unblacklisted`, |
421 | text: `Your video "${video.name}" (${videoUrl}) on ${WEBSERVER.HOST} has been unblacklisted.`, | |
422 | locals: { | |
423 | title: 'Your video was unblacklisted' | |
424 | } | |
26b7305a C |
425 | } |
426 | ||
427 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
428 | } | |
429 | ||
b426edd4 | 430 | addPasswordResetEmailJob (to: string, resetPasswordUrl: string) { |
cef534ed | 431 | const emailPayload: EmailPayload = { |
df4c603d | 432 | template: 'password-reset', |
cef534ed | 433 | to: [ to ], |
df4c603d RK |
434 | subject: 'Reset your account password', |
435 | locals: { | |
436 | resetPasswordUrl | |
437 | } | |
cef534ed C |
438 | } |
439 | ||
440 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
441 | } | |
442 | ||
df4c603d | 443 | addPasswordCreateEmailJob (username: string, to: string, createPasswordUrl: string) { |
45f1bd72 | 444 | const emailPayload: EmailPayload = { |
df4c603d | 445 | template: 'password-create', |
45f1bd72 | 446 | to: [ to ], |
df4c603d RK |
447 | subject: 'Create your account password', |
448 | locals: { | |
449 | username, | |
450 | createPasswordUrl | |
451 | } | |
45f1bd72 JL |
452 | } |
453 | ||
454 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
455 | } | |
456 | ||
cef534ed | 457 | addVerifyEmailJob (to: string, verifyEmailUrl: string) { |
cef534ed | 458 | const emailPayload: EmailPayload = { |
df4c603d | 459 | template: 'verify-email', |
cef534ed | 460 | to: [ to ], |
df4c603d RK |
461 | subject: `Verify your email on ${WEBSERVER.HOST}`, |
462 | locals: { | |
463 | verifyEmailUrl | |
464 | } | |
cef534ed C |
465 | } |
466 | ||
467 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
468 | } | |
469 | ||
453e83ea | 470 | addUserBlockJob (user: MUser, blocked: boolean, reason?: string) { |
eacb25c4 C |
471 | const reasonString = reason ? ` for the following reason: ${reason}` : '' |
472 | const blockedWord = blocked ? 'blocked' : 'unblocked' | |
eacb25c4 C |
473 | |
474 | const to = user.email | |
475 | const emailPayload: EmailPayload = { | |
476 | to: [ to ], | |
df4c603d RK |
477 | subject: 'Account ' + blockedWord, |
478 | text: `Your account ${user.username} on ${WEBSERVER.HOST} has been ${blockedWord}${reasonString}.` | |
eacb25c4 C |
479 | } |
480 | ||
481 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
482 | } | |
483 | ||
4e9fa5b7 | 484 | addContactFormJob (fromEmail: string, fromName: string, subject: string, body: string) { |
a4101923 | 485 | const emailPayload: EmailPayload = { |
df4c603d | 486 | template: 'contact-form', |
a4101923 | 487 | to: [ CONFIG.ADMIN.EMAIL ], |
df4c603d RK |
488 | replyTo: `"${fromName}" <${fromEmail}>`, |
489 | subject: `(contact form) ${subject}`, | |
490 | locals: { | |
491 | fromName, | |
492 | fromEmail, | |
493 | body | |
494 | } | |
a4101923 C |
495 | } |
496 | ||
497 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
498 | } | |
499 | ||
47f6cb31 | 500 | async sendMail (options: EmailPayload) { |
4c1c1709 | 501 | if (!isEmailEnabled()) { |
ecb4e35f C |
502 | throw new Error('Cannot send mail because SMTP is not configured.') |
503 | } | |
504 | ||
df4c603d RK |
505 | const fromDisplayName = options.from |
506 | ? options.from | |
6dd9de95 | 507 | : WEBSERVER.HOST |
4759fedc | 508 | |
df4c603d RK |
509 | const email = new Email({ |
510 | send: true, | |
511 | message: { | |
512 | from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>` | |
513 | }, | |
514 | transport: this.transporter, | |
515 | views: { | |
03fc1928 | 516 | root: join(root(), 'dist', 'server', 'lib', 'emails') |
df4c603d RK |
517 | }, |
518 | subjectPrefix: CONFIG.EMAIL.SUBJECT.PREFIX | |
519 | }) | |
520 | ||
47f6cb31 | 521 | for (const to of options.to) { |
df4c603d RK |
522 | await email |
523 | .send(merge( | |
524 | { | |
525 | template: 'common', | |
526 | message: { | |
527 | to, | |
528 | from: options.from, | |
529 | subject: options.subject, | |
530 | replyTo: options.replyTo | |
531 | }, | |
532 | locals: { // default variables available in all templates | |
533 | WEBSERVER, | |
534 | EMAIL: CONFIG.EMAIL, | |
535 | text: options.text, | |
536 | subject: options.subject | |
537 | } | |
538 | }, | |
539 | options // overriden/new variables given for a specific template in the payload | |
540 | ) as SendEmailOptions) | |
03fc1928 C |
541 | .then(res => logger.debug('Sent email.', { res })) |
542 | .catch(err => logger.error('Error in email sender.', { err })) | |
47f6cb31 | 543 | } |
ecb4e35f C |
544 | } |
545 | ||
546 | private dieOnConnectionFailure (err?: Error) { | |
d5b7d911 | 547 | logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err }) |
ecb4e35f C |
548 | process.exit(-1) |
549 | } | |
550 | ||
551 | static get Instance () { | |
552 | return this.instance || (this.instance = new this()) | |
553 | } | |
554 | } | |
555 | ||
556 | // --------------------------------------------------------------------------- | |
557 | ||
558 | export { | |
8dc8a34e | 559 | Emailer |
ecb4e35f | 560 | } |