Fix email settings loading:

MAIL_URL was overriden with database info all the time.
Now if MAIL_URL exists is not overwritten and if neither MAIL_URL nor
exists valid admin panel data MAIL_URL is not set.
MAIL_FROM was ignored. Same behaviour, env variable has bigger priority
than database configuration.
On both cases, althrought environment variable is set, updating admin-panel
mail settings will load new info and ignore the environment variable.
Remove some code that is not needed anymore
This commit is contained in:
Julen Landa Alustiza 2017-04-01 21:46:09 +02:00
parent 823aea497b
commit dfeeec308a
4 changed files with 38 additions and 47 deletions

View file

@ -16,17 +16,12 @@ AccountsTemplates.addFields([{
template: 'invitationCode', template: 'invitationCode',
}]); }]);
let sendVerificationEmail = false;
if (process.env.MAIL_URL) {
sendVerificationEmail = true;
}
AccountsTemplates.configure({ AccountsTemplates.configure({
defaultLayout: 'userFormsLayout', defaultLayout: 'userFormsLayout',
defaultContentRegion: 'content', defaultContentRegion: 'content',
confirmPassword: false, confirmPassword: false,
enablePasswordChange: true, enablePasswordChange: true,
sendVerificationEmail, sendVerificationEmail: true,
showForgotPasswordLink: true, showForgotPasswordLink: true,
onLogoutHook() { onLogoutHook() {
const homePage = 'home'; const homePage = 'home';

View file

@ -27,7 +27,6 @@ Settings.attachSchema(new SimpleSchema({
'mailServer.from': { 'mailServer.from': {
type: String, type: String,
optional: true, optional: true,
defaultValue: 'Wekan',
}, },
createdAt: { createdAt: {
type: Date, type: Date,
@ -66,14 +65,17 @@ if (Meteor.isServer) {
const setting = Settings.findOne({}); const setting = Settings.findOne({});
if(!setting){ if(!setting){
const now = new Date(); const now = new Date();
const domain = process.env.ROOT_URL.match(/\/\/(?:www\.)?(.*)?(?:\/)?/)[1];
const from = `Wekan <wekan@${domain}>`;
const defaultSetting = {disableRegistration: false, mailServer: { const defaultSetting = {disableRegistration: false, mailServer: {
username: '', password: '', host: '', port: '', enableTLS: false, from: '', username: '', password: '', host: '', port: '', enableTLS: false, from,
}, createdAt: now, modifiedAt: now}; }, createdAt: now, modifiedAt: now};
Settings.insert(defaultSetting); Settings.insert(defaultSetting);
} }
const newSetting = Settings.findOne(); const newSetting = Settings.findOne();
process.env.MAIL_URL = newSetting.mailUrl(); if (!process.env.MAIL_URL && newSetting.mailUrl())
Accounts.emailTemplates.from = newSetting.mailServer.from; process.env.MAIL_URL = newSetting.mailUrl();
Accounts.emailTemplates.from = process.env.MAIL_FROM ? process.env.MAIL_FROM : newSetting.mailServer.from;
}); });
Settings.after.update((userId, doc, fieldNames) => { Settings.after.update((userId, doc, fieldNames) => {
// assign new values to mail-from & MAIL_URL in environment // assign new values to mail-from & MAIL_URL in environment
@ -106,14 +108,12 @@ if (Meteor.isServer) {
url: FlowRouter.url('sign-up'), url: FlowRouter.url('sign-up'),
}; };
const lang = author.getLanguage(); const lang = author.getLanguage();
if (Settings.findOne().mailUrl()) { Email.send({
Email.send({ to: icode.email,
to: icode.email, from: Accounts.emailTemplates.from,
from: Accounts.emailTemplates.from, subject: TAPi18n.__('email-invite-register-subject', params, lang),
subject: TAPi18n.__('email-invite-register-subject', params, lang), text: TAPi18n.__('email-invite-register-text', params, lang),
text: TAPi18n.__('email-invite-register-text', params, lang), });
});
}
} catch (e) { } catch (e) {
InvitationCodes.remove(_id); InvitationCodes.remove(_id);
throw new Meteor.Error('email-fail', e.message); throw new Meteor.Error('email-fail', e.message);

View file

@ -383,24 +383,22 @@ if (Meteor.isServer) {
board.addMember(user._id); board.addMember(user._id);
user.addInvite(boardId); user.addInvite(boardId);
if (Settings.findOne().mailUrl()) { try {
try { const params = {
const params = { user: user.username,
user: user.username, inviter: inviter.username,
inviter: inviter.username, board: board.title,
board: board.title, url: board.absoluteUrl(),
url: board.absoluteUrl(), };
}; const lang = user.getLanguage();
const lang = user.getLanguage(); Email.send({
Email.send({ to: user.emails[0].address.toLowerCase(),
to: user.emails[0].address.toLowerCase(), from: Accounts.emailTemplates.from,
from: Accounts.emailTemplates.from, subject: TAPi18n.__('email-invite-subject', params, lang),
subject: TAPi18n.__('email-invite-subject', params, lang), text: TAPi18n.__('email-invite-text', params, lang),
text: TAPi18n.__('email-invite-text', params, lang), });
}); } catch (e) {
} catch (e) { throw new Meteor.Error('email-fail', e.message);
throw new Meteor.Error('email-fail', e.message);
}
} }
return { username: user.username, email: user.emails[0].address }; return { username: user.username, email: user.emails[0].address };
}, },

View file

@ -26,17 +26,15 @@ Meteor.startup(() => {
const text = texts.join('\n\n'); const text = texts.join('\n\n');
user.clearEmailBuffer(); user.clearEmailBuffer();
if (Settings.findOne().mailUrl()) { try {
try { Email.send({
Email.send({ to: user.emails[0].address.toLowerCase(),
to: user.emails[0].address.toLowerCase(), from: Accounts.emailTemplates.from,
from: Accounts.emailTemplates.from, subject: TAPi18n.__('act-activity-notify', {}, user.getLanguage()),
subject: TAPi18n.__('act-activity-notify', {}, user.getLanguage()), text,
text, });
}); } catch (e) {
} catch (e) { return;
return;
}
} }
}, 30000); }, 30000);
}); });