]>
Commit | Line | Data |
---|---|---|
ecb4e35f | 1 | import { createTransport, Transporter } from 'nodemailer' |
ba75d268 | 2 | import { UserRight } from '../../shared/models/users' |
ecb4e35f C |
3 | import { isTestInstance } from '../helpers/core-utils' |
4 | import { logger } from '../helpers/logger' | |
5 | import { CONFIG } from '../initializers' | |
ba75d268 C |
6 | import { UserModel } from '../models/account/user' |
7 | import { VideoModel } from '../models/video/video' | |
ecb4e35f C |
8 | import { JobQueue } from './job-queue' |
9 | import { EmailPayload } from './job-queue/handlers/email' | |
10 | import { readFileSync } from 'fs' | |
11 | ||
12 | class Emailer { | |
13 | ||
14 | private static instance: Emailer | |
15 | private initialized = false | |
16 | private transporter: Transporter | |
17 | ||
18 | private constructor () {} | |
19 | ||
20 | init () { | |
21 | // Already initialized | |
22 | if (this.initialized === true) return | |
23 | this.initialized = true | |
24 | ||
25 | if (CONFIG.SMTP.HOSTNAME && CONFIG.SMTP.PORT) { | |
26 | logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT) | |
27 | ||
28 | let tls | |
29 | if (CONFIG.SMTP.CA_FILE) { | |
30 | tls = { | |
31 | ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ] | |
32 | } | |
33 | } | |
34 | ||
f076daa7 C |
35 | let auth |
36 | if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) { | |
37 | auth = { | |
38 | user: CONFIG.SMTP.USERNAME, | |
39 | pass: CONFIG.SMTP.PASSWORD | |
40 | } | |
41 | } | |
42 | ||
ecb4e35f C |
43 | this.transporter = createTransport({ |
44 | host: CONFIG.SMTP.HOSTNAME, | |
45 | port: CONFIG.SMTP.PORT, | |
46 | secure: CONFIG.SMTP.TLS, | |
f076daa7 | 47 | ignoreTLS: isTestInstance(), |
ecb4e35f | 48 | tls, |
f076daa7 | 49 | auth |
ecb4e35f C |
50 | }) |
51 | } else { | |
52 | if (!isTestInstance()) { | |
53 | logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!') | |
54 | } | |
55 | } | |
56 | } | |
57 | ||
58 | async checkConnectionOrDie () { | |
59 | if (!this.transporter) return | |
60 | ||
61 | try { | |
62 | const success = await this.transporter.verify() | |
63 | if (success !== true) this.dieOnConnectionFailure() | |
64 | ||
65 | logger.info('Successfully connected to SMTP server.') | |
66 | } catch (err) { | |
67 | this.dieOnConnectionFailure(err) | |
68 | } | |
69 | } | |
70 | ||
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` + | |
76 | `Cheers,\n` + | |
77 | `PeerTube.` | |
78 | ||
79 | const emailPayload: EmailPayload = { | |
80 | to: [ to ], | |
81 | subject: 'Reset your PeerTube password', | |
82 | text | |
83 | } | |
84 | ||
85 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
86 | } | |
87 | ||
ba75d268 C |
88 | async addVideoAbuseReport (videoId: number) { |
89 | const video = await VideoModel.load(videoId) | |
90 | ||
91 | const text = `Hi,\n\n` + | |
92 | `Your instance received an abuse for video the following video ${video.url}\n\n` + | |
93 | `Cheers,\n` + | |
94 | `PeerTube.` | |
95 | ||
96 | const to = await UserModel.listEmailsWithRight(UserRight.MANAGE_VIDEO_ABUSES) | |
97 | const emailPayload: EmailPayload = { | |
98 | to, | |
99 | subject: '[PeerTube] Received a video abuse', | |
100 | text | |
101 | } | |
102 | ||
103 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
104 | } | |
105 | ||
ecb4e35f C |
106 | sendMail (to: string[], subject: string, text: string) { |
107 | if (!this.transporter) { | |
108 | throw new Error('Cannot send mail because SMTP is not configured.') | |
109 | } | |
110 | ||
111 | return this.transporter.sendMail({ | |
112 | from: CONFIG.SMTP.FROM_ADDRESS, | |
113 | to: to.join(','), | |
114 | subject, | |
115 | text | |
116 | }) | |
117 | } | |
118 | ||
119 | private dieOnConnectionFailure (err?: Error) { | |
120 | logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, err) | |
121 | process.exit(-1) | |
122 | } | |
123 | ||
124 | static get Instance () { | |
125 | return this.instance || (this.instance = new this()) | |
126 | } | |
127 | } | |
128 | ||
129 | // --------------------------------------------------------------------------- | |
130 | ||
131 | export { | |
132 | Emailer | |
133 | } |