Add TLS toggle option to smtp configuration

This commit is contained in:
Julen Landa Alustiza 2017-03-30 19:13:57 +02:00
parent ee6aa7a6b2
commit db2c381c00
4 changed files with 30 additions and 10 deletions

View file

@ -20,6 +20,10 @@ Settings.attachSchema(new SimpleSchema({
type: String,
optional: true,
},
'mailServer.enableTLS': {
type: Boolean,
optional: true,
},
'mailServer.from': {
type: String,
optional: true,
@ -38,10 +42,11 @@ Settings.helpers({
if (!this.mailServer.host) {
return null;
}
const protocol = this.mailServer.enableTLS ? 'smtps://' : 'smtp://';
if (!this.mailServer.username && !this.mailServer.password) {
return `smtp://${this.mailServer.host}:${this.mailServer.port}/`;
return `${protocol}${this.mailServer.host}:${this.mailServer.port}/`;
}
return `smtp://${this.mailServer.username}:${this.mailServer.password}@${this.mailServer.host}:${this.mailServer.port}/`;
return `${protocol}${this.mailServer.username}:${this.mailServer.password}@${this.mailServer.host}:${this.mailServer.port}/`;
},
});
Settings.allow({
@ -62,7 +67,7 @@ if (Meteor.isServer) {
if(!setting){
const now = new Date();
const defaultSetting = {disableRegistration: false, mailServer: {
username: '', password:'', host: '', port:'', from: '',
username: '', password: '', host: '', port: '', enableTLS: false, from: '',
}, createdAt: now, modifiedAt: now};
Settings.insert(defaultSetting);
}
@ -72,11 +77,12 @@ if (Meteor.isServer) {
});
Settings.after.update((userId, doc, fieldNames) => {
// assign new values to mail-from & MAIL_URL in environment
if (_.contains(fieldNames, 'mailServer') && _.contains(fieldNames, 'host')) {
if (_.contains(fieldNames, 'mailServer') && doc.mailServer.host) {
const protocol = doc.mailServer.enableTLS ? 'smtps://' : 'smtp://';
if (!doc.mailServer.username && !doc.mailServer.password) {
process.env.MAIL_URL = `smtp://${doc.mailServer.host}:${doc.mailServer.port}/`;
process.env.MAIL_URL = `${protocol}${doc.mailServer.host}:${doc.mailServer.port}/`;
} else {
process.env.MAIL_URL = `smtp://${doc.mailServer.username}:${doc.mailServer.password}@${doc.mailServer.host}:${doc.mailServer.port}/`;
process.env.MAIL_URL = `${protocol}${doc.mailServer.username}:${doc.mailServer.password}@${doc.mailServer.host}:${doc.mailServer.port}/`;
}
Accounts.emailTemplates.from = doc.mailServer.from;
}