]>
Commit | Line | Data |
---|---|---|
1 | import { readFileSync } from 'fs-extra' | |
2 | import { merge } from 'lodash' | |
3 | import { createTransport, Transporter } from 'nodemailer' | |
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 { AbuseState, EmailPayload, UserAbuse } from '@shared/models' | |
9 | import { SendEmailOptions } from '../../shared/models/server/emailer.model' | |
10 | import { isTestInstance, root } from '../helpers/core-utils' | |
11 | import { bunyanLogger, logger } from '../helpers/logger' | |
12 | import { CONFIG, isEmailEnabled } from '../initializers/config' | |
13 | import { WEBSERVER } from '../initializers/constants' | |
14 | import { MAbuseFull, MAbuseMessage, MAccountDefault, MActorFollowActors, MActorFollowFull, MUser } from '../types/models' | |
15 | import { MCommentOwnerVideo, MVideo, MVideoAccountLight } from '../types/models/video' | |
16 | import { JobQueue } from './job-queue' | |
17 | ||
18 | const Email = require('email-templates') | |
19 | ||
20 | class Emailer { | |
21 | ||
22 | private static instance: Emailer | |
23 | private initialized = false | |
24 | private transporter: Transporter | |
25 | ||
26 | private constructor () { | |
27 | } | |
28 | ||
29 | init () { | |
30 | // Already initialized | |
31 | if (this.initialized === true) return | |
32 | this.initialized = true | |
33 | ||
34 | if (isEmailEnabled()) { | |
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 | } | |
43 | } | |
44 | ||
45 | let auth | |
46 | if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) { | |
47 | auth = { | |
48 | user: CONFIG.SMTP.USERNAME, | |
49 | pass: CONFIG.SMTP.PASSWORD | |
50 | } | |
51 | } | |
52 | ||
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', | |
69 | path: CONFIG.SMTP.SENDMAIL | |
70 | }) | |
71 | } | |
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 | ||
79 | static isEnabled () { | |
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 | } | |
87 | } | |
88 | ||
89 | async checkConnectionOrDie () { | |
90 | if (!this.transporter || CONFIG.SMTP.TRANSPORT !== 'smtp') return | |
91 | ||
92 | logger.info('Testing SMTP server...') | |
93 | ||
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 | ||
104 | addNewVideoFromSubscriberNotification (to: string[], video: MVideoAccountLight) { | |
105 | const channelName = video.VideoChannel.getDisplayName() | |
106 | const videoUrl = WEBSERVER.URL + video.getWatchStaticPath() | |
107 | ||
108 | const emailPayload: EmailPayload = { | |
109 | to, | |
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 | } | |
119 | } | |
120 | ||
121 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
122 | } | |
123 | ||
124 | addNewFollowNotification (to: string[], actorFollow: MActorFollowFull, followType: 'account' | 'channel') { | |
125 | const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName() | |
126 | ||
127 | const emailPayload: EmailPayload = { | |
128 | template: 'follower-on-channel', | |
129 | to, | |
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 | } | |
138 | } | |
139 | ||
140 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
141 | } | |
142 | ||
143 | addNewInstanceFollowerNotification (to: string[], actorFollow: MActorFollowActors) { | |
144 | const awaitingApproval = actorFollow.state === 'pending' ? ' awaiting manual approval.' : '' | |
145 | ||
146 | const emailPayload: EmailPayload = { | |
147 | to, | |
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 | } | |
157 | } | |
158 | ||
159 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
160 | } | |
161 | ||
162 | addAutoInstanceFollowingNotification (to: string[], actorFollow: MActorFollowActors) { | |
163 | const instanceUrl = actorFollow.ActorFollowing.url | |
164 | const emailPayload: EmailPayload = { | |
165 | to, | |
166 | subject: 'Auto instance following', | |
167 | text: `Your instance automatically followed a new instance: <a href="${instanceUrl}">${instanceUrl}</a>.` | |
168 | } | |
169 | ||
170 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
171 | } | |
172 | ||
173 | myVideoPublishedNotification (to: string[], video: MVideo) { | |
174 | const videoUrl = WEBSERVER.URL + video.getWatchStaticPath() | |
175 | ||
176 | const emailPayload: EmailPayload = { | |
177 | to, | |
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 | } | |
187 | } | |
188 | ||
189 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
190 | } | |
191 | ||
192 | myVideoImportSuccessNotification (to: string[], videoImport: MVideoImportVideo) { | |
193 | const videoUrl = WEBSERVER.URL + videoImport.Video.getWatchStaticPath() | |
194 | ||
195 | const emailPayload: EmailPayload = { | |
196 | to, | |
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 | } | |
206 | } | |
207 | ||
208 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
209 | } | |
210 | ||
211 | myVideoImportErrorNotification (to: string[], videoImport: MVideoImport) { | |
212 | const importUrl = WEBSERVER.URL + '/my-account/video-imports' | |
213 | ||
214 | const text = | |
215 | `Your video import "${videoImport.getTargetIdentifier()}" encountered an error.` + | |
216 | '\n\n' + | |
217 | `See your videos import dashboard for more information: <a href="${importUrl}">${importUrl}</a>.` | |
218 | ||
219 | const emailPayload: EmailPayload = { | |
220 | to, | |
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 | } | |
230 | } | |
231 | ||
232 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
233 | } | |
234 | ||
235 | addNewCommentOnMyVideoNotification (to: string[], comment: MCommentOwnerVideo) { | |
236 | const video = comment.Video | |
237 | const videoUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath() | |
238 | const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath() | |
239 | ||
240 | const emailPayload: EmailPayload = { | |
241 | template: 'video-comment-new', | |
242 | to, | |
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 | } | |
255 | } | |
256 | ||
257 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
258 | } | |
259 | ||
260 | addNewCommentMentionNotification (to: string[], comment: MCommentOwnerVideo) { | |
261 | const accountName = comment.Account.getDisplayName() | |
262 | const video = comment.Video | |
263 | const videoUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath() | |
264 | const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath() | |
265 | ||
266 | const emailPayload: EmailPayload = { | |
267 | template: 'video-comment-mention', | |
268 | to, | |
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 | } | |
280 | } | |
281 | ||
282 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
283 | } | |
284 | ||
285 | addAbuseModeratorsNotification (to: string[], parameters: { | |
286 | abuse: UserAbuse | |
287 | abuseInstance: MAbuseFull | |
288 | reporter: string | |
289 | }) { | |
290 | const { abuse, abuseInstance, reporter } = parameters | |
291 | ||
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, | |
314 | videoChannel: abuse.video.channel, | |
315 | reporter, | |
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 = { | |
324 | template: 'video-comment-abuse-new', | |
325 | to, | |
326 | subject: `New comment abuse report from ${reporter}`, | |
327 | locals: { | |
328 | commentUrl, | |
329 | videoName: comment.Video.name, | |
330 | isLocal: comment.isOwned(), | |
331 | commentCreatedAt: new Date(comment.createdAt).toLocaleString(), | |
332 | reason: abuse.reason, | |
333 | flaggedAccount: abuseInstance.FlaggedAccount.getDisplayName(), | |
334 | reporter, | |
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, | |
351 | reporter, | |
352 | action | |
353 | } | |
354 | } | |
355 | } | |
356 | ||
357 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
358 | } | |
359 | ||
360 | addAbuseStateChangeNotification (to: string[], abuse: MAbuseFull) { | |
361 | const text = abuse.state === AbuseState.ACCEPTED | |
362 | ? 'Report #' + abuse.id + ' has been accepted' | |
363 | : 'Report #' + abuse.id + ' has been rejected' | |
364 | ||
365 | const abuseUrl = WEBSERVER.URL + '/my-account/abuses?search=%23' + abuse.id | |
366 | ||
367 | const action = { | |
368 | text, | |
369 | url: abuseUrl | |
370 | } | |
371 | ||
372 | const emailPayload: EmailPayload = { | |
373 | template: 'abuse-state-change', | |
374 | to, | |
375 | subject: text, | |
376 | locals: { | |
377 | action, | |
378 | abuseId: abuse.id, | |
379 | abuseUrl, | |
380 | isAccepted: abuse.state === AbuseState.ACCEPTED | |
381 | } | |
382 | } | |
383 | ||
384 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
385 | } | |
386 | ||
387 | addAbuseNewMessageNotification ( | |
388 | to: string[], | |
389 | options: { | |
390 | target: 'moderator' | 'reporter' | |
391 | abuse: MAbuseFull | |
392 | message: MAbuseMessage | |
393 | accountMessage: MAccountDefault | |
394 | }) { | |
395 | const { abuse, target, message, accountMessage } = options | |
396 | ||
397 | const text = 'New message on report #' + abuse.id | |
398 | const abuseUrl = target === 'moderator' | |
399 | ? WEBSERVER.URL + '/admin/moderation/abuses/list?search=%23' + abuse.id | |
400 | : WEBSERVER.URL + '/my-account/abuses?search=%23' + abuse.id | |
401 | ||
402 | const action = { | |
403 | text, | |
404 | url: abuseUrl | |
405 | } | |
406 | ||
407 | const emailPayload: EmailPayload = { | |
408 | template: 'abuse-new-message', | |
409 | to, | |
410 | subject: text, | |
411 | locals: { | |
412 | abuseId: abuse.id, | |
413 | abuseUrl: action.url, | |
414 | messageAccountName: accountMessage.getDisplayName(), | |
415 | messageText: message.message, | |
416 | action | |
417 | } | |
418 | } | |
419 | ||
420 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
421 | } | |
422 | ||
423 | async addVideoAutoBlacklistModeratorsNotification (to: string[], videoBlacklist: MVideoBlacklistLightVideo) { | |
424 | const VIDEO_AUTO_BLACKLIST_URL = WEBSERVER.URL + '/admin/moderation/video-auto-blacklist/list' | |
425 | const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath() | |
426 | const channel = (await VideoChannelModel.loadByIdAndPopulateAccount(videoBlacklist.Video.channelId)).toFormattedSummaryJSON() | |
427 | ||
428 | const emailPayload: EmailPayload = { | |
429 | template: 'video-auto-blacklist-new', | |
430 | to, | |
431 | subject: 'A new video is pending moderation', | |
432 | locals: { | |
433 | channel, | |
434 | videoUrl, | |
435 | videoName: videoBlacklist.Video.name, | |
436 | action: { | |
437 | text: 'Review autoblacklist', | |
438 | url: VIDEO_AUTO_BLACKLIST_URL | |
439 | } | |
440 | } | |
441 | } | |
442 | ||
443 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
444 | } | |
445 | ||
446 | addNewUserRegistrationNotification (to: string[], user: MUser) { | |
447 | const emailPayload: EmailPayload = { | |
448 | template: 'user-registered', | |
449 | to, | |
450 | subject: `a new user registered on ${WEBSERVER.HOST}: ${user.username}`, | |
451 | locals: { | |
452 | user | |
453 | } | |
454 | } | |
455 | ||
456 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
457 | } | |
458 | ||
459 | addVideoBlacklistNotification (to: string[], videoBlacklist: MVideoBlacklistVideo) { | |
460 | const videoName = videoBlacklist.Video.name | |
461 | const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath() | |
462 | ||
463 | const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : '' | |
464 | const blockedString = `Your video ${videoName} (${videoUrl} on ${WEBSERVER.HOST} has been blacklisted${reasonString}.` | |
465 | ||
466 | const emailPayload: EmailPayload = { | |
467 | to, | |
468 | subject: `Video ${videoName} blacklisted`, | |
469 | text: blockedString, | |
470 | locals: { | |
471 | title: 'Your video was blacklisted' | |
472 | } | |
473 | } | |
474 | ||
475 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
476 | } | |
477 | ||
478 | addVideoUnblacklistNotification (to: string[], video: MVideo) { | |
479 | const videoUrl = WEBSERVER.URL + video.getWatchStaticPath() | |
480 | ||
481 | const emailPayload: EmailPayload = { | |
482 | to, | |
483 | subject: `Video ${video.name} unblacklisted`, | |
484 | text: `Your video "${video.name}" (${videoUrl}) on ${WEBSERVER.HOST} has been unblacklisted.`, | |
485 | locals: { | |
486 | title: 'Your video was unblacklisted' | |
487 | } | |
488 | } | |
489 | ||
490 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
491 | } | |
492 | ||
493 | addPasswordResetEmailJob (username: string, to: string, resetPasswordUrl: string) { | |
494 | const emailPayload: EmailPayload = { | |
495 | template: 'password-reset', | |
496 | to: [ to ], | |
497 | subject: 'Reset your account password', | |
498 | locals: { | |
499 | username, | |
500 | resetPasswordUrl | |
501 | } | |
502 | } | |
503 | ||
504 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
505 | } | |
506 | ||
507 | addPasswordCreateEmailJob (username: string, to: string, createPasswordUrl: string) { | |
508 | const emailPayload: EmailPayload = { | |
509 | template: 'password-create', | |
510 | to: [ to ], | |
511 | subject: 'Create your account password', | |
512 | locals: { | |
513 | username, | |
514 | createPasswordUrl | |
515 | } | |
516 | } | |
517 | ||
518 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
519 | } | |
520 | ||
521 | addVerifyEmailJob (username: string, to: string, verifyEmailUrl: string) { | |
522 | const emailPayload: EmailPayload = { | |
523 | template: 'verify-email', | |
524 | to: [ to ], | |
525 | subject: `Verify your email on ${WEBSERVER.HOST}`, | |
526 | locals: { | |
527 | username, | |
528 | verifyEmailUrl | |
529 | } | |
530 | } | |
531 | ||
532 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
533 | } | |
534 | ||
535 | addUserBlockJob (user: MUser, blocked: boolean, reason?: string) { | |
536 | const reasonString = reason ? ` for the following reason: ${reason}` : '' | |
537 | const blockedWord = blocked ? 'blocked' : 'unblocked' | |
538 | ||
539 | const to = user.email | |
540 | const emailPayload: EmailPayload = { | |
541 | to: [ to ], | |
542 | subject: 'Account ' + blockedWord, | |
543 | text: `Your account ${user.username} on ${WEBSERVER.HOST} has been ${blockedWord}${reasonString}.` | |
544 | } | |
545 | ||
546 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
547 | } | |
548 | ||
549 | addContactFormJob (fromEmail: string, fromName: string, subject: string, body: string) { | |
550 | const emailPayload: EmailPayload = { | |
551 | template: 'contact-form', | |
552 | to: [ CONFIG.ADMIN.EMAIL ], | |
553 | replyTo: `"${fromName}" <${fromEmail}>`, | |
554 | subject: `(contact form) ${subject}`, | |
555 | locals: { | |
556 | fromName, | |
557 | fromEmail, | |
558 | body | |
559 | } | |
560 | } | |
561 | ||
562 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
563 | } | |
564 | ||
565 | async sendMail (options: EmailPayload) { | |
566 | if (!isEmailEnabled()) { | |
567 | throw new Error('Cannot send mail because SMTP is not configured.') | |
568 | } | |
569 | ||
570 | const fromDisplayName = options.from | |
571 | ? options.from | |
572 | : WEBSERVER.HOST | |
573 | ||
574 | const email = new Email({ | |
575 | send: true, | |
576 | message: { | |
577 | from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>` | |
578 | }, | |
579 | transport: this.transporter, | |
580 | views: { | |
581 | root: join(root(), 'dist', 'server', 'lib', 'emails') | |
582 | }, | |
583 | subjectPrefix: CONFIG.EMAIL.SUBJECT.PREFIX | |
584 | }) | |
585 | ||
586 | for (const to of options.to) { | |
587 | await email | |
588 | .send(merge( | |
589 | { | |
590 | template: 'common', | |
591 | message: { | |
592 | to, | |
593 | from: options.from, | |
594 | subject: options.subject, | |
595 | replyTo: options.replyTo | |
596 | }, | |
597 | locals: { // default variables available in all templates | |
598 | WEBSERVER, | |
599 | EMAIL: CONFIG.EMAIL, | |
600 | text: options.text, | |
601 | subject: options.subject | |
602 | } | |
603 | }, | |
604 | options // overriden/new variables given for a specific template in the payload | |
605 | ) as SendEmailOptions) | |
606 | .then(res => logger.debug('Sent email.', { res })) | |
607 | .catch(err => logger.error('Error in email sender.', { err })) | |
608 | } | |
609 | } | |
610 | ||
611 | private dieOnConnectionFailure (err?: Error) { | |
612 | logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err }) | |
613 | process.exit(-1) | |
614 | } | |
615 | ||
616 | static get Instance () { | |
617 | return this.instance || (this.instance = new this()) | |
618 | } | |
619 | } | |
620 | ||
621 | // --------------------------------------------------------------------------- | |
622 | ||
623 | export { | |
624 | Emailer | |
625 | } |