1 import { createTransport, Transporter } from 'nodemailer'
2 import { isTestInstance } from '../helpers/core-utils'
3 import { bunyanLogger, logger } from '../helpers/logger'
4 import { CONFIG, isEmailEnabled } from '../initializers/config'
5 import { JobQueue } from './job-queue'
6 import { readFileSync } from 'fs-extra'
7 import { WEBSERVER } from '../initializers/constants'
13 MVideoBlacklistLightVideo,
15 } from '../typings/models/video'
16 import { MActorFollowActors, MActorFollowFull, MUser } from '../typings/models'
17 import { MVideoImport, MVideoImportVideo } from '@server/typings/models/video/video-import'
18 import { EmailPayload } from '@shared/models'
22 private static instance: Emailer
23 private initialized = false
24 private transporter: Transporter
26 private constructor () {
30 // Already initialized
31 if (this.initialized === true) return
32 this.initialized = true
34 if (isEmailEnabled()) {
35 if (CONFIG.SMTP.TRANSPORT === 'smtp') {
36 logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
39 if (CONFIG.SMTP.CA_FILE) {
41 ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ]
46 if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) {
48 user: CONFIG.SMTP.USERNAME,
49 pass: CONFIG.SMTP.PASSWORD
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,
64 logger.info('Using sendmail to send emails')
66 this.transporter = createTransport({
69 path: CONFIG.SMTP.SENDMAIL
73 if (!isTestInstance()) {
74 logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!')
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
89 async checkConnectionOrDie () {
90 if (!this.transporter || CONFIG.SMTP.TRANSPORT !== 'smtp') return
92 logger.info('Testing SMTP server...')
95 const success = await this.transporter.verify()
96 if (success !== true) this.dieOnConnectionFailure()
98 logger.info('Successfully connected to SMTP server.')
100 this.dieOnConnectionFailure(err)
104 addNewVideoFromSubscriberNotification (to: string[], video: MVideoAccountLight) {
105 const channelName = video.VideoChannel.getDisplayName()
106 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
108 const text = 'Hi dear user,\n\n' +
109 `Your subscription ${channelName} just published a new video: ${video.name}` +
111 `You can view it on ${videoUrl} ` +
114 `${CONFIG.EMAIL.BODY.SIGNATURE}`
116 const emailPayload: EmailPayload = {
118 subject: CONFIG.EMAIL.SUBJECT.PREFIX + channelName + ' just published a new video',
122 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
125 addNewFollowNotification (to: string[], actorFollow: MActorFollowFull, followType: 'account' | 'channel') {
126 const followerName = actorFollow.ActorFollower.Account.getDisplayName()
127 const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName()
129 const text = 'Hi dear user,\n\n' +
130 `Your ${followType} ${followingName} has a new subscriber: ${followerName}` +
133 `${CONFIG.EMAIL.BODY.SIGNATURE}`
135 const emailPayload: EmailPayload = {
137 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New follower on your channel ' + followingName,
141 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
144 addNewInstanceFollowerNotification (to: string[], actorFollow: MActorFollowActors) {
145 const awaitingApproval = actorFollow.state === 'pending' ? ' awaiting manual approval.' : ''
147 const text = 'Hi dear admin,\n\n' +
148 `Your instance has a new follower: ${actorFollow.ActorFollower.url}${awaitingApproval}` +
151 `${CONFIG.EMAIL.BODY.SIGNATURE}`
153 const emailPayload: EmailPayload = {
155 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New instance follower',
159 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
162 addAutoInstanceFollowingNotification (to: string[], actorFollow: MActorFollowActors) {
163 const text = 'Hi dear admin,\n\n' +
164 `Your instance automatically followed a new instance: ${actorFollow.ActorFollowing.url}` +
167 `${CONFIG.EMAIL.BODY.SIGNATURE}`
169 const emailPayload: EmailPayload = {
171 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Auto instance following',
175 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
178 myVideoPublishedNotification (to: string[], video: MVideo) {
179 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
181 const text = 'Hi dear user,\n\n' +
182 `Your video ${video.name} has been published.` +
184 `You can view it on ${videoUrl} ` +
187 `${CONFIG.EMAIL.BODY.SIGNATURE}`
189 const emailPayload: EmailPayload = {
191 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video ${video.name} is published`,
195 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
198 myVideoImportSuccessNotification (to: string[], videoImport: MVideoImportVideo) {
199 const videoUrl = WEBSERVER.URL + videoImport.Video.getWatchStaticPath()
201 const text = 'Hi dear user,\n\n' +
202 `Your video import ${videoImport.getTargetIdentifier()} is finished.` +
204 `You can view the imported video on ${videoUrl} ` +
207 `${CONFIG.EMAIL.BODY.SIGNATURE}`
209 const emailPayload: EmailPayload = {
211 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} is finished`,
215 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
218 myVideoImportErrorNotification (to: string[], videoImport: MVideoImport) {
219 const importUrl = WEBSERVER.URL + '/my-account/video-imports'
221 const text = 'Hi dear user,\n\n' +
222 `Your video import ${videoImport.getTargetIdentifier()} encountered an error.` +
224 `See your videos import dashboard for more information: ${importUrl}` +
227 `${CONFIG.EMAIL.BODY.SIGNATURE}`
229 const emailPayload: EmailPayload = {
231 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} encountered an error`,
235 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
238 addNewCommentOnMyVideoNotification (to: string[], comment: MCommentOwnerVideo) {
239 const accountName = comment.Account.getDisplayName()
240 const video = comment.Video
241 const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
243 const text = 'Hi dear user,\n\n' +
244 `A new comment has been posted by ${accountName} on your video ${video.name}` +
246 `You can view it on ${commentUrl} ` +
249 `${CONFIG.EMAIL.BODY.SIGNATURE}`
251 const emailPayload: EmailPayload = {
253 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New comment on your video ' + video.name,
257 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
260 addNewCommentMentionNotification (to: string[], comment: MCommentOwnerVideo) {
261 const accountName = comment.Account.getDisplayName()
262 const video = comment.Video
263 const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
265 const text = 'Hi dear user,\n\n' +
266 `${accountName} mentioned you on video ${video.name}` +
268 `You can view the comment on ${commentUrl} ` +
271 `${CONFIG.EMAIL.BODY.SIGNATURE}`
273 const emailPayload: EmailPayload = {
275 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Mention on video ' + video.name,
279 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
282 addVideoAbuseModeratorsNotification (to: string[], videoAbuse: MVideoAbuseVideo) {
283 const videoUrl = WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath()
285 const text = 'Hi,\n\n' +
286 `${WEBSERVER.HOST} received an abuse for the following video: ${videoUrl}\n\n` +
288 `${CONFIG.EMAIL.BODY.SIGNATURE}`
290 const emailPayload: EmailPayload = {
292 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Received a video abuse',
296 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
299 addVideoAutoBlacklistModeratorsNotification (to: string[], videoBlacklist: MVideoBlacklistLightVideo) {
300 const VIDEO_AUTO_BLACKLIST_URL = WEBSERVER.URL + '/admin/moderation/video-auto-blacklist/list'
301 const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
303 const text = 'Hi,\n\n' +
304 'A recently added video was auto-blacklisted and requires moderator review before publishing.' +
306 `You can view it and take appropriate action on ${videoUrl}` +
308 `A full list of auto-blacklisted videos can be reviewed here: ${VIDEO_AUTO_BLACKLIST_URL}` +
311 `${CONFIG.EMAIL.BODY.SIGNATURE}`
313 const emailPayload: EmailPayload = {
315 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'An auto-blacklisted video is awaiting review',
319 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
322 addNewUserRegistrationNotification (to: string[], user: MUser) {
323 const text = 'Hi,\n\n' +
324 `User ${user.username} just registered on ${WEBSERVER.HOST} PeerTube instance.\n\n` +
326 `${CONFIG.EMAIL.BODY.SIGNATURE}`
328 const emailPayload: EmailPayload = {
330 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New user registration on ' + WEBSERVER.HOST,
334 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
337 addVideoBlacklistNotification (to: string[], videoBlacklist: MVideoBlacklistVideo) {
338 const videoName = videoBlacklist.Video.name
339 const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
341 const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : ''
342 const blockedString = `Your video ${videoName} (${videoUrl} on ${WEBSERVER.HOST} has been blacklisted${reasonString}.`
344 const text = 'Hi,\n\n' +
348 `${CONFIG.EMAIL.BODY.SIGNATURE}`
350 const emailPayload: EmailPayload = {
352 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${videoName} blacklisted`,
356 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
359 addVideoUnblacklistNotification (to: string[], video: MVideo) {
360 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
362 const text = 'Hi,\n\n' +
363 `Your video ${video.name} (${videoUrl}) on ${WEBSERVER.HOST} has been unblacklisted.` +
366 `${CONFIG.EMAIL.BODY.SIGNATURE}`
368 const emailPayload: EmailPayload = {
370 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${video.name} unblacklisted`,
374 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
377 addPasswordResetEmailJob (to: string, resetPasswordUrl: string) {
378 const text = 'Hi dear user,\n\n' +
379 `A reset password procedure for your account ${to} has been requested on ${WEBSERVER.HOST} ` +
380 `Please follow this link to reset it: ${resetPasswordUrl} (the link will expire within 1 hour)\n\n` +
381 'If you are not the person who initiated this request, please ignore this email.\n\n' +
383 `${CONFIG.EMAIL.BODY.SIGNATURE}`
385 const emailPayload: EmailPayload = {
387 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Reset your password',
391 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
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` +
399 `${CONFIG.EMAIL.BODY.SIGNATURE}`
401 const emailPayload: EmailPayload = {
403 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New PeerTube account password',
407 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
410 addVerifyEmailJob (to: string, verifyEmailUrl: string) {
411 const text = 'Welcome to PeerTube,\n\n' +
412 `To start using PeerTube on ${WEBSERVER.HOST} you must verify your email! ` +
413 `Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` +
414 'If you are not the person who initiated this request, please ignore this email.\n\n' +
416 `${CONFIG.EMAIL.BODY.SIGNATURE}`
418 const emailPayload: EmailPayload = {
420 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Verify your email',
424 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
427 addUserBlockJob (user: MUser, blocked: boolean, reason?: string) {
428 const reasonString = reason ? ` for the following reason: ${reason}` : ''
429 const blockedWord = blocked ? 'blocked' : 'unblocked'
430 const blockedString = `Your account ${user.username} on ${WEBSERVER.HOST} has been ${blockedWord}${reasonString}.`
432 const text = 'Hi,\n\n' +
436 `${CONFIG.EMAIL.BODY.SIGNATURE}`
438 const to = user.email
439 const emailPayload: EmailPayload = {
441 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Account ' + blockedWord,
445 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
448 addContactFormJob (fromEmail: string, fromName: string, subject: string, body: string) {
449 const text = 'Hello dear admin,\n\n' +
450 fromName + ' sent you a message' +
451 '\n\n---------------------------------------\n\n' +
453 '\n\n---------------------------------------\n\n' +
457 const emailPayload: EmailPayload = {
458 fromDisplayName: fromEmail,
460 to: [ CONFIG.ADMIN.EMAIL ],
461 subject: CONFIG.EMAIL.SUBJECT.PREFIX + subject,
465 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
468 async sendMail (options: EmailPayload) {
469 if (!isEmailEnabled()) {
470 throw new Error('Cannot send mail because SMTP is not configured.')
473 const fromDisplayName = options.fromDisplayName
474 ? options.fromDisplayName
477 for (const to of options.to) {
478 await this.transporter.sendMail({
479 from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>`,
480 replyTo: options.replyTo,
482 subject: options.subject,
488 private dieOnConnectionFailure (err?: Error) {
489 logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err })
493 static get Instance () {
494 return this.instance || (this.instance = new this())
498 // ---------------------------------------------------------------------------