2020-05-13 01:30:11 +00:00
|
|
|
const Mail = use('Mail');
|
|
|
|
const Env = use('Env');
|
2020-05-14 01:59:40 +00:00
|
|
|
const Logger = use('Logger')
|
2020-05-13 01:30:11 +00:00
|
|
|
|
|
|
|
const from = Env.get('MAIL_FROM', 'admin@localhost.com');
|
|
|
|
const baseUrl = Env.get('APP_URL', 'http://localhost:3333');
|
|
|
|
|
|
|
|
const emailEnabled = !!Env.get('MAIL_CONNECTION', false);
|
|
|
|
|
|
|
|
class EmailUtils {
|
|
|
|
static async sendTestEmail(user) {
|
|
|
|
const to = user.email;
|
|
|
|
try {
|
2020-05-14 01:59:40 +00:00
|
|
|
await Mail.send(
|
|
|
|
['emails.test-settings', 'emails.test-settings-text'],
|
|
|
|
{user, baseUrl}, (message) => {
|
|
|
|
message.from(from).to(to).subject('Seepur | Email Settings Test');
|
|
|
|
});
|
2020-05-13 01:30:11 +00:00
|
|
|
return true;
|
|
|
|
} catch (e) {
|
2020-05-14 01:59:40 +00:00
|
|
|
Logger.error(e);
|
|
|
|
return false;
|
2020-05-13 01:30:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-14 01:59:40 +00:00
|
|
|
static async sendResetPassword(
|
|
|
|
user, code) { // TODO: Make a text version of this email
|
2020-05-13 01:30:11 +00:00
|
|
|
if (!emailEnabled) return true;
|
|
|
|
const to = user.email;
|
|
|
|
const link = {
|
2020-05-18 00:39:53 +00:00
|
|
|
href: `${baseUrl}/password/reset/${code}`,
|
2020-05-13 01:30:11 +00:00
|
|
|
text: 'Reset your password'
|
|
|
|
};
|
2020-05-14 01:59:40 +00:00
|
|
|
Logger.info(`Sending test email to ${user.email}`);
|
2020-05-13 01:30:11 +00:00
|
|
|
try {
|
|
|
|
await Mail.send(
|
2020-05-18 00:39:53 +00:00
|
|
|
['emails.reset-password', 'emails.reset-password-text'],
|
|
|
|
{user, code, link, baseUrl}, (message) => {
|
2020-05-13 01:30:11 +00:00
|
|
|
message.from(from).to(to).subject('Reset Password');
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
2020-05-14 01:59:40 +00:00
|
|
|
Logger.error(e);
|
2020-05-13 01:30:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static async sendWelcomeEmail(user) {
|
|
|
|
if (!emailEnabled) return true;
|
|
|
|
const to = user.email;
|
2020-05-14 01:59:40 +00:00
|
|
|
Logger.info(`Sending welcome email to:${to} from:${from}`);
|
2020-05-13 01:30:11 +00:00
|
|
|
try {
|
2020-05-14 01:59:40 +00:00
|
|
|
await Mail.send(
|
|
|
|
['emails.welcome', 'emails.welcome-text'], {user, baseUrl},
|
|
|
|
(message) => {
|
|
|
|
message.from(from).to(to).subject('Welcome to your Seepur');
|
|
|
|
});
|
2020-05-13 01:30:11 +00:00
|
|
|
return true;
|
|
|
|
} catch (e) {
|
2020-05-14 01:59:40 +00:00
|
|
|
Logger.error(e);
|
2020-05-13 01:30:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EmailUtils;
|