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