PeerTube/server/lib/emailer.ts

497 lines
16 KiB
TypeScript
Raw Normal View History

2018-01-30 13:27:07 +01:00
import { createTransport, Transporter } from 'nodemailer'
import { isTestInstance } from '../helpers/core-utils'
2018-03-22 11:32:43 +01:00
import { bunyanLogger, logger } from '../helpers/logger'
2020-02-17 10:27:00 +01:00
import { CONFIG, isEmailEnabled } from '../initializers/config'
2018-01-30 13:27:07 +01:00
import { JobQueue } from './job-queue'
import { EmailPayload } from './job-queue/handlers/email'
import { readFileSync } from 'fs-extra'
2019-04-11 11:33:44 +02:00
import { WEBSERVER } from '../initializers/constants'
import {
MCommentOwnerVideo,
MVideo,
MVideoAbuseVideo,
MVideoAccountLight,
MVideoBlacklistLightVideo,
MVideoBlacklistVideo
} from '../typings/models/video'
import { MActorFollowActors, MActorFollowFull, MUser } from '../typings/models'
2019-08-15 11:53:26 +02:00
import { MVideoImport, MVideoImportVideo } from '@server/typings/models/video/video-import'
2018-01-30 13:27:07 +01:00
type SendEmailOptions = {
to: string[]
subject: string
text: string
fromDisplayName?: string
replyTo?: string
}
2018-01-30 13:27:07 +01:00
class Emailer {
private static instance: Emailer
private initialized = false
private transporter: Transporter
2020-01-31 16:56:52 +01:00
private constructor () {
}
2018-01-30 13:27:07 +01:00
init () {
// Already initialized
if (this.initialized === true) return
this.initialized = true
2020-02-18 08:29:23 +01:00
if (isEmailEnabled()) {
2018-01-30 13:27:07 +01:00
logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
let tls
if (CONFIG.SMTP.CA_FILE) {
tls = {
ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ]
}
}
2018-01-30 15:16:24 +01:00
let auth
if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) {
auth = {
user: CONFIG.SMTP.USERNAME,
pass: CONFIG.SMTP.PASSWORD
}
}
2018-01-30 13:27:07 +01:00
this.transporter = createTransport({
host: CONFIG.SMTP.HOSTNAME,
port: CONFIG.SMTP.PORT,
secure: CONFIG.SMTP.TLS,
2018-03-22 11:32:43 +01:00
debug: CONFIG.LOG.LEVEL === 'debug',
logger: bunyanLogger as any,
2018-03-22 16:12:52 +01:00
ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS,
2018-01-30 13:27:07 +01:00
tls,
2018-01-30 15:16:24 +01:00
auth
2018-01-30 13:27:07 +01:00
})
} else {
if (!isTestInstance()) {
logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!')
}
}
}
2019-01-10 11:12:41 +01:00
static isEnabled () {
return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT
}
2018-01-30 13:27:07 +01:00
async checkConnectionOrDie () {
if (!this.transporter) return
logger.info('Testing SMTP server...')
2018-01-30 13:27:07 +01:00
try {
const success = await this.transporter.verify()
if (success !== true) this.dieOnConnectionFailure()
logger.info('Successfully connected to SMTP server.')
} catch (err) {
this.dieOnConnectionFailure(err)
}
}
2019-08-15 11:53:26 +02:00
addNewVideoFromSubscriberNotification (to: string[], video: MVideoAccountLight) {
2018-12-26 10:36:24 +01:00
const channelName = video.VideoChannel.getDisplayName()
2019-04-11 11:33:44 +02:00
const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
2018-12-26 10:36:24 +01:00
2020-01-31 16:56:52 +01:00
const text = 'Hi dear user,\n\n' +
2018-12-26 10:36:24 +01:00
`Your subscription ${channelName} just published a new video: ${video.name}` +
2020-01-31 16:56:52 +01:00
'\n\n' +
2018-12-26 10:36:24 +01:00
`You can view it on ${videoUrl} ` +
2020-01-31 16:56:52 +01:00
'\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
2018-01-30 13:27:07 +01:00
const emailPayload: EmailPayload = {
2018-12-26 10:36:24 +01:00
to,
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + channelName + ' just published a new video',
2018-01-30 13:27:07 +01:00
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
addNewFollowNotification (to: string[], actorFollow: MActorFollowFull, followType: 'account' | 'channel') {
const followerName = actorFollow.ActorFollower.Account.getDisplayName()
const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName()
2020-01-31 16:56:52 +01:00
const text = 'Hi dear user,\n\n' +
`Your ${followType} ${followingName} has a new subscriber: ${followerName}` +
2020-01-31 16:56:52 +01:00
'\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
const emailPayload: EmailPayload = {
to,
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New follower on your channel ' + followingName,
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
2019-08-15 11:53:26 +02:00
addNewInstanceFollowerNotification (to: string[], actorFollow: MActorFollowActors) {
const awaitingApproval = actorFollow.state === 'pending' ? ' awaiting manual approval.' : ''
2020-01-31 16:56:52 +01:00
const text = 'Hi dear admin,\n\n' +
`Your instance has a new follower: ${actorFollow.ActorFollower.url}${awaitingApproval}` +
2020-01-31 16:56:52 +01:00
'\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
const emailPayload: EmailPayload = {
to,
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New instance follower',
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
addAutoInstanceFollowingNotification (to: string[], actorFollow: MActorFollowActors) {
2020-01-31 16:56:52 +01:00
const text = 'Hi dear admin,\n\n' +
`Your instance automatically followed a new instance: ${actorFollow.ActorFollowing.url}` +
2020-01-31 16:56:52 +01:00
'\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
const emailPayload: EmailPayload = {
to,
subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Auto instance following',
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
2019-08-15 11:53:26 +02:00
myVideoPublishedNotification (to: string[], video: MVideo) {
2019-04-11 11:33:44 +02:00
const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
2020-01-31 16:56:52 +01:00
const text = 'Hi dear user,\n\n' +
`Your video ${video.name} has been published.` +
2020-01-31 16:56:52 +01:00
'\n\n' +
`You can view it on ${videoUrl} ` +
2020-01-31 16:56:52 +01:00
'\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
const emailPayload: EmailPayload = {
to,
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video ${video.name} is published`,
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
2019-08-15 11:53:26 +02:00
myVideoImportSuccessNotification (to: string[], videoImport: MVideoImportVideo) {
2019-04-11 11:33:44 +02:00
const videoUrl = WEBSERVER.URL + videoImport.Video.getWatchStaticPath()
2020-01-31 16:56:52 +01:00
const text = 'Hi dear user,\n\n' +
`Your video import ${videoImport.getTargetIdentifier()} is finished.` +
2020-01-31 16:56:52 +01:00
'\n\n' +
`You can view the imported video on ${videoUrl} ` +
2020-01-31 16:56:52 +01:00
'\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
const emailPayload: EmailPayload = {
to,
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} is finished`,
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
2019-08-15 11:53:26 +02:00
myVideoImportErrorNotification (to: string[], videoImport: MVideoImport) {
2019-04-11 11:33:44 +02:00
const importUrl = WEBSERVER.URL + '/my-account/video-imports'
2020-01-31 16:56:52 +01:00
const text = 'Hi dear user,\n\n' +
`Your video import ${videoImport.getTargetIdentifier()} encountered an error.` +
2020-01-31 16:56:52 +01:00
'\n\n' +
`See your videos import dashboard for more information: ${importUrl}` +
2020-01-31 16:56:52 +01:00
'\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
const emailPayload: EmailPayload = {
to,
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} encountered an error`,
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
2019-08-15 11:53:26 +02:00
addNewCommentOnMyVideoNotification (to: string[], comment: MCommentOwnerVideo) {
2018-12-26 10:36:24 +01:00
const accountName = comment.Account.getDisplayName()
const video = comment.Video
2019-04-11 11:33:44 +02:00
const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
2018-12-26 10:36:24 +01:00
2020-01-31 16:56:52 +01:00
const text = 'Hi dear user,\n\n' +
2018-12-26 10:36:24 +01:00
`A new comment has been posted by ${accountName} on your video ${video.name}` +
2020-01-31 16:56:52 +01:00
'\n\n' +
2018-12-26 10:36:24 +01:00
`You can view it on ${commentUrl} ` +
2020-01-31 16:56:52 +01:00
'\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
const emailPayload: EmailPayload = {
2018-12-26 10:36:24 +01:00
to,
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New comment on your video ' + video.name,
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
2019-08-15 11:53:26 +02:00
addNewCommentMentionNotification (to: string[], comment: MCommentOwnerVideo) {
const accountName = comment.Account.getDisplayName()
const video = comment.Video
2019-04-11 11:33:44 +02:00
const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
2020-01-31 16:56:52 +01:00
const text = 'Hi dear user,\n\n' +
`${accountName} mentioned you on video ${video.name}` +
2020-01-31 16:56:52 +01:00
'\n\n' +
`You can view the comment on ${commentUrl} ` +
2020-01-31 16:56:52 +01:00
'\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
const emailPayload: EmailPayload = {
to,
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Mention on video ' + video.name,
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
2019-08-15 11:53:26 +02:00
addVideoAbuseModeratorsNotification (to: string[], videoAbuse: MVideoAbuseVideo) {
2019-04-11 11:33:44 +02:00
const videoUrl = WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath()
2018-02-01 11:08:10 +01:00
2020-01-31 16:56:52 +01:00
const text = 'Hi,\n\n' +
2019-04-11 11:33:44 +02:00
`${WEBSERVER.HOST} received an abuse for the following video ${videoUrl}\n\n` +
2020-01-31 16:56:52 +01:00
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
2018-02-01 11:08:10 +01:00
const emailPayload: EmailPayload = {
to,
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Received a video abuse',
2018-02-01 11:08:10 +01:00
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
addVideoAutoBlacklistModeratorsNotification (to: string[], videoBlacklist: MVideoBlacklistLightVideo) {
2019-04-11 11:33:44 +02:00
const VIDEO_AUTO_BLACKLIST_URL = WEBSERVER.URL + '/admin/moderation/video-auto-blacklist/list'
const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
2020-01-31 16:56:52 +01:00
const text = 'Hi,\n\n' +
'A recently added video was auto-blacklisted and requires moderator review before publishing.' +
'\n\n' +
`You can view it and take appropriate action on ${videoUrl}` +
2020-01-31 16:56:52 +01:00
'\n\n' +
`A full list of auto-blacklisted videos can be reviewed here: ${VIDEO_AUTO_BLACKLIST_URL}` +
2020-01-31 16:56:52 +01:00
'\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
const emailPayload: EmailPayload = {
to,
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'An auto-blacklisted video is awaiting review',
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
2019-08-15 11:53:26 +02:00
addNewUserRegistrationNotification (to: string[], user: MUser) {
2020-01-31 16:56:52 +01:00
const text = 'Hi,\n\n' +
2019-04-11 11:33:44 +02:00
`User ${user.username} just registered on ${WEBSERVER.HOST} PeerTube instance.\n\n` +
2020-01-31 16:56:52 +01:00
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
const emailPayload: EmailPayload = {
to,
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New user registration on ' + WEBSERVER.HOST,
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
2019-08-15 11:53:26 +02:00
addVideoBlacklistNotification (to: string[], videoBlacklist: MVideoBlacklistVideo) {
2018-12-26 10:36:24 +01:00
const videoName = videoBlacklist.Video.name
2019-04-11 11:33:44 +02:00
const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
2018-08-13 16:57:13 +02:00
2018-12-26 10:36:24 +01:00
const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : ''
2019-04-11 11:33:44 +02:00
const blockedString = `Your video ${videoName} (${videoUrl} on ${WEBSERVER.HOST} has been blacklisted${reasonString}.`
2018-08-13 16:57:13 +02:00
const text = 'Hi,\n\n' +
blockedString +
'\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
2018-08-13 16:57:13 +02:00
const emailPayload: EmailPayload = {
2018-12-26 10:36:24 +01:00
to,
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${videoName} blacklisted`,
2018-08-13 16:57:13 +02:00
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
2019-08-15 11:53:26 +02:00
addVideoUnblacklistNotification (to: string[], video: MVideo) {
2019-04-11 11:33:44 +02:00
const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
2018-08-13 16:57:13 +02:00
const text = 'Hi,\n\n' +
2019-04-11 11:33:44 +02:00
`Your video ${video.name} (${videoUrl}) on ${WEBSERVER.HOST} has been unblacklisted.` +
2018-08-13 16:57:13 +02:00
'\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
2018-08-13 16:57:13 +02:00
const emailPayload: EmailPayload = {
2018-12-26 10:36:24 +01:00
to,
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${video.name} unblacklisted`,
2018-08-13 16:57:13 +02:00
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
addPasswordResetEmailJob (to: string, resetPasswordUrl: string) {
2020-01-31 16:56:52 +01:00
const text = 'Hi dear user,\n\n' +
2019-04-11 11:33:44 +02:00
`A reset password procedure for your account ${to} has been requested on ${WEBSERVER.HOST} ` +
`Please follow this link to reset it: ${resetPasswordUrl} (the link will expire within 1 hour)\n\n` +
2020-01-31 16:56:52 +01:00
'If you are not the person who initiated this request, please ignore this email.\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
2018-12-26 10:36:24 +01:00
const emailPayload: EmailPayload = {
to: [ to ],
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Reset your password',
2018-12-26 10:36:24 +01:00
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
addPasswordCreateEmailJob (username: string, to: string, resetPasswordUrl: string) {
const text = 'Hi,\n\n' +
`Welcome to your ${WEBSERVER.HOST} PeerTube instance. Your username is: ${username}.\n\n` +
`Please set your password by following this link: ${resetPasswordUrl} (this link will expire within seven days).\n\n` +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
const emailPayload: EmailPayload = {
to: [ to ],
subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New PeerTube account password',
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
2018-12-26 10:36:24 +01:00
addVerifyEmailJob (to: string, verifyEmailUrl: string) {
2020-01-31 16:56:52 +01:00
const text = 'Welcome to PeerTube,\n\n' +
2019-04-11 11:33:44 +02:00
`To start using PeerTube on ${WEBSERVER.HOST} you must verify your email! ` +
2018-12-26 10:36:24 +01:00
`Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` +
2020-01-31 16:56:52 +01:00
'If you are not the person who initiated this request, please ignore this email.\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
2018-12-26 10:36:24 +01:00
const emailPayload: EmailPayload = {
to: [ to ],
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Verify your email',
2018-12-26 10:36:24 +01:00
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
2019-08-15 11:53:26 +02:00
addUserBlockJob (user: MUser, blocked: boolean, reason?: string) {
2018-08-08 17:36:10 +02:00
const reasonString = reason ? ` for the following reason: ${reason}` : ''
const blockedWord = blocked ? 'blocked' : 'unblocked'
2019-04-11 11:33:44 +02:00
const blockedString = `Your account ${user.username} on ${WEBSERVER.HOST} has been ${blockedWord}${reasonString}.`
2018-08-08 17:36:10 +02:00
const text = 'Hi,\n\n' +
blockedString +
'\n\n' +
'Cheers,\n' +
`${CONFIG.EMAIL.BODY.SIGNATURE}`
2018-08-08 17:36:10 +02:00
const to = user.email
const emailPayload: EmailPayload = {
to: [ to ],
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Account ' + blockedWord,
2018-08-08 17:36:10 +02:00
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
addContactFormJob (fromEmail: string, fromName: string, subject: string, body: string) {
2019-01-09 15:14:29 +01:00
const text = 'Hello dear admin,\n\n' +
fromName + ' sent you a message' +
'\n\n---------------------------------------\n\n' +
body +
'\n\n---------------------------------------\n\n' +
'Cheers,\n' +
'PeerTube.'
const emailPayload: EmailPayload = {
2019-02-14 11:56:23 +01:00
fromDisplayName: fromEmail,
replyTo: fromEmail,
2019-01-09 15:14:29 +01:00
to: [ CONFIG.ADMIN.EMAIL ],
2019-08-22 10:22:01 +02:00
subject: CONFIG.EMAIL.SUBJECT.PREFIX + subject,
2019-01-09 15:14:29 +01:00
text
}
return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
}
2019-11-29 13:36:40 +01:00
async sendMail (options: EmailPayload) {
2020-02-17 10:27:00 +01:00
if (!isEmailEnabled()) {
2018-01-30 13:27:07 +01:00
throw new Error('Cannot send mail because SMTP is not configured.')
}
2019-02-14 11:56:23 +01:00
const fromDisplayName = options.fromDisplayName
? options.fromDisplayName
2019-04-11 11:33:44 +02:00
: WEBSERVER.HOST
2019-02-14 11:56:23 +01:00
2019-11-29 13:36:40 +01:00
for (const to of options.to) {
await this.transporter.sendMail({
from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>`,
replyTo: options.replyTo,
to,
subject: options.subject,
text: options.text
})
}
2018-01-30 13:27:07 +01:00
}
private dieOnConnectionFailure (err?: Error) {
2018-03-26 15:54:13 +02:00
logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err })
2018-01-30 13:27:07 +01:00
process.exit(-1)
}
static get Instance () {
return this.instance || (this.instance = new this())
}
}
// ---------------------------------------------------------------------------
export {
Emailer,
SendEmailOptions
2018-01-30 13:27:07 +01:00
}