]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/emailer.ts
Add plugin auth migrations
[github/Chocobozzz/PeerTube.git] / server / lib / emailer.ts
index 9ce6186b155ebea5049666851d70b431794ecb33..45d57fd28a7a7b3479c20fc90587f851466f8149 100644 (file)
@@ -1,9 +1,8 @@
 import { createTransport, Transporter } from 'nodemailer'
 import { isTestInstance } from '../helpers/core-utils'
 import { bunyanLogger, logger } from '../helpers/logger'
-import { CONFIG } from '../initializers/config'
+import { CONFIG, isEmailEnabled } from '../initializers/config'
 import { JobQueue } from './job-queue'
-import { EmailPayload } from './job-queue/handlers/email'
 import { readFileSync } from 'fs-extra'
 import { WEBSERVER } from '../initializers/constants'
 import {
@@ -16,15 +15,7 @@ import {
 } from '../typings/models/video'
 import { MActorFollowActors, MActorFollowFull, MUser } from '../typings/models'
 import { MVideoImport, MVideoImportVideo } from '@server/typings/models/video/video-import'
-
-type SendEmailOptions = {
-  to: string[]
-  subject: string
-  text: string
-
-  fromDisplayName?: string
-  replyTo?: string
-}
+import { EmailPayload } from '@shared/models'
 
 class Emailer {
 
@@ -40,34 +31,44 @@ class Emailer {
     if (this.initialized === true) return
     this.initialized = true
 
-    if (Emailer.isEnabled()) {
-      logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
+    if (isEmailEnabled()) {
+      if (CONFIG.SMTP.TRANSPORT === 'smtp') {
+        logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
 
-      let tls
-      if (CONFIG.SMTP.CA_FILE) {
-        tls = {
-          ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ]
+        let tls
+        if (CONFIG.SMTP.CA_FILE) {
+          tls = {
+            ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ]
+          }
         }
-      }
 
-      let auth
-      if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) {
-        auth = {
-          user: CONFIG.SMTP.USERNAME,
-          pass: CONFIG.SMTP.PASSWORD
+        let auth
+        if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) {
+          auth = {
+            user: CONFIG.SMTP.USERNAME,
+            pass: CONFIG.SMTP.PASSWORD
+          }
         }
-      }
 
-      this.transporter = createTransport({
-        host: CONFIG.SMTP.HOSTNAME,
-        port: CONFIG.SMTP.PORT,
-        secure: CONFIG.SMTP.TLS,
-        debug: CONFIG.LOG.LEVEL === 'debug',
-        logger: bunyanLogger as any,
-        ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS,
-        tls,
-        auth
-      })
+        this.transporter = createTransport({
+          host: CONFIG.SMTP.HOSTNAME,
+          port: CONFIG.SMTP.PORT,
+          secure: CONFIG.SMTP.TLS,
+          debug: CONFIG.LOG.LEVEL === 'debug',
+          logger: bunyanLogger as any,
+          ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS,
+          tls,
+          auth
+        })
+      } else { // sendmail
+        logger.info('Using sendmail to send emails')
+
+        this.transporter = createTransport({
+          sendmail: true,
+          newline: 'unix',
+          path: CONFIG.SMTP.SENDMAIL
+        })
+      }
     } else {
       if (!isTestInstance()) {
         logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!')
@@ -76,11 +77,17 @@ class Emailer {
   }
 
   static isEnabled () {
-    return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT
+    if (CONFIG.SMTP.TRANSPORT === 'sendmail') {
+      return !!CONFIG.SMTP.SENDMAIL
+    } else if (CONFIG.SMTP.TRANSPORT === 'smtp') {
+      return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT
+    } else {
+      return false
+    }
   }
 
   async checkConnectionOrDie () {
-    if (!this.transporter) return
+    if (!this.transporter || CONFIG.SMTP.TRANSPORT !== 'smtp') return
 
     logger.info('Testing SMTP server...')
 
@@ -276,7 +283,7 @@ class Emailer {
     const videoUrl = WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath()
 
     const text = 'Hi,\n\n' +
-      `${WEBSERVER.HOST} received an abuse for the following video ${videoUrl}\n\n` +
+      `${WEBSERVER.HOST} received an abuse for the following video: ${videoUrl}\n\n` +
       'Cheers,\n' +
       `${CONFIG.EMAIL.BODY.SIGNATURE}`
 
@@ -384,6 +391,22 @@ class Emailer {
     return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
   }
 
+  addPasswordCreateEmailJob (username: string, to: string, resetPasswordUrl: string) {
+    const text = 'Hi,\n\n' +
+      `Welcome to your ${WEBSERVER.HOST} PeerTube instance. Your username is: ${username}.\n\n` +
+      `Please set your password by following this link: ${resetPasswordUrl} (this link will expire within seven days).\n\n` +
+      'Cheers,\n' +
+      `${CONFIG.EMAIL.BODY.SIGNATURE}`
+
+    const emailPayload: EmailPayload = {
+      to: [ to ],
+      subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New PeerTube account password',
+      text
+    }
+
+    return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
+  }
+
   addVerifyEmailJob (to: string, verifyEmailUrl: string) {
     const text = 'Welcome to PeerTube,\n\n' +
       `To start using PeerTube on ${WEBSERVER.HOST} you must  verify your email! ` +
@@ -443,7 +466,7 @@ class Emailer {
   }
 
   async sendMail (options: EmailPayload) {
-    if (!Emailer.isEnabled()) {
+    if (!isEmailEnabled()) {
       throw new Error('Cannot send mail because SMTP is not configured.')
     }
 
@@ -475,6 +498,5 @@ class Emailer {
 // ---------------------------------------------------------------------------
 
 export {
-  Emailer,
-  SendEmailOptions
+  Emailer
 }