]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/emailer.ts
Add ability to reset our password
[github/Chocobozzz/PeerTube.git] / server / lib / emailer.ts
1 import { createTransport, Transporter } from 'nodemailer'
2 import { isTestInstance } from '../helpers/core-utils'
3 import { logger } from '../helpers/logger'
4 import { CONFIG } from '../initializers'
5 import { JobQueue } from './job-queue'
6 import { EmailPayload } from './job-queue/handlers/email'
7 import { readFileSync } from 'fs'
8
9 class Emailer {
10
11 private static instance: Emailer
12 private initialized = false
13 private transporter: Transporter
14
15 private constructor () {}
16
17 init () {
18 // Already initialized
19 if (this.initialized === true) return
20 this.initialized = true
21
22 if (CONFIG.SMTP.HOSTNAME && CONFIG.SMTP.PORT) {
23 logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
24
25 let tls
26 if (CONFIG.SMTP.CA_FILE) {
27 tls = {
28 ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ]
29 }
30 }
31
32 this.transporter = createTransport({
33 host: CONFIG.SMTP.HOSTNAME,
34 port: CONFIG.SMTP.PORT,
35 secure: CONFIG.SMTP.TLS,
36 tls,
37 auth: {
38 user: CONFIG.SMTP.USERNAME,
39 pass: CONFIG.SMTP.PASSWORD
40 }
41 })
42 } else {
43 if (!isTestInstance()) {
44 logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!')
45 }
46 }
47 }
48
49 async checkConnectionOrDie () {
50 if (!this.transporter) return
51
52 try {
53 const success = await this.transporter.verify()
54 if (success !== true) this.dieOnConnectionFailure()
55
56 logger.info('Successfully connected to SMTP server.')
57 } catch (err) {
58 this.dieOnConnectionFailure(err)
59 }
60 }
61
62 addForgetPasswordEmailJob (to: string, resetPasswordUrl: string) {
63 const text = `Hi dear user,\n\n` +
64 `It seems you forgot your password on ${CONFIG.WEBSERVER.HOST}! ` +
65 `Please follow this link to reset it: ${resetPasswordUrl}.\n\n` +
66 `If you are not the person who initiated this request, please ignore this email.\n\n` +
67 `Cheers,\n` +
68 `PeerTube.`
69
70 const emailPayload: EmailPayload = {
71 to: [ to ],
72 subject: 'Reset your PeerTube password',
73 text
74 }
75
76 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
77 }
78
79 sendMail (to: string[], subject: string, text: string) {
80 if (!this.transporter) {
81 throw new Error('Cannot send mail because SMTP is not configured.')
82 }
83
84 return this.transporter.sendMail({
85 from: CONFIG.SMTP.FROM_ADDRESS,
86 to: to.join(','),
87 subject,
88 text
89 })
90 }
91
92 private dieOnConnectionFailure (err?: Error) {
93 logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, err)
94 process.exit(-1)
95 }
96
97 static get Instance () {
98 return this.instance || (this.instance = new this())
99 }
100 }
101
102 // ---------------------------------------------------------------------------
103
104 export {
105 Emailer
106 }