X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Femailer.ts;h=f384a254e017d6912a71e8063ce2c25c19aa8512;hb=73471b1a52f242e86364ffb077ea6cadb3b07ae2;hp=3429498e7d5a9d070d26ce016c5093e39ae86958;hpb=f7cc67b455a12ccae9b0ea16876d166720364357;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/emailer.ts b/server/lib/emailer.ts index 3429498e7..f384a254e 100644 --- a/server/lib/emailer.ts +++ b/server/lib/emailer.ts @@ -18,7 +18,6 @@ class Emailer { private static instance: Emailer private initialized = false private transporter: Transporter - private enabled = false private constructor () {} @@ -27,7 +26,7 @@ class Emailer { if (this.initialized === true) return this.initialized = true - if (CONFIG.SMTP.HOSTNAME && CONFIG.SMTP.PORT) { + if (Emailer.isEnabled()) { logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT) let tls @@ -55,8 +54,6 @@ class Emailer { tls, auth }) - - this.enabled = true } else { if (!isTestInstance()) { logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!') @@ -64,8 +61,8 @@ class Emailer { } } - isEnabled () { - return this.enabled + static isEnabled () { + return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT } async checkConnectionOrDie () { @@ -354,13 +351,32 @@ class Emailer { return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) } - sendMail (to: string[], subject: string, text: string) { - if (!this.enabled) { + addContactFormJob (fromEmail: string, fromName: string, body: string) { + const text = 'Hello dear admin,\n\n' + + fromName + ' sent you a message' + + '\n\n---------------------------------------\n\n' + + body + + '\n\n---------------------------------------\n\n' + + 'Cheers,\n' + + 'PeerTube.' + + const emailPayload: EmailPayload = { + from: fromEmail, + to: [ CONFIG.ADMIN.EMAIL ], + subject: '[PeerTube] Contact form submitted', + text + } + + return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) + } + + sendMail (to: string[], subject: string, text: string, from?: string) { + if (!Emailer.isEnabled()) { throw new Error('Cannot send mail because SMTP is not configured.') } return this.transporter.sendMail({ - from: CONFIG.SMTP.FROM_ADDRESS, + from: from || CONFIG.SMTP.FROM_ADDRESS, to: to.join(','), subject, text