]>
Commit | Line | Data |
---|---|---|
ecb4e35f C |
1 | import { createTransport, Transporter } from 'nodemailer' |
2 | import { isTestInstance } from '../helpers/core-utils' | |
05e67d62 | 3 | import { bunyanLogger, logger } from '../helpers/logger' |
ecb4e35f | 4 | import { CONFIG } from '../initializers' |
ba75d268 C |
5 | import { UserModel } from '../models/account/user' |
6 | import { VideoModel } from '../models/video/video' | |
ecb4e35f C |
7 | import { JobQueue } from './job-queue' |
8 | import { EmailPayload } from './job-queue/handlers/email' | |
c9d5c64f | 9 | import { readFileSync } from 'fs-extra' |
cef534ed C |
10 | import { VideoCommentModel } from '../models/video/video-comment' |
11 | import { VideoAbuseModel } from '../models/video/video-abuse' | |
12 | import { VideoBlacklistModel } from '../models/video/video-blacklist' | |
dc133480 | 13 | import { VideoImportModel } from '../models/video/video-import' |
f7cc67b4 | 14 | import { ActorFollowModel } from '../models/activitypub/actor-follow' |
ecb4e35f C |
15 | |
16 | class Emailer { | |
17 | ||
18 | private static instance: Emailer | |
19 | private initialized = false | |
20 | private transporter: Transporter | |
21 | ||
22 | private constructor () {} | |
23 | ||
24 | init () { | |
25 | // Already initialized | |
26 | if (this.initialized === true) return | |
27 | this.initialized = true | |
28 | ||
d3e56c0c | 29 | if (Emailer.isEnabled()) { |
ecb4e35f C |
30 | logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT) |
31 | ||
32 | let tls | |
33 | if (CONFIG.SMTP.CA_FILE) { | |
34 | tls = { | |
35 | ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ] | |
36 | } | |
37 | } | |
38 | ||
f076daa7 C |
39 | let auth |
40 | if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) { | |
41 | auth = { | |
42 | user: CONFIG.SMTP.USERNAME, | |
43 | pass: CONFIG.SMTP.PASSWORD | |
44 | } | |
45 | } | |
46 | ||
ecb4e35f C |
47 | this.transporter = createTransport({ |
48 | host: CONFIG.SMTP.HOSTNAME, | |
49 | port: CONFIG.SMTP.PORT, | |
50 | secure: CONFIG.SMTP.TLS, | |
05e67d62 C |
51 | debug: CONFIG.LOG.LEVEL === 'debug', |
52 | logger: bunyanLogger as any, | |
bebf2d89 | 53 | ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS, |
ecb4e35f | 54 | tls, |
f076daa7 | 55 | auth |
ecb4e35f C |
56 | }) |
57 | } else { | |
58 | if (!isTestInstance()) { | |
59 | logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!') | |
60 | } | |
61 | } | |
62 | } | |
63 | ||
d3e56c0c C |
64 | static isEnabled () { |
65 | return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT | |
3b3b1820 C |
66 | } |
67 | ||
ecb4e35f C |
68 | async checkConnectionOrDie () { |
69 | if (!this.transporter) return | |
70 | ||
3d3441d6 C |
71 | logger.info('Testing SMTP server...') |
72 | ||
ecb4e35f C |
73 | try { |
74 | const success = await this.transporter.verify() | |
75 | if (success !== true) this.dieOnConnectionFailure() | |
76 | ||
77 | logger.info('Successfully connected to SMTP server.') | |
78 | } catch (err) { | |
79 | this.dieOnConnectionFailure(err) | |
80 | } | |
81 | } | |
82 | ||
cef534ed C |
83 | addNewVideoFromSubscriberNotification (to: string[], video: VideoModel) { |
84 | const channelName = video.VideoChannel.getDisplayName() | |
85 | const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath() | |
86 | ||
ecb4e35f | 87 | const text = `Hi dear user,\n\n` + |
cef534ed C |
88 | `Your subscription ${channelName} just published a new video: ${video.name}` + |
89 | `\n\n` + | |
90 | `You can view it on ${videoUrl} ` + | |
91 | `\n\n` + | |
ecb4e35f C |
92 | `Cheers,\n` + |
93 | `PeerTube.` | |
94 | ||
95 | const emailPayload: EmailPayload = { | |
cef534ed C |
96 | to, |
97 | subject: channelName + ' just published a new video', | |
ecb4e35f C |
98 | text |
99 | } | |
100 | ||
101 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
102 | } | |
103 | ||
328c78bc RK |
104 | addForceResetPasswordEmailJob (to: string, resetPasswordUrl: string) { |
105 | const text = `Hi dear user,\n\n` + | |
106 | `Your password has been reset on ${CONFIG.WEBSERVER.HOST}! ` + | |
107 | `Please follow this link to reset it: ${resetPasswordUrl}\n\n` + | |
108 | `Cheers,\n` + | |
109 | `PeerTube.` | |
110 | ||
111 | const emailPayload: EmailPayload = { | |
112 | to: [ to ], | |
113 | subject: 'Reset of your PeerTube password', | |
114 | text | |
115 | } | |
116 | ||
117 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
118 | } | |
119 | ||
f7cc67b4 C |
120 | addNewFollowNotification (to: string[], actorFollow: ActorFollowModel, followType: 'account' | 'channel') { |
121 | const followerName = actorFollow.ActorFollower.Account.getDisplayName() | |
122 | const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName() | |
123 | ||
124 | const text = `Hi dear user,\n\n` + | |
125 | `Your ${followType} ${followingName} has a new subscriber: ${followerName}` + | |
126 | `\n\n` + | |
127 | `Cheers,\n` + | |
128 | `PeerTube.` | |
129 | ||
130 | const emailPayload: EmailPayload = { | |
131 | to, | |
132 | subject: 'New follower on your channel ' + followingName, | |
133 | text | |
134 | } | |
135 | ||
136 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
137 | } | |
138 | ||
dc133480 C |
139 | myVideoPublishedNotification (to: string[], video: VideoModel) { |
140 | const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath() | |
141 | ||
142 | const text = `Hi dear user,\n\n` + | |
143 | `Your video ${video.name} has been published.` + | |
144 | `\n\n` + | |
145 | `You can view it on ${videoUrl} ` + | |
146 | `\n\n` + | |
147 | `Cheers,\n` + | |
148 | `PeerTube.` | |
149 | ||
150 | const emailPayload: EmailPayload = { | |
151 | to, | |
152 | subject: `Your video ${video.name} is published`, | |
153 | text | |
154 | } | |
155 | ||
156 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
157 | } | |
158 | ||
159 | myVideoImportSuccessNotification (to: string[], videoImport: VideoImportModel) { | |
160 | const videoUrl = CONFIG.WEBSERVER.URL + videoImport.Video.getWatchStaticPath() | |
161 | ||
162 | const text = `Hi dear user,\n\n` + | |
163 | `Your video import ${videoImport.getTargetIdentifier()} is finished.` + | |
164 | `\n\n` + | |
165 | `You can view the imported video on ${videoUrl} ` + | |
166 | `\n\n` + | |
167 | `Cheers,\n` + | |
168 | `PeerTube.` | |
169 | ||
170 | const emailPayload: EmailPayload = { | |
171 | to, | |
172 | subject: `Your video import ${videoImport.getTargetIdentifier()} is finished`, | |
173 | text | |
174 | } | |
175 | ||
176 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
177 | } | |
178 | ||
179 | myVideoImportErrorNotification (to: string[], videoImport: VideoImportModel) { | |
180 | const importUrl = CONFIG.WEBSERVER.URL + '/my-account/video-imports' | |
181 | ||
182 | const text = `Hi dear user,\n\n` + | |
183 | `Your video import ${videoImport.getTargetIdentifier()} encountered an error.` + | |
184 | `\n\n` + | |
185 | `See your videos import dashboard for more information: ${importUrl}` + | |
186 | `\n\n` + | |
187 | `Cheers,\n` + | |
188 | `PeerTube.` | |
189 | ||
190 | const emailPayload: EmailPayload = { | |
191 | to, | |
192 | subject: `Your video import ${videoImport.getTargetIdentifier()} encountered an error`, | |
193 | text | |
194 | } | |
195 | ||
196 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
197 | } | |
198 | ||
cef534ed C |
199 | addNewCommentOnMyVideoNotification (to: string[], comment: VideoCommentModel) { |
200 | const accountName = comment.Account.getDisplayName() | |
201 | const video = comment.Video | |
202 | const commentUrl = CONFIG.WEBSERVER.URL + comment.getCommentStaticPath() | |
203 | ||
204 | const text = `Hi dear user,\n\n` + | |
205 | `A new comment has been posted by ${accountName} on your video ${video.name}` + | |
206 | `\n\n` + | |
207 | `You can view it on ${commentUrl} ` + | |
208 | `\n\n` + | |
d9eaee39 JM |
209 | `Cheers,\n` + |
210 | `PeerTube.` | |
211 | ||
212 | const emailPayload: EmailPayload = { | |
cef534ed C |
213 | to, |
214 | subject: 'New comment on your video ' + video.name, | |
d9eaee39 JM |
215 | text |
216 | } | |
217 | ||
218 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
219 | } | |
220 | ||
f7cc67b4 C |
221 | addNewCommentMentionNotification (to: string[], comment: VideoCommentModel) { |
222 | const accountName = comment.Account.getDisplayName() | |
223 | const video = comment.Video | |
224 | const commentUrl = CONFIG.WEBSERVER.URL + comment.getCommentStaticPath() | |
225 | ||
226 | const text = `Hi dear user,\n\n` + | |
227 | `${accountName} mentioned you on video ${video.name}` + | |
228 | `\n\n` + | |
229 | `You can view the comment on ${commentUrl} ` + | |
230 | `\n\n` + | |
231 | `Cheers,\n` + | |
232 | `PeerTube.` | |
233 | ||
234 | const emailPayload: EmailPayload = { | |
235 | to, | |
236 | subject: 'Mention on video ' + video.name, | |
237 | text | |
238 | } | |
239 | ||
240 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
241 | } | |
242 | ||
243 | addVideoAbuseModeratorsNotification (to: string[], videoAbuse: VideoAbuseModel) { | |
cef534ed | 244 | const videoUrl = CONFIG.WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath() |
ba75d268 C |
245 | |
246 | const text = `Hi,\n\n` + | |
cef534ed | 247 | `${CONFIG.WEBSERVER.HOST} received an abuse for the following video ${videoUrl}\n\n` + |
ba75d268 C |
248 | `Cheers,\n` + |
249 | `PeerTube.` | |
250 | ||
ba75d268 C |
251 | const emailPayload: EmailPayload = { |
252 | to, | |
253 | subject: '[PeerTube] Received a video abuse', | |
254 | text | |
255 | } | |
256 | ||
257 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
258 | } | |
259 | ||
f7cc67b4 C |
260 | addNewUserRegistrationNotification (to: string[], user: UserModel) { |
261 | const text = `Hi,\n\n` + | |
262 | `User ${user.username} just registered on ${CONFIG.WEBSERVER.HOST} PeerTube instance.\n\n` + | |
263 | `Cheers,\n` + | |
264 | `PeerTube.` | |
265 | ||
266 | const emailPayload: EmailPayload = { | |
267 | to, | |
268 | subject: '[PeerTube] New user registration on ' + CONFIG.WEBSERVER.HOST, | |
269 | text | |
270 | } | |
271 | ||
272 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
273 | } | |
274 | ||
275 | addVideoBlacklistNotification (to: string[], videoBlacklist: VideoBlacklistModel) { | |
cef534ed C |
276 | const videoName = videoBlacklist.Video.name |
277 | const videoUrl = CONFIG.WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath() | |
26b7305a | 278 | |
cef534ed C |
279 | const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : '' |
280 | const blockedString = `Your video ${videoName} (${videoUrl} on ${CONFIG.WEBSERVER.HOST} has been blacklisted${reasonString}.` | |
26b7305a C |
281 | |
282 | const text = 'Hi,\n\n' + | |
283 | blockedString + | |
284 | '\n\n' + | |
285 | 'Cheers,\n' + | |
286 | `PeerTube.` | |
287 | ||
26b7305a | 288 | const emailPayload: EmailPayload = { |
cef534ed C |
289 | to, |
290 | subject: `[PeerTube] Video ${videoName} blacklisted`, | |
26b7305a C |
291 | text |
292 | } | |
293 | ||
294 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
295 | } | |
296 | ||
f7cc67b4 | 297 | addVideoUnblacklistNotification (to: string[], video: VideoModel) { |
cef534ed | 298 | const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath() |
26b7305a C |
299 | |
300 | const text = 'Hi,\n\n' + | |
cef534ed | 301 | `Your video ${video.name} (${videoUrl}) on ${CONFIG.WEBSERVER.HOST} has been unblacklisted.` + |
26b7305a C |
302 | '\n\n' + |
303 | 'Cheers,\n' + | |
304 | `PeerTube.` | |
305 | ||
26b7305a | 306 | const emailPayload: EmailPayload = { |
cef534ed | 307 | to, |
26b7305a C |
308 | subject: `[PeerTube] Video ${video.name} unblacklisted`, |
309 | text | |
310 | } | |
311 | ||
312 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
313 | } | |
314 | ||
cef534ed C |
315 | addForgetPasswordEmailJob (to: string, resetPasswordUrl: string) { |
316 | const text = `Hi dear user,\n\n` + | |
317 | `It seems you forgot your password on ${CONFIG.WEBSERVER.HOST}! ` + | |
318 | `Please follow this link to reset it: ${resetPasswordUrl}\n\n` + | |
319 | `If you are not the person who initiated this request, please ignore this email.\n\n` + | |
320 | `Cheers,\n` + | |
321 | `PeerTube.` | |
322 | ||
323 | const emailPayload: EmailPayload = { | |
324 | to: [ to ], | |
325 | subject: 'Reset your PeerTube password', | |
326 | text | |
327 | } | |
328 | ||
329 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
330 | } | |
331 | ||
332 | addVerifyEmailJob (to: string, verifyEmailUrl: string) { | |
333 | const text = `Welcome to PeerTube,\n\n` + | |
334 | `To start using PeerTube on ${CONFIG.WEBSERVER.HOST} you must verify your email! ` + | |
335 | `Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` + | |
336 | `If you are not the person who initiated this request, please ignore this email.\n\n` + | |
337 | `Cheers,\n` + | |
338 | `PeerTube.` | |
339 | ||
340 | const emailPayload: EmailPayload = { | |
341 | to: [ to ], | |
342 | subject: 'Verify your PeerTube email', | |
343 | text | |
344 | } | |
345 | ||
346 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
347 | } | |
348 | ||
eacb25c4 C |
349 | addUserBlockJob (user: UserModel, blocked: boolean, reason?: string) { |
350 | const reasonString = reason ? ` for the following reason: ${reason}` : '' | |
351 | const blockedWord = blocked ? 'blocked' : 'unblocked' | |
352 | const blockedString = `Your account ${user.username} on ${CONFIG.WEBSERVER.HOST} has been ${blockedWord}${reasonString}.` | |
353 | ||
354 | const text = 'Hi,\n\n' + | |
355 | blockedString + | |
356 | '\n\n' + | |
357 | 'Cheers,\n' + | |
358 | `PeerTube.` | |
359 | ||
360 | const to = user.email | |
361 | const emailPayload: EmailPayload = { | |
362 | to: [ to ], | |
363 | subject: '[PeerTube] Account ' + blockedWord, | |
364 | text | |
365 | } | |
366 | ||
367 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
368 | } | |
369 | ||
a4101923 C |
370 | addContactFormJob (fromEmail: string, fromName: string, body: string) { |
371 | const text = 'Hello dear admin,\n\n' + | |
372 | fromName + ' sent you a message' + | |
373 | '\n\n---------------------------------------\n\n' + | |
374 | body + | |
375 | '\n\n---------------------------------------\n\n' + | |
376 | 'Cheers,\n' + | |
377 | 'PeerTube.' | |
378 | ||
379 | const emailPayload: EmailPayload = { | |
380 | from: fromEmail, | |
381 | to: [ CONFIG.ADMIN.EMAIL ], | |
382 | subject: '[PeerTube] Contact form submitted', | |
383 | text | |
384 | } | |
385 | ||
386 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
387 | } | |
388 | ||
389 | sendMail (to: string[], subject: string, text: string, from?: string) { | |
d3e56c0c | 390 | if (!Emailer.isEnabled()) { |
ecb4e35f C |
391 | throw new Error('Cannot send mail because SMTP is not configured.') |
392 | } | |
393 | ||
394 | return this.transporter.sendMail({ | |
a4101923 | 395 | from: from || CONFIG.SMTP.FROM_ADDRESS, |
ecb4e35f C |
396 | to: to.join(','), |
397 | subject, | |
398 | text | |
399 | }) | |
400 | } | |
401 | ||
402 | private dieOnConnectionFailure (err?: Error) { | |
d5b7d911 | 403 | logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err }) |
ecb4e35f C |
404 | process.exit(-1) |
405 | } | |
406 | ||
407 | static get Instance () { | |
408 | return this.instance || (this.instance = new this()) | |
409 | } | |
410 | } | |
411 | ||
412 | // --------------------------------------------------------------------------- | |
413 | ||
414 | export { | |
415 | Emailer | |
416 | } |