]>
Commit | Line | Data |
---|---|---|
d95d1559 | 1 | import { readFileSync } from 'fs-extra' |
d26836cd | 2 | import { isArray, merge } from 'lodash' |
ecb4e35f | 3 | import { createTransport, Transporter } from 'nodemailer' |
d95d1559 | 4 | import { join } from 'path' |
d26836cd | 5 | import { EmailPayload } from '@shared/models' |
cae2df6b | 6 | import { SendEmailDefaultOptions } from '../../shared/models/server/emailer.model' |
06aad801 | 7 | import { isTestInstance } from '../helpers/core-utils' |
8 | import { root } from '@shared/core-utils' | |
05e67d62 | 9 | import { bunyanLogger, logger } from '../helpers/logger' |
4c1c1709 | 10 | import { CONFIG, isEmailEnabled } from '../initializers/config' |
6dd9de95 | 11 | import { WEBSERVER } from '../initializers/constants' |
d26836cd | 12 | import { MUser } from '../types/models' |
d95d1559 | 13 | import { JobQueue } from './job-queue' |
98b94643 | 14 | |
df4c603d | 15 | const Email = require('email-templates') |
dee77e76 | 16 | |
ecb4e35f C |
17 | class Emailer { |
18 | ||
19 | private static instance: Emailer | |
20 | private initialized = false | |
21 | private transporter: Transporter | |
22 | ||
a1587156 C |
23 | private constructor () { |
24 | } | |
ecb4e35f C |
25 | |
26 | init () { | |
27 | // Already initialized | |
28 | if (this.initialized === true) return | |
29 | this.initialized = true | |
30 | ||
448487a6 | 31 | if (!isEmailEnabled()) { |
ecb4e35f C |
32 | if (!isTestInstance()) { |
33 | logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!') | |
34 | } | |
ecb4e35f | 35 | |
448487a6 | 36 | return |
ed3f089c | 37 | } |
448487a6 C |
38 | |
39 | if (CONFIG.SMTP.TRANSPORT === 'smtp') this.initSMTPTransport() | |
40 | else if (CONFIG.SMTP.TRANSPORT === 'sendmail') this.initSendmailTransport() | |
3b3b1820 C |
41 | } |
42 | ||
75594f47 | 43 | async checkConnection () { |
ed3f089c | 44 | if (!this.transporter || CONFIG.SMTP.TRANSPORT !== 'smtp') return |
ecb4e35f | 45 | |
3d3441d6 C |
46 | logger.info('Testing SMTP server...') |
47 | ||
ecb4e35f C |
48 | try { |
49 | const success = await this.transporter.verify() | |
75594f47 | 50 | if (success !== true) this.warnOnConnectionFailure() |
ecb4e35f C |
51 | |
52 | logger.info('Successfully connected to SMTP server.') | |
53 | } catch (err) { | |
75594f47 | 54 | this.warnOnConnectionFailure(err) |
ecb4e35f C |
55 | } |
56 | } | |
57 | ||
963023ab | 58 | addPasswordResetEmailJob (username: string, to: string, resetPasswordUrl: string) { |
cef534ed | 59 | const emailPayload: EmailPayload = { |
df4c603d | 60 | template: 'password-reset', |
cef534ed | 61 | to: [ to ], |
df4c603d RK |
62 | subject: 'Reset your account password', |
63 | locals: { | |
963023ab | 64 | username, |
df4c603d RK |
65 | resetPasswordUrl |
66 | } | |
cef534ed C |
67 | } |
68 | ||
69 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
70 | } | |
71 | ||
df4c603d | 72 | addPasswordCreateEmailJob (username: string, to: string, createPasswordUrl: string) { |
45f1bd72 | 73 | const emailPayload: EmailPayload = { |
df4c603d | 74 | template: 'password-create', |
45f1bd72 | 75 | to: [ to ], |
df4c603d RK |
76 | subject: 'Create your account password', |
77 | locals: { | |
78 | username, | |
79 | createPasswordUrl | |
80 | } | |
45f1bd72 JL |
81 | } |
82 | ||
83 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
84 | } | |
85 | ||
963023ab | 86 | addVerifyEmailJob (username: string, to: string, verifyEmailUrl: string) { |
cef534ed | 87 | const emailPayload: EmailPayload = { |
df4c603d | 88 | template: 'verify-email', |
cef534ed | 89 | to: [ to ], |
2e4b8ae4 | 90 | subject: `Verify your email on ${CONFIG.INSTANCE.NAME}`, |
df4c603d | 91 | locals: { |
963023ab | 92 | username, |
df4c603d RK |
93 | verifyEmailUrl |
94 | } | |
cef534ed C |
95 | } |
96 | ||
97 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
98 | } | |
99 | ||
453e83ea | 100 | addUserBlockJob (user: MUser, blocked: boolean, reason?: string) { |
eacb25c4 C |
101 | const reasonString = reason ? ` for the following reason: ${reason}` : '' |
102 | const blockedWord = blocked ? 'blocked' : 'unblocked' | |
eacb25c4 C |
103 | |
104 | const to = user.email | |
105 | const emailPayload: EmailPayload = { | |
106 | to: [ to ], | |
df4c603d | 107 | subject: 'Account ' + blockedWord, |
2e4b8ae4 | 108 | text: `Your account ${user.username} on ${CONFIG.INSTANCE.NAME} has been ${blockedWord}${reasonString}.` |
eacb25c4 C |
109 | } |
110 | ||
111 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
112 | } | |
113 | ||
4e9fa5b7 | 114 | addContactFormJob (fromEmail: string, fromName: string, subject: string, body: string) { |
a4101923 | 115 | const emailPayload: EmailPayload = { |
df4c603d | 116 | template: 'contact-form', |
a4101923 | 117 | to: [ CONFIG.ADMIN.EMAIL ], |
df4c603d RK |
118 | replyTo: `"${fromName}" <${fromEmail}>`, |
119 | subject: `(contact form) ${subject}`, | |
120 | locals: { | |
121 | fromName, | |
122 | fromEmail, | |
b9cf3fb6 C |
123 | body, |
124 | ||
125 | // There are not notification preferences for the contact form | |
126 | hideNotificationPreferences: true | |
df4c603d | 127 | } |
a4101923 C |
128 | } |
129 | ||
130 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
131 | } | |
132 | ||
47f6cb31 | 133 | async sendMail (options: EmailPayload) { |
4c1c1709 | 134 | if (!isEmailEnabled()) { |
ecb4e35f C |
135 | throw new Error('Cannot send mail because SMTP is not configured.') |
136 | } | |
137 | ||
df4c603d RK |
138 | const fromDisplayName = options.from |
139 | ? options.from | |
2e4b8ae4 | 140 | : CONFIG.INSTANCE.NAME |
4759fedc | 141 | |
df4c603d RK |
142 | const email = new Email({ |
143 | send: true, | |
3c7ddd7d C |
144 | htmlToText: { |
145 | selectors: [ | |
146 | { selector: 'img', format: 'skip' }, | |
e85a36cb | 147 | { selector: 'a', options: { hideLinkHrefIfSameAsText: true } } |
3c7ddd7d C |
148 | ] |
149 | }, | |
df4c603d RK |
150 | message: { |
151 | from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>` | |
152 | }, | |
153 | transport: this.transporter, | |
154 | views: { | |
03fc1928 | 155 | root: join(root(), 'dist', 'server', 'lib', 'emails') |
df4c603d RK |
156 | }, |
157 | subjectPrefix: CONFIG.EMAIL.SUBJECT.PREFIX | |
158 | }) | |
159 | ||
d26836cd C |
160 | const toEmails = isArray(options.to) |
161 | ? options.to | |
162 | : [ options.to ] | |
163 | ||
164 | for (const to of toEmails) { | |
cae2df6b C |
165 | const baseOptions: SendEmailDefaultOptions = { |
166 | template: 'common', | |
167 | message: { | |
168 | to, | |
169 | from: options.from, | |
170 | subject: options.subject, | |
171 | replyTo: options.replyTo | |
172 | }, | |
173 | locals: { // default variables available in all templates | |
174 | WEBSERVER, | |
175 | EMAIL: CONFIG.EMAIL, | |
176 | instanceName: CONFIG.INSTANCE.NAME, | |
177 | text: options.text, | |
178 | subject: options.subject | |
179 | } | |
180 | } | |
181 | ||
182 | // overriden/new variables given for a specific template in the payload | |
183 | const sendOptions = merge(baseOptions, options) | |
184 | ||
185 | await email.send(sendOptions) | |
03fc1928 C |
186 | .then(res => logger.debug('Sent email.', { res })) |
187 | .catch(err => logger.error('Error in email sender.', { err })) | |
47f6cb31 | 188 | } |
ecb4e35f C |
189 | } |
190 | ||
75594f47 | 191 | private warnOnConnectionFailure (err?: Error) { |
d5b7d911 | 192 | logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err }) |
ecb4e35f C |
193 | } |
194 | ||
448487a6 C |
195 | private initSMTPTransport () { |
196 | logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT) | |
197 | ||
198 | let tls | |
199 | if (CONFIG.SMTP.CA_FILE) { | |
200 | tls = { | |
201 | ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ] | |
202 | } | |
203 | } | |
204 | ||
205 | let auth | |
206 | if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) { | |
207 | auth = { | |
208 | user: CONFIG.SMTP.USERNAME, | |
209 | pass: CONFIG.SMTP.PASSWORD | |
210 | } | |
211 | } | |
212 | ||
213 | this.transporter = createTransport({ | |
214 | host: CONFIG.SMTP.HOSTNAME, | |
215 | port: CONFIG.SMTP.PORT, | |
216 | secure: CONFIG.SMTP.TLS, | |
217 | debug: CONFIG.LOG.LEVEL === 'debug', | |
218 | logger: bunyanLogger as any, | |
219 | ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS, | |
220 | tls, | |
221 | auth | |
222 | }) | |
223 | } | |
224 | ||
225 | private initSendmailTransport () { | |
226 | logger.info('Using sendmail to send emails') | |
227 | ||
228 | this.transporter = createTransport({ | |
229 | sendmail: true, | |
230 | newline: 'unix', | |
b7a27f28 | 231 | path: CONFIG.SMTP.SENDMAIL, |
40a8c0a4 | 232 | logger: bunyanLogger |
448487a6 C |
233 | }) |
234 | } | |
235 | ||
ecb4e35f C |
236 | static get Instance () { |
237 | return this.instance || (this.instance = new this()) | |
238 | } | |
239 | } | |
240 | ||
241 | // --------------------------------------------------------------------------- | |
242 | ||
243 | export { | |
8dc8a34e | 244 | Emailer |
ecb4e35f | 245 | } |