]>
Commit | Line | Data |
---|---|---|
ecb4e35f | 1 | import { createTransport, Transporter } from 'nodemailer' |
ba75d268 | 2 | import { UserRight } from '../../shared/models/users' |
ecb4e35f | 3 | import { isTestInstance } from '../helpers/core-utils' |
05e67d62 | 4 | import { bunyanLogger, logger } from '../helpers/logger' |
ecb4e35f | 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, | |
05e67d62 C |
47 | debug: CONFIG.LOG.LEVEL === 'debug', |
48 | logger: bunyanLogger as any, | |
bebf2d89 | 49 | ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS, |
ecb4e35f | 50 | tls, |
f076daa7 | 51 | auth |
ecb4e35f C |
52 | }) |
53 | } else { | |
54 | if (!isTestInstance()) { | |
55 | logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!') | |
56 | } | |
57 | } | |
58 | } | |
59 | ||
60 | async checkConnectionOrDie () { | |
61 | if (!this.transporter) return | |
62 | ||
3d3441d6 C |
63 | logger.info('Testing SMTP server...') |
64 | ||
ecb4e35f C |
65 | try { |
66 | const success = await this.transporter.verify() | |
67 | if (success !== true) this.dieOnConnectionFailure() | |
68 | ||
69 | logger.info('Successfully connected to SMTP server.') | |
70 | } catch (err) { | |
71 | this.dieOnConnectionFailure(err) | |
72 | } | |
73 | } | |
74 | ||
75 | addForgetPasswordEmailJob (to: string, resetPasswordUrl: string) { | |
76 | const text = `Hi dear user,\n\n` + | |
77 | `It seems you forgot your password on ${CONFIG.WEBSERVER.HOST}! ` + | |
37ddeba5 | 78 | `Please follow this link to reset it: ${resetPasswordUrl}\n\n` + |
ecb4e35f C |
79 | `If you are not the person who initiated this request, please ignore this email.\n\n` + |
80 | `Cheers,\n` + | |
81 | `PeerTube.` | |
82 | ||
83 | const emailPayload: EmailPayload = { | |
84 | to: [ to ], | |
85 | subject: 'Reset your PeerTube password', | |
86 | text | |
87 | } | |
88 | ||
89 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
90 | } | |
91 | ||
ba75d268 C |
92 | async addVideoAbuseReport (videoId: number) { |
93 | const video = await VideoModel.load(videoId) | |
94 | ||
95 | const text = `Hi,\n\n` + | |
96 | `Your instance received an abuse for video the following video ${video.url}\n\n` + | |
97 | `Cheers,\n` + | |
98 | `PeerTube.` | |
99 | ||
100 | const to = await UserModel.listEmailsWithRight(UserRight.MANAGE_VIDEO_ABUSES) | |
101 | const emailPayload: EmailPayload = { | |
102 | to, | |
103 | subject: '[PeerTube] Received a video abuse', | |
104 | text | |
105 | } | |
106 | ||
107 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
108 | } | |
109 | ||
ecb4e35f C |
110 | sendMail (to: string[], subject: string, text: string) { |
111 | if (!this.transporter) { | |
112 | throw new Error('Cannot send mail because SMTP is not configured.') | |
113 | } | |
114 | ||
115 | return this.transporter.sendMail({ | |
116 | from: CONFIG.SMTP.FROM_ADDRESS, | |
117 | to: to.join(','), | |
118 | subject, | |
119 | text | |
120 | }) | |
121 | } | |
122 | ||
123 | private dieOnConnectionFailure (err?: Error) { | |
d5b7d911 | 124 | logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err }) |
ecb4e35f C |
125 | process.exit(-1) |
126 | } | |
127 | ||
128 | static get Instance () { | |
129 | return this.instance || (this.instance = new this()) | |
130 | } | |
131 | } | |
132 | ||
133 | // --------------------------------------------------------------------------- | |
134 | ||
135 | export { | |
136 | Emailer | |
137 | } |