1 import { createTransport, Transporter } from 'nodemailer'
2 import { isTestInstance } from '../helpers/core-utils'
3 import { bunyanLogger, logger } from '../helpers/logger'
4 import { CONFIG } from '../initializers/config'
5 import { JobQueue } from './job-queue'
6 import { EmailPayload } from './job-queue/handlers/email'
7 import { readFileSync } from 'fs-extra'
8 import { WEBSERVER } from '../initializers/constants'
14 MVideoBlacklistLightVideo,
16 } from '../typings/models/video'
17 import { MActorFollowActors, MActorFollowFull, MUser } from '../typings/models'
18 import { MVideoImport, MVideoImportVideo } from '@server/typings/models/video/video-import'
20 type SendEmailOptions = {
25 fromDisplayName?: string
31 private static instance: Emailer
32 private initialized = false
33 private transporter: Transporter
35 private constructor () {}
38 // Already initialized
39 if (this.initialized === true) return
40 this.initialized = true
42 if (Emailer.isEnabled()) {
43 logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
46 if (CONFIG.SMTP.CA_FILE) {
48 ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ]
53 if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) {
55 user: CONFIG.SMTP.USERNAME,
56 pass: CONFIG.SMTP.PASSWORD
60 this.transporter = createTransport({
61 host: CONFIG.SMTP.HOSTNAME,
62 port: CONFIG.SMTP.PORT,
63 secure: CONFIG.SMTP.TLS,
64 debug: CONFIG.LOG.LEVEL === 'debug',
65 logger: bunyanLogger as any,
66 ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS,
71 if (!isTestInstance()) {
72 logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!')
78 return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT
81 async checkConnectionOrDie () {
82 if (!this.transporter) return
84 logger.info('Testing SMTP server...')
87 const success = await this.transporter.verify()
88 if (success !== true) this.dieOnConnectionFailure()
90 logger.info('Successfully connected to SMTP server.')
92 this.dieOnConnectionFailure(err)
96 addNewVideoFromSubscriberNotification (to: string[], video: MVideoAccountLight) {
97 const channelName = video.VideoChannel.getDisplayName()
98 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
100 const text = `Hi dear user,\n\n` +
101 `Your subscription ${channelName} just published a new video: ${video.name}` +
103 `You can view it on ${videoUrl} ` +
106 `${CONFIG.EMAIL.BODY.SIGNATURE}`
108 const emailPayload: EmailPayload = {
110 subject: CONFIG.EMAIL.SUBJECT.PREFIX + channelName + ' just published a new video',
114 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
117 addNewFollowNotification (to: string[], actorFollow: MActorFollowFull, followType: 'account' | 'channel') {
118 const followerName = actorFollow.ActorFollower.Account.getDisplayName()
119 const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName()
121 const text = `Hi dear user,\n\n` +
122 `Your ${followType} ${followingName} has a new subscriber: ${followerName}` +
125 `${CONFIG.EMAIL.BODY.SIGNATURE}`
127 const emailPayload: EmailPayload = {
129 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New follower on your channel ' + followingName,
133 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
136 addNewInstanceFollowerNotification (to: string[], actorFollow: MActorFollowActors) {
137 const awaitingApproval = actorFollow.state === 'pending' ? ' awaiting manual approval.' : ''
139 const text = `Hi dear admin,\n\n` +
140 `Your instance has a new follower: ${actorFollow.ActorFollower.url}${awaitingApproval}` +
143 `${CONFIG.EMAIL.BODY.SIGNATURE}`
145 const emailPayload: EmailPayload = {
147 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New instance follower',
151 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
154 addAutoInstanceFollowingNotification (to: string[], actorFollow: MActorFollowActors) {
155 const text = `Hi dear admin,\n\n` +
156 `Your instance automatically followed a new instance: ${actorFollow.ActorFollowing.url}` +
159 `${CONFIG.EMAIL.BODY.SIGNATURE}`
161 const emailPayload: EmailPayload = {
163 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Auto instance following',
167 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
170 myVideoPublishedNotification (to: string[], video: MVideo) {
171 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
173 const text = `Hi dear user,\n\n` +
174 `Your video ${video.name} has been published.` +
176 `You can view it on ${videoUrl} ` +
179 `${CONFIG.EMAIL.BODY.SIGNATURE}`
181 const emailPayload: EmailPayload = {
183 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video ${video.name} is published`,
187 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
190 myVideoImportSuccessNotification (to: string[], videoImport: MVideoImportVideo) {
191 const videoUrl = WEBSERVER.URL + videoImport.Video.getWatchStaticPath()
193 const text = `Hi dear user,\n\n` +
194 `Your video import ${videoImport.getTargetIdentifier()} is finished.` +
196 `You can view the imported video on ${videoUrl} ` +
199 `${CONFIG.EMAIL.BODY.SIGNATURE}`
201 const emailPayload: EmailPayload = {
203 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} is finished`,
207 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
210 myVideoImportErrorNotification (to: string[], videoImport: MVideoImport) {
211 const importUrl = WEBSERVER.URL + '/my-account/video-imports'
213 const text = `Hi dear user,\n\n` +
214 `Your video import ${videoImport.getTargetIdentifier()} encountered an error.` +
216 `See your videos import dashboard for more information: ${importUrl}` +
219 `${CONFIG.EMAIL.BODY.SIGNATURE}`
221 const emailPayload: EmailPayload = {
223 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} encountered an error`,
227 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
230 addNewCommentOnMyVideoNotification (to: string[], comment: MCommentOwnerVideo) {
231 const accountName = comment.Account.getDisplayName()
232 const video = comment.Video
233 const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
235 const text = `Hi dear user,\n\n` +
236 `A new comment has been posted by ${accountName} on your video ${video.name}` +
238 `You can view it on ${commentUrl} ` +
241 `${CONFIG.EMAIL.BODY.SIGNATURE}`
243 const emailPayload: EmailPayload = {
245 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New comment on your video ' + video.name,
249 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
252 addNewCommentMentionNotification (to: string[], comment: MCommentOwnerVideo) {
253 const accountName = comment.Account.getDisplayName()
254 const video = comment.Video
255 const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
257 const text = `Hi dear user,\n\n` +
258 `${accountName} mentioned you on video ${video.name}` +
260 `You can view the comment on ${commentUrl} ` +
263 `${CONFIG.EMAIL.BODY.SIGNATURE}`
265 const emailPayload: EmailPayload = {
267 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Mention on video ' + video.name,
271 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
274 addVideoAbuseModeratorsNotification (to: string[], videoAbuse: MVideoAbuseVideo) {
275 const videoUrl = WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath()
277 const text = `Hi,\n\n` +
278 `${WEBSERVER.HOST} received an abuse for the following video ${videoUrl}\n\n` +
280 `${CONFIG.EMAIL.BODY.SIGNATURE}`
282 const emailPayload: EmailPayload = {
284 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Received a video abuse',
288 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
291 addVideoAutoBlacklistModeratorsNotification (to: string[], videoBlacklist: MVideoBlacklistLightVideo) {
292 const VIDEO_AUTO_BLACKLIST_URL = WEBSERVER.URL + '/admin/moderation/video-auto-blacklist/list'
293 const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
295 const text = `Hi,\n\n` +
296 `A recently added video was auto-blacklisted and requires moderator review before publishing.` +
298 `You can view it and take appropriate action on ${videoUrl}` +
300 `A full list of auto-blacklisted videos can be reviewed here: ${VIDEO_AUTO_BLACKLIST_URL}` +
303 `${CONFIG.EMAIL.BODY.SIGNATURE}`
305 const emailPayload: EmailPayload = {
307 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'An auto-blacklisted video is awaiting review',
311 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
314 addNewUserRegistrationNotification (to: string[], user: MUser) {
315 const text = `Hi,\n\n` +
316 `User ${user.username} just registered on ${WEBSERVER.HOST} PeerTube instance.\n\n` +
318 `${CONFIG.EMAIL.BODY.SIGNATURE}`
320 const emailPayload: EmailPayload = {
322 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New user registration on ' + WEBSERVER.HOST,
326 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
329 addVideoBlacklistNotification (to: string[], videoBlacklist: MVideoBlacklistVideo) {
330 const videoName = videoBlacklist.Video.name
331 const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
333 const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : ''
334 const blockedString = `Your video ${videoName} (${videoUrl} on ${WEBSERVER.HOST} has been blacklisted${reasonString}.`
336 const text = 'Hi,\n\n' +
340 `${CONFIG.EMAIL.BODY.SIGNATURE}`
342 const emailPayload: EmailPayload = {
344 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${videoName} blacklisted`,
348 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
351 addVideoUnblacklistNotification (to: string[], video: MVideo) {
352 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
354 const text = 'Hi,\n\n' +
355 `Your video ${video.name} (${videoUrl}) on ${WEBSERVER.HOST} has been unblacklisted.` +
358 `${CONFIG.EMAIL.BODY.SIGNATURE}`
360 const emailPayload: EmailPayload = {
362 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${video.name} unblacklisted`,
366 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
369 addPasswordResetEmailJob (to: string, resetPasswordUrl: string) {
370 const text = `Hi dear user,\n\n` +
371 `A reset password procedure for your account ${to} has been requested on ${WEBSERVER.HOST} ` +
372 `Please follow this link to reset it: ${resetPasswordUrl} (the link will expire within 1 hour)\n\n` +
373 `If you are not the person who initiated this request, please ignore this email.\n\n` +
375 `${CONFIG.EMAIL.BODY.SIGNATURE}`
377 const emailPayload: EmailPayload = {
379 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Reset your password',
383 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
386 addVerifyEmailJob (to: string, verifyEmailUrl: string) {
387 const text = `Welcome to PeerTube,\n\n` +
388 `To start using PeerTube on ${WEBSERVER.HOST} you must verify your email! ` +
389 `Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` +
390 `If you are not the person who initiated this request, please ignore this email.\n\n` +
392 `${CONFIG.EMAIL.BODY.SIGNATURE}`
394 const emailPayload: EmailPayload = {
396 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Verify your email',
400 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
403 addUserBlockJob (user: MUser, blocked: boolean, reason?: string) {
404 const reasonString = reason ? ` for the following reason: ${reason}` : ''
405 const blockedWord = blocked ? 'blocked' : 'unblocked'
406 const blockedString = `Your account ${user.username} on ${WEBSERVER.HOST} has been ${blockedWord}${reasonString}.`
408 const text = 'Hi,\n\n' +
412 `${CONFIG.EMAIL.BODY.SIGNATURE}`
414 const to = user.email
415 const emailPayload: EmailPayload = {
417 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Account ' + blockedWord,
421 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
424 addContactFormJob (fromEmail: string, fromName: string, subject: string, body: string) {
425 const text = 'Hello dear admin,\n\n' +
426 fromName + ' sent you a message' +
427 '\n\n---------------------------------------\n\n' +
429 '\n\n---------------------------------------\n\n' +
433 const emailPayload: EmailPayload = {
434 fromDisplayName: fromEmail,
436 to: [ CONFIG.ADMIN.EMAIL ],
437 subject: CONFIG.EMAIL.SUBJECT.PREFIX + subject,
441 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
444 async sendMail (options: EmailPayload) {
445 if (!Emailer.isEnabled()) {
446 throw new Error('Cannot send mail because SMTP is not configured.')
449 const fromDisplayName = options.fromDisplayName
450 ? options.fromDisplayName
453 for (const to of options.to) {
454 await this.transporter.sendMail({
455 from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>`,
456 replyTo: options.replyTo,
458 subject: options.subject,
464 private dieOnConnectionFailure (err?: Error) {
465 logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err })
469 static get Instance () {
470 return this.instance || (this.instance = new this())
474 // ---------------------------------------------------------------------------