1
0
Fork 0

Fix sendmail emailer

This commit is contained in:
Chocobozzz 2021-01-26 09:28:28 +01:00
parent 1735ef3101
commit 448487a602
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 50 additions and 49 deletions

View File

@ -344,7 +344,11 @@ function registerConfigChangedHandler (fun: Function) {
} }
function isEmailEnabled () { function isEmailEnabled () {
return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT if (CONFIG.SMTP.TRANSPORT === 'sendmail' && CONFIG.SMTP.SENDMAIL) return true
if (CONFIG.SMTP.TRANSPORT === 'smtp' && CONFIG.SMTP.HOSTNAME && CONFIG.SMTP.PORT) return true
return false
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------

View File

@ -52,59 +52,16 @@ class Emailer {
if (this.initialized === true) return if (this.initialized === true) return
this.initialized = true this.initialized = true
if (isEmailEnabled()) { 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 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
})
} else { // sendmail
logger.info('Using sendmail to send emails')
this.transporter = createTransport({
sendmail: true,
newline: 'unix',
path: CONFIG.SMTP.SENDMAIL
})
}
} else {
if (!isTestInstance()) { if (!isTestInstance()) {
logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!') logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!')
} }
}
}
static isEnabled () { return
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
} }
if (CONFIG.SMTP.TRANSPORT === 'smtp') this.initSMTPTransport()
else if (CONFIG.SMTP.TRANSPORT === 'sendmail') this.initSendmailTransport()
} }
async checkConnection () { async checkConnection () {
@ -641,6 +598,46 @@ class Emailer {
logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err }) logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err })
} }
private initSMTPTransport () {
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
}
}
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
})
}
private initSendmailTransport () {
logger.info('Using sendmail to send emails')
this.transporter = createTransport({
sendmail: true,
newline: 'unix',
path: CONFIG.SMTP.SENDMAIL
})
}
static get Instance () { static get Instance () {
return this.instance || (this.instance = new this()) return this.instance || (this.instance = new this())
} }