1 import { createTransport, Transporter } from 'nodemailer'
2 import { UserRight } from '../../shared/models/users'
3 import { isTestInstance } from '../helpers/core-utils'
4 import { logger } from '../helpers/logger'
5 import { CONFIG } from '../initializers'
6 import { UserModel } from '../models/account/user'
7 import { VideoModel } from '../models/video/video'
8 import { JobQueue } from './job-queue'
9 import { EmailPayload } from './job-queue/handlers/email'
10 import { readFileSync } from 'fs'
14 private static instance: Emailer
15 private initialized = false
16 private transporter: Transporter
18 private constructor () {}
21 // Already initialized
22 if (this.initialized === true) return
23 this.initialized = true
25 if (CONFIG.SMTP.HOSTNAME && CONFIG.SMTP.PORT) {
26 logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
29 if (CONFIG.SMTP.CA_FILE) {
31 ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ]
36 if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) {
38 user: CONFIG.SMTP.USERNAME,
39 pass: CONFIG.SMTP.PASSWORD
43 this.transporter = createTransport({
44 host: CONFIG.SMTP.HOSTNAME,
45 port: CONFIG.SMTP.PORT,
46 secure: CONFIG.SMTP.TLS,
47 ignoreTLS: isTestInstance(),
52 if (!isTestInstance()) {
53 logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!')
58 async checkConnectionOrDie () {
59 if (!this.transporter) return
62 const success = await this.transporter.verify()
63 if (success !== true) this.dieOnConnectionFailure()
65 logger.info('Successfully connected to SMTP server.')
67 this.dieOnConnectionFailure(err)
71 addForgetPasswordEmailJob (to: string, resetPasswordUrl: string) {
72 const text = `Hi dear user,\n\n` +
73 `It seems you forgot your password on ${CONFIG.WEBSERVER.HOST}! ` +
74 `Please follow this link to reset it: ${resetPasswordUrl}.\n\n` +
75 `If you are not the person who initiated this request, please ignore this email.\n\n` +
79 const emailPayload: EmailPayload = {
81 subject: 'Reset your PeerTube password',
85 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
88 async addVideoAbuseReport (videoId: number) {
89 const video = await VideoModel.load(videoId)
91 const text = `Hi,\n\n` +
92 `Your instance received an abuse for video the following video ${video.url}\n\n` +
96 const to = await UserModel.listEmailsWithRight(UserRight.MANAGE_VIDEO_ABUSES)
97 const emailPayload: EmailPayload = {
99 subject: '[PeerTube] Received a video abuse',
103 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
106 sendMail (to: string[], subject: string, text: string) {
107 if (!this.transporter) {
108 throw new Error('Cannot send mail because SMTP is not configured.')
111 return this.transporter.sendMail({
112 from: CONFIG.SMTP.FROM_ADDRESS,
119 private dieOnConnectionFailure (err?: Error) {
120 logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, err)
124 static get Instance () {
125 return this.instance || (this.instance = new this())
129 // ---------------------------------------------------------------------------