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 () {
39 // Already initialized
40 if (this.initialized === true) return
41 this.initialized = true
43 if (Emailer.isEnabled()) {
44 logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
47 if (CONFIG.SMTP.CA_FILE) {
49 ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ]
54 if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) {
56 user: CONFIG.SMTP.USERNAME,
57 pass: CONFIG.SMTP.PASSWORD
61 this.transporter = createTransport({
62 host: CONFIG.SMTP.HOSTNAME,
63 port: CONFIG.SMTP.PORT,
64 secure: CONFIG.SMTP.TLS,
65 debug: CONFIG.LOG.LEVEL === 'debug',
66 logger: bunyanLogger as any,
67 ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS,
72 if (!isTestInstance()) {
73 logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!')
79 return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT
82 async checkConnectionOrDie () {
83 if (!this.transporter) return
85 logger.info('Testing SMTP server...')
88 const success = await this.transporter.verify()
89 if (success !== true) this.dieOnConnectionFailure()
91 logger.info('Successfully connected to SMTP server.')
93 this.dieOnConnectionFailure(err)
97 addNewVideoFromSubscriberNotification (to: string[], video: MVideoAccountLight) {
98 const channelName = video.VideoChannel.getDisplayName()
99 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
101 const text = 'Hi dear user,\n\n' +
102 `Your subscription ${channelName} just published a new video: ${video.name}` +
104 `You can view it on ${videoUrl} ` +
107 `${CONFIG.EMAIL.BODY.SIGNATURE}`
109 const emailPayload: EmailPayload = {
111 subject: CONFIG.EMAIL.SUBJECT.PREFIX + channelName + ' just published a new video',
115 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
118 addNewFollowNotification (to: string[], actorFollow: MActorFollowFull, followType: 'account' | 'channel') {
119 const followerName = actorFollow.ActorFollower.Account.getDisplayName()
120 const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName()
122 const text = 'Hi dear user,\n\n' +
123 `Your ${followType} ${followingName} has a new subscriber: ${followerName}` +
126 `${CONFIG.EMAIL.BODY.SIGNATURE}`
128 const emailPayload: EmailPayload = {
130 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New follower on your channel ' + followingName,
134 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
137 addNewInstanceFollowerNotification (to: string[], actorFollow: MActorFollowActors) {
138 const awaitingApproval = actorFollow.state === 'pending' ? ' awaiting manual approval.' : ''
140 const text = 'Hi dear admin,\n\n' +
141 `Your instance has a new follower: ${actorFollow.ActorFollower.url}${awaitingApproval}` +
144 `${CONFIG.EMAIL.BODY.SIGNATURE}`
146 const emailPayload: EmailPayload = {
148 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New instance follower',
152 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
155 addAutoInstanceFollowingNotification (to: string[], actorFollow: MActorFollowActors) {
156 const text = 'Hi dear admin,\n\n' +
157 `Your instance automatically followed a new instance: ${actorFollow.ActorFollowing.url}` +
160 `${CONFIG.EMAIL.BODY.SIGNATURE}`
162 const emailPayload: EmailPayload = {
164 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Auto instance following',
168 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
171 myVideoPublishedNotification (to: string[], video: MVideo) {
172 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
174 const text = 'Hi dear user,\n\n' +
175 `Your video ${video.name} has been published.` +
177 `You can view it on ${videoUrl} ` +
180 `${CONFIG.EMAIL.BODY.SIGNATURE}`
182 const emailPayload: EmailPayload = {
184 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video ${video.name} is published`,
188 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
191 myVideoImportSuccessNotification (to: string[], videoImport: MVideoImportVideo) {
192 const videoUrl = WEBSERVER.URL + videoImport.Video.getWatchStaticPath()
194 const text = 'Hi dear user,\n\n' +
195 `Your video import ${videoImport.getTargetIdentifier()} is finished.` +
197 `You can view the imported video on ${videoUrl} ` +
200 `${CONFIG.EMAIL.BODY.SIGNATURE}`
202 const emailPayload: EmailPayload = {
204 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} is finished`,
208 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
211 myVideoImportErrorNotification (to: string[], videoImport: MVideoImport) {
212 const importUrl = WEBSERVER.URL + '/my-account/video-imports'
214 const text = 'Hi dear user,\n\n' +
215 `Your video import ${videoImport.getTargetIdentifier()} encountered an error.` +
217 `See your videos import dashboard for more information: ${importUrl}` +
220 `${CONFIG.EMAIL.BODY.SIGNATURE}`
222 const emailPayload: EmailPayload = {
224 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} encountered an error`,
228 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
231 addNewCommentOnMyVideoNotification (to: string[], comment: MCommentOwnerVideo) {
232 const accountName = comment.Account.getDisplayName()
233 const video = comment.Video
234 const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
236 const text = 'Hi dear user,\n\n' +
237 `A new comment has been posted by ${accountName} on your video ${video.name}` +
239 `You can view it on ${commentUrl} ` +
242 `${CONFIG.EMAIL.BODY.SIGNATURE}`
244 const emailPayload: EmailPayload = {
246 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New comment on your video ' + video.name,
250 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
253 addNewCommentMentionNotification (to: string[], comment: MCommentOwnerVideo) {
254 const accountName = comment.Account.getDisplayName()
255 const video = comment.Video
256 const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
258 const text = 'Hi dear user,\n\n' +
259 `${accountName} mentioned you on video ${video.name}` +
261 `You can view the comment on ${commentUrl} ` +
264 `${CONFIG.EMAIL.BODY.SIGNATURE}`
266 const emailPayload: EmailPayload = {
268 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Mention on video ' + video.name,
272 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
275 addVideoAbuseModeratorsNotification (to: string[], videoAbuse: MVideoAbuseVideo) {
276 const videoUrl = WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath()
278 const text = 'Hi,\n\n' +
279 `${WEBSERVER.HOST} received an abuse for the following video ${videoUrl}\n\n` +
281 `${CONFIG.EMAIL.BODY.SIGNATURE}`
283 const emailPayload: EmailPayload = {
285 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Received a video abuse',
289 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
292 addVideoAutoBlacklistModeratorsNotification (to: string[], videoBlacklist: MVideoBlacklistLightVideo) {
293 const VIDEO_AUTO_BLACKLIST_URL = WEBSERVER.URL + '/admin/moderation/video-auto-blacklist/list'
294 const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
296 const text = 'Hi,\n\n' +
297 'A recently added video was auto-blacklisted and requires moderator review before publishing.' +
299 `You can view it and take appropriate action on ${videoUrl}` +
301 `A full list of auto-blacklisted videos can be reviewed here: ${VIDEO_AUTO_BLACKLIST_URL}` +
304 `${CONFIG.EMAIL.BODY.SIGNATURE}`
306 const emailPayload: EmailPayload = {
308 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'An auto-blacklisted video is awaiting review',
312 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
315 addNewUserRegistrationNotification (to: string[], user: MUser) {
316 const text = 'Hi,\n\n' +
317 `User ${user.username} just registered on ${WEBSERVER.HOST} PeerTube instance.\n\n` +
319 `${CONFIG.EMAIL.BODY.SIGNATURE}`
321 const emailPayload: EmailPayload = {
323 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New user registration on ' + WEBSERVER.HOST,
327 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
330 addVideoBlacklistNotification (to: string[], videoBlacklist: MVideoBlacklistVideo) {
331 const videoName = videoBlacklist.Video.name
332 const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
334 const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : ''
335 const blockedString = `Your video ${videoName} (${videoUrl} on ${WEBSERVER.HOST} has been blacklisted${reasonString}.`
337 const text = 'Hi,\n\n' +
341 `${CONFIG.EMAIL.BODY.SIGNATURE}`
343 const emailPayload: EmailPayload = {
345 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${videoName} blacklisted`,
349 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
352 addVideoUnblacklistNotification (to: string[], video: MVideo) {
353 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
355 const text = 'Hi,\n\n' +
356 `Your video ${video.name} (${videoUrl}) on ${WEBSERVER.HOST} has been unblacklisted.` +
359 `${CONFIG.EMAIL.BODY.SIGNATURE}`
361 const emailPayload: EmailPayload = {
363 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${video.name} unblacklisted`,
367 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
370 addPasswordResetEmailJob (to: string, resetPasswordUrl: string) {
371 const text = 'Hi dear user,\n\n' +
372 `A reset password procedure for your account ${to} has been requested on ${WEBSERVER.HOST} ` +
373 `Please follow this link to reset it: ${resetPasswordUrl} (the link will expire within 1 hour)\n\n` +
374 'If you are not the person who initiated this request, please ignore this email.\n\n' +
376 `${CONFIG.EMAIL.BODY.SIGNATURE}`
378 const emailPayload: EmailPayload = {
380 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Reset your password',
384 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
387 addVerifyEmailJob (to: string, verifyEmailUrl: string) {
388 const text = 'Welcome to PeerTube,\n\n' +
389 `To start using PeerTube on ${WEBSERVER.HOST} you must verify your email! ` +
390 `Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` +
391 'If you are not the person who initiated this request, please ignore this email.\n\n' +
393 `${CONFIG.EMAIL.BODY.SIGNATURE}`
395 const emailPayload: EmailPayload = {
397 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Verify your email',
401 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
404 addUserBlockJob (user: MUser, blocked: boolean, reason?: string) {
405 const reasonString = reason ? ` for the following reason: ${reason}` : ''
406 const blockedWord = blocked ? 'blocked' : 'unblocked'
407 const blockedString = `Your account ${user.username} on ${WEBSERVER.HOST} has been ${blockedWord}${reasonString}.`
409 const text = 'Hi,\n\n' +
413 `${CONFIG.EMAIL.BODY.SIGNATURE}`
415 const to = user.email
416 const emailPayload: EmailPayload = {
418 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Account ' + blockedWord,
422 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
425 addContactFormJob (fromEmail: string, fromName: string, subject: string, body: string) {
426 const text = 'Hello dear admin,\n\n' +
427 fromName + ' sent you a message' +
428 '\n\n---------------------------------------\n\n' +
430 '\n\n---------------------------------------\n\n' +
434 const emailPayload: EmailPayload = {
435 fromDisplayName: fromEmail,
437 to: [ CONFIG.ADMIN.EMAIL ],
438 subject: CONFIG.EMAIL.SUBJECT.PREFIX + subject,
442 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
445 async sendMail (options: EmailPayload) {
446 if (!Emailer.isEnabled()) {
447 throw new Error('Cannot send mail because SMTP is not configured.')
450 const fromDisplayName = options.fromDisplayName
451 ? options.fromDisplayName
454 for (const to of options.to) {
455 await this.transporter.sendMail({
456 from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>`,
457 replyTo: options.replyTo,
459 subject: options.subject,
465 private dieOnConnectionFailure (err?: Error) {
466 logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err })
470 static get Instance () {
471 return this.instance || (this.instance = new this())
475 // ---------------------------------------------------------------------------