]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/emailer.ts
Add plugin ldap tests
[github/Chocobozzz/PeerTube.git] / server / lib / emailer.ts
CommitLineData
ecb4e35f
C
1import { createTransport, Transporter } from 'nodemailer'
2import { isTestInstance } from '../helpers/core-utils'
05e67d62 3import { bunyanLogger, logger } from '../helpers/logger'
4c1c1709 4import { CONFIG, isEmailEnabled } from '../initializers/config'
ecb4e35f 5import { JobQueue } from './job-queue'
c9d5c64f 6import { readFileSync } from 'fs-extra'
6dd9de95 7import { WEBSERVER } from '../initializers/constants'
8424c402
C
8import {
9 MCommentOwnerVideo,
10 MVideo,
11 MVideoAbuseVideo,
12 MVideoAccountLight,
13 MVideoBlacklistLightVideo,
14 MVideoBlacklistVideo
15} from '../typings/models/video'
16import { MActorFollowActors, MActorFollowFull, MUser } from '../typings/models'
453e83ea 17import { MVideoImport, MVideoImportVideo } from '@server/typings/models/video/video-import'
8dc8a34e 18import { EmailPayload } from '@shared/models'
dee77e76 19
ecb4e35f
C
20class 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
a1587156 108 const text = 'Hi dear user,\n\n' +
cef534ed 109 `Your subscription ${channelName} just published a new video: ${video.name}` +
a1587156 110 '\n\n' +
cef534ed 111 `You can view it on ${videoUrl} ` +
a1587156
C
112 '\n\n' +
113 'Cheers,\n' +
b5bfadf0 114 `${CONFIG.EMAIL.BODY.SIGNATURE}`
ecb4e35f
C
115
116 const emailPayload: EmailPayload = {
cef534ed 117 to,
916937d7 118 subject: CONFIG.EMAIL.SUBJECT.PREFIX + channelName + ' just published a new video',
ecb4e35f
C
119 text
120 }
121
122 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
123 }
124
8424c402 125 addNewFollowNotification (to: string[], actorFollow: MActorFollowFull, followType: 'account' | 'channel') {
f7cc67b4
C
126 const followerName = actorFollow.ActorFollower.Account.getDisplayName()
127 const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName()
128
a1587156 129 const text = 'Hi dear user,\n\n' +
f7cc67b4 130 `Your ${followType} ${followingName} has a new subscriber: ${followerName}` +
a1587156
C
131 '\n\n' +
132 'Cheers,\n' +
b5bfadf0 133 `${CONFIG.EMAIL.BODY.SIGNATURE}`
f7cc67b4
C
134
135 const emailPayload: EmailPayload = {
136 to,
916937d7 137 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New follower on your channel ' + followingName,
f7cc67b4
C
138 text
139 }
140
141 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
142 }
143
453e83ea 144 addNewInstanceFollowerNotification (to: string[], actorFollow: MActorFollowActors) {
883993c8
C
145 const awaitingApproval = actorFollow.state === 'pending' ? ' awaiting manual approval.' : ''
146
a1587156 147 const text = 'Hi dear admin,\n\n' +
883993c8 148 `Your instance has a new follower: ${actorFollow.ActorFollower.url}${awaitingApproval}` +
a1587156
C
149 '\n\n' +
150 'Cheers,\n' +
b5bfadf0 151 `${CONFIG.EMAIL.BODY.SIGNATURE}`
883993c8
C
152
153 const emailPayload: EmailPayload = {
154 to,
916937d7 155 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New instance follower',
883993c8
C
156 text
157 }
158
159 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
160 }
161
8424c402 162 addAutoInstanceFollowingNotification (to: string[], actorFollow: MActorFollowActors) {
a1587156 163 const text = 'Hi dear admin,\n\n' +
8424c402 164 `Your instance automatically followed a new instance: ${actorFollow.ActorFollowing.url}` +
a1587156
C
165 '\n\n' +
166 'Cheers,\n' +
8424c402
C
167 `${CONFIG.EMAIL.BODY.SIGNATURE}`
168
169 const emailPayload: EmailPayload = {
170 to,
171 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Auto instance following',
172 text
173 }
174
175 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
176 }
177
453e83ea 178 myVideoPublishedNotification (to: string[], video: MVideo) {
6dd9de95 179 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
dc133480 180
a1587156 181 const text = 'Hi dear user,\n\n' +
dc133480 182 `Your video ${video.name} has been published.` +
a1587156 183 '\n\n' +
dc133480 184 `You can view it on ${videoUrl} ` +
a1587156
C
185 '\n\n' +
186 'Cheers,\n' +
b5bfadf0 187 `${CONFIG.EMAIL.BODY.SIGNATURE}`
dc133480
C
188
189 const emailPayload: EmailPayload = {
190 to,
916937d7 191 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video ${video.name} is published`,
dc133480
C
192 text
193 }
194
195 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
196 }
197
453e83ea 198 myVideoImportSuccessNotification (to: string[], videoImport: MVideoImportVideo) {
6dd9de95 199 const videoUrl = WEBSERVER.URL + videoImport.Video.getWatchStaticPath()
dc133480 200
a1587156 201 const text = 'Hi dear user,\n\n' +
dc133480 202 `Your video import ${videoImport.getTargetIdentifier()} is finished.` +
a1587156 203 '\n\n' +
dc133480 204 `You can view the imported video on ${videoUrl} ` +
a1587156
C
205 '\n\n' +
206 'Cheers,\n' +
b5bfadf0 207 `${CONFIG.EMAIL.BODY.SIGNATURE}`
dc133480
C
208
209 const emailPayload: EmailPayload = {
210 to,
916937d7 211 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} is finished`,
dc133480
C
212 text
213 }
214
215 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
216 }
217
453e83ea 218 myVideoImportErrorNotification (to: string[], videoImport: MVideoImport) {
6dd9de95 219 const importUrl = WEBSERVER.URL + '/my-account/video-imports'
dc133480 220
a1587156 221 const text = 'Hi dear user,\n\n' +
dc133480 222 `Your video import ${videoImport.getTargetIdentifier()} encountered an error.` +
a1587156 223 '\n\n' +
dc133480 224 `See your videos import dashboard for more information: ${importUrl}` +
a1587156
C
225 '\n\n' +
226 'Cheers,\n' +
b5bfadf0 227 `${CONFIG.EMAIL.BODY.SIGNATURE}`
dc133480
C
228
229 const emailPayload: EmailPayload = {
230 to,
916937d7 231 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} encountered an error`,
dc133480
C
232 text
233 }
234
235 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
236 }
237
453e83ea 238 addNewCommentOnMyVideoNotification (to: string[], comment: MCommentOwnerVideo) {
cef534ed
C
239 const accountName = comment.Account.getDisplayName()
240 const video = comment.Video
6dd9de95 241 const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
cef534ed 242
a1587156 243 const text = 'Hi dear user,\n\n' +
cef534ed 244 `A new comment has been posted by ${accountName} on your video ${video.name}` +
a1587156 245 '\n\n' +
cef534ed 246 `You can view it on ${commentUrl} ` +
a1587156
C
247 '\n\n' +
248 'Cheers,\n' +
b5bfadf0 249 `${CONFIG.EMAIL.BODY.SIGNATURE}`
d9eaee39
JM
250
251 const emailPayload: EmailPayload = {
cef534ed 252 to,
916937d7 253 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New comment on your video ' + video.name,
d9eaee39
JM
254 text
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
6dd9de95 263 const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
f7cc67b4 264
a1587156 265 const text = 'Hi dear user,\n\n' +
f7cc67b4 266 `${accountName} mentioned you on video ${video.name}` +
a1587156 267 '\n\n' +
f7cc67b4 268 `You can view the comment on ${commentUrl} ` +
a1587156
C
269 '\n\n' +
270 'Cheers,\n' +
b5bfadf0 271 `${CONFIG.EMAIL.BODY.SIGNATURE}`
f7cc67b4
C
272
273 const emailPayload: EmailPayload = {
274 to,
916937d7 275 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Mention on video ' + video.name,
f7cc67b4
C
276 text
277 }
278
279 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
280 }
281
453e83ea 282 addVideoAbuseModeratorsNotification (to: string[], videoAbuse: MVideoAbuseVideo) {
6dd9de95 283 const videoUrl = WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath()
ba75d268 284
a1587156 285 const text = 'Hi,\n\n' +
86521a67 286 `${WEBSERVER.HOST} received an abuse for the following video: ${videoUrl}\n\n` +
a1587156 287 'Cheers,\n' +
b5bfadf0 288 `${CONFIG.EMAIL.BODY.SIGNATURE}`
ba75d268 289
ba75d268
C
290 const emailPayload: EmailPayload = {
291 to,
916937d7 292 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Received a video abuse',
ba75d268
C
293 text
294 }
295
296 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
297 }
298
8424c402 299 addVideoAutoBlacklistModeratorsNotification (to: string[], videoBlacklist: MVideoBlacklistLightVideo) {
6dd9de95 300 const VIDEO_AUTO_BLACKLIST_URL = WEBSERVER.URL + '/admin/moderation/video-auto-blacklist/list'
8424c402 301 const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
7ccddd7b 302
a1587156
C
303 const text = 'Hi,\n\n' +
304 'A recently added video was auto-blacklisted and requires moderator review before publishing.' +
305 '\n\n' +
7ccddd7b 306 `You can view it and take appropriate action on ${videoUrl}` +
a1587156 307 '\n\n' +
7ccddd7b 308 `A full list of auto-blacklisted videos can be reviewed here: ${VIDEO_AUTO_BLACKLIST_URL}` +
a1587156
C
309 '\n\n' +
310 'Cheers,\n' +
b5bfadf0 311 `${CONFIG.EMAIL.BODY.SIGNATURE}`
7ccddd7b
JM
312
313 const emailPayload: EmailPayload = {
314 to,
916937d7 315 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'An auto-blacklisted video is awaiting review',
7ccddd7b
JM
316 text
317 }
318
319 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
320 }
321
453e83ea 322 addNewUserRegistrationNotification (to: string[], user: MUser) {
a1587156 323 const text = 'Hi,\n\n' +
6dd9de95 324 `User ${user.username} just registered on ${WEBSERVER.HOST} PeerTube instance.\n\n` +
a1587156 325 'Cheers,\n' +
b5bfadf0 326 `${CONFIG.EMAIL.BODY.SIGNATURE}`
f7cc67b4
C
327
328 const emailPayload: EmailPayload = {
329 to,
916937d7 330 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New user registration on ' + WEBSERVER.HOST,
f7cc67b4
C
331 text
332 }
333
334 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
335 }
336
453e83ea 337 addVideoBlacklistNotification (to: string[], videoBlacklist: MVideoBlacklistVideo) {
cef534ed 338 const videoName = videoBlacklist.Video.name
6dd9de95 339 const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
26b7305a 340
cef534ed 341 const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : ''
6dd9de95 342 const blockedString = `Your video ${videoName} (${videoUrl} on ${WEBSERVER.HOST} has been blacklisted${reasonString}.`
26b7305a
C
343
344 const text = 'Hi,\n\n' +
345 blockedString +
346 '\n\n' +
347 'Cheers,\n' +
b5bfadf0 348 `${CONFIG.EMAIL.BODY.SIGNATURE}`
26b7305a 349
26b7305a 350 const emailPayload: EmailPayload = {
cef534ed 351 to,
916937d7 352 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${videoName} blacklisted`,
26b7305a
C
353 text
354 }
355
356 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
357 }
358
453e83ea 359 addVideoUnblacklistNotification (to: string[], video: MVideo) {
6dd9de95 360 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
26b7305a
C
361
362 const text = 'Hi,\n\n' +
6dd9de95 363 `Your video ${video.name} (${videoUrl}) on ${WEBSERVER.HOST} has been unblacklisted.` +
26b7305a
C
364 '\n\n' +
365 'Cheers,\n' +
b5bfadf0 366 `${CONFIG.EMAIL.BODY.SIGNATURE}`
26b7305a 367
26b7305a 368 const emailPayload: EmailPayload = {
cef534ed 369 to,
916937d7 370 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${video.name} unblacklisted`,
26b7305a
C
371 text
372 }
373
374 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
375 }
376
b426edd4 377 addPasswordResetEmailJob (to: string, resetPasswordUrl: string) {
a1587156 378 const text = 'Hi dear user,\n\n' +
6dd9de95 379 `A reset password procedure for your account ${to} has been requested on ${WEBSERVER.HOST} ` +
f88ee4a9 380 `Please follow this link to reset it: ${resetPasswordUrl} (the link will expire within 1 hour)\n\n` +
a1587156
C
381 'If you are not the person who initiated this request, please ignore this email.\n\n' +
382 'Cheers,\n' +
b5bfadf0 383 `${CONFIG.EMAIL.BODY.SIGNATURE}`
cef534ed
C
384
385 const emailPayload: EmailPayload = {
386 to: [ to ],
916937d7 387 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Reset your password',
cef534ed
C
388 text
389 }
390
391 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
392 }
393
45f1bd72
JL
394 addPasswordCreateEmailJob (username: string, to: string, resetPasswordUrl: string) {
395 const text = 'Hi,\n\n' +
396 `Welcome to your ${WEBSERVER.HOST} PeerTube instance. Your username is: ${username}.\n\n` +
397 `Please set your password by following this link: ${resetPasswordUrl} (this link will expire within seven days).\n\n` +
398 'Cheers,\n' +
399 `${CONFIG.EMAIL.BODY.SIGNATURE}`
400
401 const emailPayload: EmailPayload = {
402 to: [ to ],
403 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New PeerTube account password',
404 text
405 }
406
407 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
408 }
409
cef534ed 410 addVerifyEmailJob (to: string, verifyEmailUrl: string) {
a1587156 411 const text = 'Welcome to PeerTube,\n\n' +
6dd9de95 412 `To start using PeerTube on ${WEBSERVER.HOST} you must verify your email! ` +
cef534ed 413 `Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` +
a1587156
C
414 'If you are not the person who initiated this request, please ignore this email.\n\n' +
415 'Cheers,\n' +
b5bfadf0 416 `${CONFIG.EMAIL.BODY.SIGNATURE}`
cef534ed
C
417
418 const emailPayload: EmailPayload = {
419 to: [ to ],
916937d7 420 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Verify your email',
cef534ed
C
421 text
422 }
423
424 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
425 }
426
453e83ea 427 addUserBlockJob (user: MUser, blocked: boolean, reason?: string) {
eacb25c4
C
428 const reasonString = reason ? ` for the following reason: ${reason}` : ''
429 const blockedWord = blocked ? 'blocked' : 'unblocked'
6dd9de95 430 const blockedString = `Your account ${user.username} on ${WEBSERVER.HOST} has been ${blockedWord}${reasonString}.`
eacb25c4
C
431
432 const text = 'Hi,\n\n' +
433 blockedString +
434 '\n\n' +
435 'Cheers,\n' +
b5bfadf0 436 `${CONFIG.EMAIL.BODY.SIGNATURE}`
eacb25c4
C
437
438 const to = user.email
439 const emailPayload: EmailPayload = {
440 to: [ to ],
916937d7 441 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Account ' + blockedWord,
eacb25c4
C
442 text
443 }
444
445 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
446 }
447
4e9fa5b7 448 addContactFormJob (fromEmail: string, fromName: string, subject: string, body: string) {
a4101923
C
449 const text = 'Hello dear admin,\n\n' +
450 fromName + ' sent you a message' +
451 '\n\n---------------------------------------\n\n' +
452 body +
453 '\n\n---------------------------------------\n\n' +
454 'Cheers,\n' +
455 'PeerTube.'
456
457 const emailPayload: EmailPayload = {
4759fedc
C
458 fromDisplayName: fromEmail,
459 replyTo: fromEmail,
a4101923 460 to: [ CONFIG.ADMIN.EMAIL ],
916937d7 461 subject: CONFIG.EMAIL.SUBJECT.PREFIX + subject,
a4101923
C
462 text
463 }
464
465 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
466 }
467
47f6cb31 468 async sendMail (options: EmailPayload) {
4c1c1709 469 if (!isEmailEnabled()) {
ecb4e35f
C
470 throw new Error('Cannot send mail because SMTP is not configured.')
471 }
472
4759fedc
C
473 const fromDisplayName = options.fromDisplayName
474 ? options.fromDisplayName
6dd9de95 475 : WEBSERVER.HOST
4759fedc 476
47f6cb31
C
477 for (const to of options.to) {
478 await this.transporter.sendMail({
479 from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>`,
480 replyTo: options.replyTo,
481 to,
482 subject: options.subject,
483 text: options.text
484 })
485 }
ecb4e35f
C
486 }
487
488 private dieOnConnectionFailure (err?: Error) {
d5b7d911 489 logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err })
ecb4e35f
C
490 process.exit(-1)
491 }
492
493 static get Instance () {
494 return this.instance || (this.instance = new this())
495 }
496}
497
498// ---------------------------------------------------------------------------
499
500export {
8dc8a34e 501 Emailer
ecb4e35f 502}