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'
5 import { UserModel } from '../models/account/user'
6 import { VideoModel } from '../models/video/video'
7 import { JobQueue } from './job-queue'
8 import { EmailPayload } from './job-queue/handlers/email'
9 import { readFileSync } from 'fs-extra'
10 import { VideoCommentModel } from '../models/video/video-comment'
11 import { VideoAbuseModel } from '../models/video/video-abuse'
12 import { VideoBlacklistModel } from '../models/video/video-blacklist'
13 import { VideoImportModel } from '../models/video/video-import'
14 import { ActorFollowModel } from '../models/activitypub/actor-follow'
18 private static instance: Emailer
19 private initialized = false
20 private transporter: Transporter
21 private enabled = false
23 private constructor () {}
26 // Already initialized
27 if (this.initialized === true) return
28 this.initialized = true
30 if (CONFIG.SMTP.HOSTNAME && CONFIG.SMTP.PORT) {
31 logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
34 if (CONFIG.SMTP.CA_FILE) {
36 ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ]
41 if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) {
43 user: CONFIG.SMTP.USERNAME,
44 pass: CONFIG.SMTP.PASSWORD
48 this.transporter = createTransport({
49 host: CONFIG.SMTP.HOSTNAME,
50 port: CONFIG.SMTP.PORT,
51 secure: CONFIG.SMTP.TLS,
52 debug: CONFIG.LOG.LEVEL === 'debug',
53 logger: bunyanLogger as any,
54 ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS,
61 if (!isTestInstance()) {
62 logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!')
71 async checkConnectionOrDie () {
72 if (!this.transporter) return
74 logger.info('Testing SMTP server...')
77 const success = await this.transporter.verify()
78 if (success !== true) this.dieOnConnectionFailure()
80 logger.info('Successfully connected to SMTP server.')
82 this.dieOnConnectionFailure(err)
86 addNewVideoFromSubscriberNotification (to: string[], video: VideoModel) {
87 const channelName = video.VideoChannel.getDisplayName()
88 const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath()
90 const text = `Hi dear user,\n\n` +
91 `Your subscription ${channelName} just published a new video: ${video.name}` +
93 `You can view it on ${videoUrl} ` +
98 const emailPayload: EmailPayload = {
100 subject: channelName + ' just published a new video',
104 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
107 addNewFollowNotification (to: string[], actorFollow: ActorFollowModel, followType: 'account' | 'channel') {
108 const followerName = actorFollow.ActorFollower.Account.getDisplayName()
109 const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName()
111 const text = `Hi dear user,\n\n` +
112 `Your ${followType} ${followingName} has a new subscriber: ${followerName}` +
117 const emailPayload: EmailPayload = {
119 subject: 'New follower on your channel ' + followingName,
123 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
126 myVideoPublishedNotification (to: string[], video: VideoModel) {
127 const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath()
129 const text = `Hi dear user,\n\n` +
130 `Your video ${video.name} has been published.` +
132 `You can view it on ${videoUrl} ` +
137 const emailPayload: EmailPayload = {
139 subject: `Your video ${video.name} is published`,
143 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
146 myVideoImportSuccessNotification (to: string[], videoImport: VideoImportModel) {
147 const videoUrl = CONFIG.WEBSERVER.URL + videoImport.Video.getWatchStaticPath()
149 const text = `Hi dear user,\n\n` +
150 `Your video import ${videoImport.getTargetIdentifier()} is finished.` +
152 `You can view the imported video on ${videoUrl} ` +
157 const emailPayload: EmailPayload = {
159 subject: `Your video import ${videoImport.getTargetIdentifier()} is finished`,
163 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
166 myVideoImportErrorNotification (to: string[], videoImport: VideoImportModel) {
167 const importUrl = CONFIG.WEBSERVER.URL + '/my-account/video-imports'
169 const text = `Hi dear user,\n\n` +
170 `Your video import ${videoImport.getTargetIdentifier()} encountered an error.` +
172 `See your videos import dashboard for more information: ${importUrl}` +
177 const emailPayload: EmailPayload = {
179 subject: `Your video import ${videoImport.getTargetIdentifier()} encountered an error`,
183 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
186 addNewCommentOnMyVideoNotification (to: string[], comment: VideoCommentModel) {
187 const accountName = comment.Account.getDisplayName()
188 const video = comment.Video
189 const commentUrl = CONFIG.WEBSERVER.URL + comment.getCommentStaticPath()
191 const text = `Hi dear user,\n\n` +
192 `A new comment has been posted by ${accountName} on your video ${video.name}` +
194 `You can view it on ${commentUrl} ` +
199 const emailPayload: EmailPayload = {
201 subject: 'New comment on your video ' + video.name,
205 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
208 addNewCommentMentionNotification (to: string[], comment: VideoCommentModel) {
209 const accountName = comment.Account.getDisplayName()
210 const video = comment.Video
211 const commentUrl = CONFIG.WEBSERVER.URL + comment.getCommentStaticPath()
213 const text = `Hi dear user,\n\n` +
214 `${accountName} mentioned you on video ${video.name}` +
216 `You can view the comment on ${commentUrl} ` +
221 const emailPayload: EmailPayload = {
223 subject: 'Mention on video ' + video.name,
227 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
230 addVideoAbuseModeratorsNotification (to: string[], videoAbuse: VideoAbuseModel) {
231 const videoUrl = CONFIG.WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath()
233 const text = `Hi,\n\n` +
234 `${CONFIG.WEBSERVER.HOST} received an abuse for the following video ${videoUrl}\n\n` +
238 const emailPayload: EmailPayload = {
240 subject: '[PeerTube] Received a video abuse',
244 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
247 addNewUserRegistrationNotification (to: string[], user: UserModel) {
248 const text = `Hi,\n\n` +
249 `User ${user.username} just registered on ${CONFIG.WEBSERVER.HOST} PeerTube instance.\n\n` +
253 const emailPayload: EmailPayload = {
255 subject: '[PeerTube] New user registration on ' + CONFIG.WEBSERVER.HOST,
259 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
262 addVideoBlacklistNotification (to: string[], videoBlacklist: VideoBlacklistModel) {
263 const videoName = videoBlacklist.Video.name
264 const videoUrl = CONFIG.WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
266 const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : ''
267 const blockedString = `Your video ${videoName} (${videoUrl} on ${CONFIG.WEBSERVER.HOST} has been blacklisted${reasonString}.`
269 const text = 'Hi,\n\n' +
275 const emailPayload: EmailPayload = {
277 subject: `[PeerTube] Video ${videoName} blacklisted`,
281 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
284 addVideoUnblacklistNotification (to: string[], video: VideoModel) {
285 const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath()
287 const text = 'Hi,\n\n' +
288 `Your video ${video.name} (${videoUrl}) on ${CONFIG.WEBSERVER.HOST} has been unblacklisted.` +
293 const emailPayload: EmailPayload = {
295 subject: `[PeerTube] Video ${video.name} unblacklisted`,
299 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
302 addForgetPasswordEmailJob (to: string, resetPasswordUrl: string) {
303 const text = `Hi dear user,\n\n` +
304 `It seems you forgot your password on ${CONFIG.WEBSERVER.HOST}! ` +
305 `Please follow this link to reset it: ${resetPasswordUrl}\n\n` +
306 `If you are not the person who initiated this request, please ignore this email.\n\n` +
310 const emailPayload: EmailPayload = {
312 subject: 'Reset your PeerTube password',
316 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
319 addVerifyEmailJob (to: string, verifyEmailUrl: string) {
320 const text = `Welcome to PeerTube,\n\n` +
321 `To start using PeerTube on ${CONFIG.WEBSERVER.HOST} you must verify your email! ` +
322 `Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` +
323 `If you are not the person who initiated this request, please ignore this email.\n\n` +
327 const emailPayload: EmailPayload = {
329 subject: 'Verify your PeerTube email',
333 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
336 addUserBlockJob (user: UserModel, blocked: boolean, reason?: string) {
337 const reasonString = reason ? ` for the following reason: ${reason}` : ''
338 const blockedWord = blocked ? 'blocked' : 'unblocked'
339 const blockedString = `Your account ${user.username} on ${CONFIG.WEBSERVER.HOST} has been ${blockedWord}${reasonString}.`
341 const text = 'Hi,\n\n' +
347 const to = user.email
348 const emailPayload: EmailPayload = {
350 subject: '[PeerTube] Account ' + blockedWord,
354 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
357 sendMail (to: string[], subject: string, text: string) {
359 throw new Error('Cannot send mail because SMTP is not configured.')
362 return this.transporter.sendMail({
363 from: CONFIG.SMTP.FROM_ADDRESS,
370 private dieOnConnectionFailure (err?: Error) {
371 logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err })
375 static get Instance () {
376 return this.instance || (this.instance = new this())
380 // ---------------------------------------------------------------------------