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
22 private constructor () {}
25 // Already initialized
26 if (this.initialized === true) return
27 this.initialized = true
29 if (Emailer.isEnabled()) {
30 logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
33 if (CONFIG.SMTP.CA_FILE) {
35 ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ]
40 if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) {
42 user: CONFIG.SMTP.USERNAME,
43 pass: CONFIG.SMTP.PASSWORD
47 this.transporter = createTransport({
48 host: CONFIG.SMTP.HOSTNAME,
49 port: CONFIG.SMTP.PORT,
50 secure: CONFIG.SMTP.TLS,
51 debug: CONFIG.LOG.LEVEL === 'debug',
52 logger: bunyanLogger as any,
53 ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS,
58 if (!isTestInstance()) {
59 logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!')
65 return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT
68 async checkConnectionOrDie () {
69 if (!this.transporter) return
71 logger.info('Testing SMTP server...')
74 const success = await this.transporter.verify()
75 if (success !== true) this.dieOnConnectionFailure()
77 logger.info('Successfully connected to SMTP server.')
79 this.dieOnConnectionFailure(err)
83 addNewVideoFromSubscriberNotification (to: string[], video: VideoModel) {
84 const channelName = video.VideoChannel.getDisplayName()
85 const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath()
87 const text = `Hi dear user,\n\n` +
88 `Your subscription ${channelName} just published a new video: ${video.name}` +
90 `You can view it on ${videoUrl} ` +
95 const emailPayload: EmailPayload = {
97 subject: channelName + ' just published a new video',
101 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
104 addNewFollowNotification (to: string[], actorFollow: ActorFollowModel, followType: 'account' | 'channel') {
105 const followerName = actorFollow.ActorFollower.Account.getDisplayName()
106 const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName()
108 const text = `Hi dear user,\n\n` +
109 `Your ${followType} ${followingName} has a new subscriber: ${followerName}` +
114 const emailPayload: EmailPayload = {
116 subject: 'New follower on your channel ' + followingName,
120 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
123 myVideoPublishedNotification (to: string[], video: VideoModel) {
124 const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath()
126 const text = `Hi dear user,\n\n` +
127 `Your video ${video.name} has been published.` +
129 `You can view it on ${videoUrl} ` +
134 const emailPayload: EmailPayload = {
136 subject: `Your video ${video.name} is published`,
140 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
143 myVideoImportSuccessNotification (to: string[], videoImport: VideoImportModel) {
144 const videoUrl = CONFIG.WEBSERVER.URL + videoImport.Video.getWatchStaticPath()
146 const text = `Hi dear user,\n\n` +
147 `Your video import ${videoImport.getTargetIdentifier()} is finished.` +
149 `You can view the imported video on ${videoUrl} ` +
154 const emailPayload: EmailPayload = {
156 subject: `Your video import ${videoImport.getTargetIdentifier()} is finished`,
160 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
163 myVideoImportErrorNotification (to: string[], videoImport: VideoImportModel) {
164 const importUrl = CONFIG.WEBSERVER.URL + '/my-account/video-imports'
166 const text = `Hi dear user,\n\n` +
167 `Your video import ${videoImport.getTargetIdentifier()} encountered an error.` +
169 `See your videos import dashboard for more information: ${importUrl}` +
174 const emailPayload: EmailPayload = {
176 subject: `Your video import ${videoImport.getTargetIdentifier()} encountered an error`,
180 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
183 addNewCommentOnMyVideoNotification (to: string[], comment: VideoCommentModel) {
184 const accountName = comment.Account.getDisplayName()
185 const video = comment.Video
186 const commentUrl = CONFIG.WEBSERVER.URL + comment.getCommentStaticPath()
188 const text = `Hi dear user,\n\n` +
189 `A new comment has been posted by ${accountName} on your video ${video.name}` +
191 `You can view it on ${commentUrl} ` +
196 const emailPayload: EmailPayload = {
198 subject: 'New comment on your video ' + video.name,
202 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
205 addNewCommentMentionNotification (to: string[], comment: VideoCommentModel) {
206 const accountName = comment.Account.getDisplayName()
207 const video = comment.Video
208 const commentUrl = CONFIG.WEBSERVER.URL + comment.getCommentStaticPath()
210 const text = `Hi dear user,\n\n` +
211 `${accountName} mentioned you on video ${video.name}` +
213 `You can view the comment on ${commentUrl} ` +
218 const emailPayload: EmailPayload = {
220 subject: 'Mention on video ' + video.name,
224 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
227 addVideoAbuseModeratorsNotification (to: string[], videoAbuse: VideoAbuseModel) {
228 const videoUrl = CONFIG.WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath()
230 const text = `Hi,\n\n` +
231 `${CONFIG.WEBSERVER.HOST} received an abuse for the following video ${videoUrl}\n\n` +
235 const emailPayload: EmailPayload = {
237 subject: '[PeerTube] Received a video abuse',
241 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
244 addNewUserRegistrationNotification (to: string[], user: UserModel) {
245 const text = `Hi,\n\n` +
246 `User ${user.username} just registered on ${CONFIG.WEBSERVER.HOST} PeerTube instance.\n\n` +
250 const emailPayload: EmailPayload = {
252 subject: '[PeerTube] New user registration on ' + CONFIG.WEBSERVER.HOST,
256 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
259 addVideoBlacklistNotification (to: string[], videoBlacklist: VideoBlacklistModel) {
260 const videoName = videoBlacklist.Video.name
261 const videoUrl = CONFIG.WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
263 const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : ''
264 const blockedString = `Your video ${videoName} (${videoUrl} on ${CONFIG.WEBSERVER.HOST} has been blacklisted${reasonString}.`
266 const text = 'Hi,\n\n' +
272 const emailPayload: EmailPayload = {
274 subject: `[PeerTube] Video ${videoName} blacklisted`,
278 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
281 addVideoUnblacklistNotification (to: string[], video: VideoModel) {
282 const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath()
284 const text = 'Hi,\n\n' +
285 `Your video ${video.name} (${videoUrl}) on ${CONFIG.WEBSERVER.HOST} has been unblacklisted.` +
290 const emailPayload: EmailPayload = {
292 subject: `[PeerTube] Video ${video.name} unblacklisted`,
296 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
299 addPasswordResetEmailJob (to: string, resetPasswordUrl: string) {
300 const text = `Hi dear user,\n\n` +
301 `A reset password procedure for your account ${to} has been requested on ${CONFIG.WEBSERVER.HOST} ` +
302 `Please follow this link to reset it: ${resetPasswordUrl}\n\n` +
303 `If you are not the person who initiated this request, please ignore this email.\n\n` +
307 const emailPayload: EmailPayload = {
309 subject: 'Reset your PeerTube password',
313 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
316 addVerifyEmailJob (to: string, verifyEmailUrl: string) {
317 const text = `Welcome to PeerTube,\n\n` +
318 `To start using PeerTube on ${CONFIG.WEBSERVER.HOST} you must verify your email! ` +
319 `Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` +
320 `If you are not the person who initiated this request, please ignore this email.\n\n` +
324 const emailPayload: EmailPayload = {
326 subject: 'Verify your PeerTube email',
330 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
333 addUserBlockJob (user: UserModel, blocked: boolean, reason?: string) {
334 const reasonString = reason ? ` for the following reason: ${reason}` : ''
335 const blockedWord = blocked ? 'blocked' : 'unblocked'
336 const blockedString = `Your account ${user.username} on ${CONFIG.WEBSERVER.HOST} has been ${blockedWord}${reasonString}.`
338 const text = 'Hi,\n\n' +
344 const to = user.email
345 const emailPayload: EmailPayload = {
347 subject: '[PeerTube] Account ' + blockedWord,
351 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
354 addContactFormJob (fromEmail: string, fromName: string, body: string) {
355 const text = 'Hello dear admin,\n\n' +
356 fromName + ' sent you a message' +
357 '\n\n---------------------------------------\n\n' +
359 '\n\n---------------------------------------\n\n' +
363 const emailPayload: EmailPayload = {
365 to: [ CONFIG.ADMIN.EMAIL ],
366 subject: '[PeerTube] Contact form submitted',
370 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
373 sendMail (to: string[], subject: string, text: string, from?: string) {
374 if (!Emailer.isEnabled()) {
375 throw new Error('Cannot send mail because SMTP is not configured.')
378 return this.transporter.sendMail({
379 from: from || CONFIG.SMTP.FROM_ADDRESS,
386 private dieOnConnectionFailure (err?: Error) {
387 logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err })
391 static get Instance () {
392 return this.instance || (this.instance = new this())
396 // ---------------------------------------------------------------------------