Add sendmail

This commit is contained in:
Ismaël Bouya 2019-02-13 12:16:27 +01:00 committed by Chocobozzz
parent 4d9ae8f7cf
commit ed3f089cc7
4 changed files with 50 additions and 24 deletions

View File

@ -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

View File

@ -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

View File

@ -36,6 +36,8 @@ const CONFIG = {
DB: config.has('redis.db') ? config.get<number>('redis.db') : null
},
SMTP: {
TRANSPORT: config.has('smtp.transport') ? config.get<string>('smtp.transport') : 'smtp',
SENDMAIL: config.has('smtp.sendmail') ? config.get<string>('smtp.sendmail') : null,
HOSTNAME: config.get<string>('smtp.hostname'),
PORT: config.get<number>('smtp.port'),
USERNAME: config.get<string>('smtp.username'),

View File

@ -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)
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 +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...')