diff options
author | Chocobozzz <me@florianbigard.com> | 2021-01-26 09:28:28 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-01-26 09:28:49 +0100 |
commit | 448487a60216dcfe1ae29fa581cb31c726f1441b (patch) | |
tree | 4f5aa8077e6ba7d77e3096619e4ef2ac13cb23e2 /server | |
parent | 1735ef31017102e27f2d050a3428f43d055aa110 (diff) | |
download | PeerTube-448487a60216dcfe1ae29fa581cb31c726f1441b.tar.gz PeerTube-448487a60216dcfe1ae29fa581cb31c726f1441b.tar.zst PeerTube-448487a60216dcfe1ae29fa581cb31c726f1441b.zip |
Fix sendmail emailer
Diffstat (limited to 'server')
-rw-r--r-- | server/initializers/config.ts | 6 | ||||
-rw-r--r-- | server/lib/emailer.ts | 93 |
2 files changed, 50 insertions, 49 deletions
diff --git a/server/initializers/config.ts b/server/initializers/config.ts index 1630f7f0c..ba79b4ea1 100644 --- a/server/initializers/config.ts +++ b/server/initializers/config.ts | |||
@@ -344,7 +344,11 @@ function registerConfigChangedHandler (fun: Function) { | |||
344 | } | 344 | } |
345 | 345 | ||
346 | function isEmailEnabled () { | 346 | function isEmailEnabled () { |
347 | return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT | 347 | if (CONFIG.SMTP.TRANSPORT === 'sendmail' && CONFIG.SMTP.SENDMAIL) return true |
348 | |||
349 | if (CONFIG.SMTP.TRANSPORT === 'smtp' && CONFIG.SMTP.HOSTNAME && CONFIG.SMTP.PORT) return true | ||
350 | |||
351 | return false | ||
348 | } | 352 | } |
349 | 353 | ||
350 | // --------------------------------------------------------------------------- | 354 | // --------------------------------------------------------------------------- |
diff --git a/server/lib/emailer.ts b/server/lib/emailer.ts index 5243a6029..969eae77b 100644 --- a/server/lib/emailer.ts +++ b/server/lib/emailer.ts | |||
@@ -52,59 +52,16 @@ class Emailer { | |||
52 | if (this.initialized === true) return | 52 | if (this.initialized === true) return |
53 | this.initialized = true | 53 | this.initialized = true |
54 | 54 | ||
55 | if (isEmailEnabled()) { | 55 | if (!isEmailEnabled()) { |
56 | if (CONFIG.SMTP.TRANSPORT === 'smtp') { | ||
57 | logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT) | ||
58 | |||
59 | let tls | ||
60 | if (CONFIG.SMTP.CA_FILE) { | ||
61 | tls = { | ||
62 | ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ] | ||
63 | } | ||
64 | } | ||
65 | |||
66 | let auth | ||
67 | if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) { | ||
68 | auth = { | ||
69 | user: CONFIG.SMTP.USERNAME, | ||
70 | pass: CONFIG.SMTP.PASSWORD | ||
71 | } | ||
72 | } | ||
73 | |||
74 | this.transporter = createTransport({ | ||
75 | host: CONFIG.SMTP.HOSTNAME, | ||
76 | port: CONFIG.SMTP.PORT, | ||
77 | secure: CONFIG.SMTP.TLS, | ||
78 | debug: CONFIG.LOG.LEVEL === 'debug', | ||
79 | logger: bunyanLogger as any, | ||
80 | ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS, | ||
81 | tls, | ||
82 | auth | ||
83 | }) | ||
84 | } else { // sendmail | ||
85 | logger.info('Using sendmail to send emails') | ||
86 | |||
87 | this.transporter = createTransport({ | ||
88 | sendmail: true, | ||
89 | newline: 'unix', | ||
90 | path: CONFIG.SMTP.SENDMAIL | ||
91 | }) | ||
92 | } | ||
93 | } else { | ||
94 | if (!isTestInstance()) { | 56 | if (!isTestInstance()) { |
95 | logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!') | 57 | logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!') |
96 | } | 58 | } |
97 | } | ||
98 | } | ||
99 | 59 | ||
100 | static isEnabled () { | 60 | return |
101 | if (CONFIG.SMTP.TRANSPORT === 'sendmail') { | ||
102 | return !!CONFIG.SMTP.SENDMAIL | ||
103 | } else if (CONFIG.SMTP.TRANSPORT === 'smtp') { | ||
104 | return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT | ||
105 | } else { | ||
106 | return false | ||
107 | } | 61 | } |
62 | |||
63 | if (CONFIG.SMTP.TRANSPORT === 'smtp') this.initSMTPTransport() | ||
64 | else if (CONFIG.SMTP.TRANSPORT === 'sendmail') this.initSendmailTransport() | ||
108 | } | 65 | } |
109 | 66 | ||
110 | async checkConnection () { | 67 | async checkConnection () { |
@@ -641,6 +598,46 @@ class Emailer { | |||
641 | logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err }) | 598 | logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err }) |
642 | } | 599 | } |
643 | 600 | ||
601 | private initSMTPTransport () { | ||
602 | logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT) | ||
603 | |||
604 | let tls | ||
605 | if (CONFIG.SMTP.CA_FILE) { | ||
606 | tls = { | ||
607 | ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ] | ||
608 | } | ||
609 | } | ||
610 | |||
611 | let auth | ||
612 | if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) { | ||
613 | auth = { | ||
614 | user: CONFIG.SMTP.USERNAME, | ||
615 | pass: CONFIG.SMTP.PASSWORD | ||
616 | } | ||
617 | } | ||
618 | |||
619 | this.transporter = createTransport({ | ||
620 | host: CONFIG.SMTP.HOSTNAME, | ||
621 | port: CONFIG.SMTP.PORT, | ||
622 | secure: CONFIG.SMTP.TLS, | ||
623 | debug: CONFIG.LOG.LEVEL === 'debug', | ||
624 | logger: bunyanLogger as any, | ||
625 | ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS, | ||
626 | tls, | ||
627 | auth | ||
628 | }) | ||
629 | } | ||
630 | |||
631 | private initSendmailTransport () { | ||
632 | logger.info('Using sendmail to send emails') | ||
633 | |||
634 | this.transporter = createTransport({ | ||
635 | sendmail: true, | ||
636 | newline: 'unix', | ||
637 | path: CONFIG.SMTP.SENDMAIL | ||
638 | }) | ||
639 | } | ||
640 | |||
644 | static get Instance () { | 641 | static get Instance () { |
645 | return this.instance || (this.instance = new this()) | 642 | return this.instance || (this.instance = new this()) |
646 | } | 643 | } |