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