From ed3f089cc78a2153b6fc194e2b9be762b8a939e9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Wed, 13 Feb 2019 12:16:27 +0100 Subject: [PATCH] Add sendmail --- config/default.yaml | 4 +++ config/production.yaml.example | 4 +++ server/initializers/config.ts | 2 ++ server/lib/emailer.ts | 66 +++++++++++++++++++++------------- 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/config/default.yaml b/config/default.yaml index 0b096cf8d..78995b4bc 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -52,6 +52,10 @@ redis: db: 0 smtp: + # smtp or sendmail + transport: smtp + # Path to sendmail command. Required if you use sendmail transport + sendmail: null hostname: null port: 465 username: null diff --git a/config/production.yaml.example b/config/production.yaml.example index b6f7d1913..bd867be53 100644 --- a/config/production.yaml.example +++ b/config/production.yaml.example @@ -53,6 +53,10 @@ redis: # SMTP server to send emails smtp: + # smtp or sendmail + transport: smtp + # Path to sendmail command. Required if you use sendmail transport + sendmail: null hostname: null port: 465 # If you use StartTLS: 587 username: null diff --git a/server/initializers/config.ts b/server/initializers/config.ts index 2c4d26a9e..6932b41e1 100644 --- a/server/initializers/config.ts +++ b/server/initializers/config.ts @@ -36,6 +36,8 @@ const CONFIG = { DB: config.has('redis.db') ? config.get('redis.db') : null }, SMTP: { + TRANSPORT: config.has('smtp.transport') ? config.get('smtp.transport') : 'smtp', + SENDMAIL: config.has('smtp.sendmail') ? config.get('smtp.sendmail') : null, HOSTNAME: config.get('smtp.hostname'), PORT: config.get('smtp.port'), USERNAME: config.get('smtp.username'), diff --git a/server/lib/emailer.ts b/server/lib/emailer.ts index 26262972d..4c74d1ef6 100644 --- a/server/lib/emailer.ts +++ b/server/lib/emailer.ts @@ -41,33 +41,43 @@ class Emailer { this.initialized = true if (isEmailEnabled()) { - 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) ] + 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 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 +86,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...') -- 2.25.1