Skip to content

Instantly share code, notes, and snippets.

@WahdanZ WahdanZ/mail.js
Last active May 15, 2019

Embed
What would you like to do?
mail adapter for parse server
const sendmail = require('sendmail')({ silent: true });
const nodemailer = require('nodemailer');
class Mail {
constructor(options) {
this.transporter = null;
this.options = Object.assign({
smtp: null,
verificationSubject: 'Please activate your email address',
verificationBody: '<p>Hello %username%,</p><p>Thank you for registering %appname%</p><p>Please click on the link below to activate your account.</p><p><a href="%link%" target="_blank">%link%</a></p><p>If the link above is invalid, try copying the link to the browser address bar.</p>',
passwordResetSubject: 'Reset Password',
passwordResetBody: '<p>Hello %username%,</p><p>You have requested a password reset, you can click on the link below</p><p><a href="%link%" target="_blank">%link%</a></p><p>If you did not request a password reset, please ignore this message. </p><p>Your password will remain the same until you click the link above to change your password.'
}, options || {});
if (!this.options.fromAddress) {
throw 'Sending email is not set';
}
if (!this.options.sendmail && this.options.smtp) {
this.transporter = nodemailer.createTransport(this.options.smtp, {
from: `${this.options.fromName} <${this.options.fromAddress}>`,
});
}
}
fillVariables(text, options) {
text = text.replace(/%username%/ig, options.user.get('username'));
text = text.replace(/%email%/ig, options.user.get('email'));
text = text.replace(/%appname%/ig, options.appName);
text = text.replace(/%link%/ig, options.link);
return text;
}
emailFrom(fromAddress, fromName) {
if (fromName) {
return '=?utf-8?B?' + new Buffer(fromName).toString('base64') + '?=<' + fromAddress + '>';
}
return fromAddress;
}
sendVerificationEmail(options) {
return this.sendMail({
to: options.user.get('email'),
subject: this.fillVariables(this.options.verificationSubject, options),
html: this.fillVariables(this.options.verificationBody, options)
});
}
sendPasswordResetEmail(options) {
return this.sendMail({
to: options.user.get('email'),
subject: this.fillVariables(this.options.passwordResetSubject, options),
html: this.fillVariables(this.options.passwordResetBody, options)
});
}
sendMail(mail) {
return new Promise((resolve, reject) => {
if (this.transporter) {
this.transporter.sendMail({
to: mail.to,
subject: mail.subject,
html: mail.html || mail.text
}, (error, info) => {
if (error) {
return reject(error);
}
resolve(info);
});
} else {
sendmail({
from: this.emailFrom(this.options.fromAddress, this.options.fromName),
to: mail.to,
subject: '=?utf-8?B?' + new Buffer(mail.subject).toString('base64') + '?=',
type: 'text/html',
charset: 'utf-8',
encoding: 'base64',
html: mail.html || mail.text
}, function (err, reply) {
if (err) {
reject(err);
}
resolve(reply);
});
}
});
}
}
module.exports.Mail = Mail;
module.exports.default = Mail;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.