seepur/app/Utils/EmailUtils.js

65 lines
1.8 KiB
JavaScript

const Mail = use('Mail');
const Env = use('Env');
const Logger = use('Logger')
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 {
await Mail.send(
['emails.test-settings', 'emails.test-settings-text'],
{user, baseUrl}, (message) => {
message.from(from).to(to).subject('Seepur | Email Settings Test');
});
return true;
} catch (e) {
Logger.error(e);
return false;
}
}
static async sendResetPassword(
user, code) { // TODO: Make a text version of this email
if (!emailEnabled) return true;
const to = user.email;
const link = {
href: `${baseUrl}/password/reset/${code}`,
text: 'Reset your password'
};
Logger.info(`Sending test email to ${user.email}`);
try {
await Mail.send(
['emails.reset-password', 'emails.reset-password-text'],
{user, code, link, baseUrl}, (message) => {
message.from(from).to(to).subject('Reset Password');
});
return true;
} catch (e) {
Logger.error(e);
return false;
}
}
static async sendWelcomeEmail(user) {
if (!emailEnabled) return true;
const to = user.email;
Logger.info(`Sending welcome email to:${to} from:${from}`);
try {
await Mail.send(
['emails.welcome', 'emails.welcome-text'], {user, baseUrl},
(message) => {
message.from(from).to(to).subject('Welcome to your Seepur');
});
return true;
} catch (e) {
Logger.error(e);
return false;
}
}
}
module.exports = EmailUtils;