Skip to content

Instantly share code, notes, and snippets.

@gnz00
Last active March 17, 2016 13:14
Show Gist options
  • Save gnz00/5390485e676ee3745960 to your computer and use it in GitHub Desktop.
Save gnz00/5390485e676ee3745960 to your computer and use it in GitHub Desktop.
One approach to a mail adapter in ParseServer
// (A user's implementation of Parse Server)
// We can override the default templates with whichever templates we'd like
import { ParseServer, MailgunAdapter, SendGridAdapter } from "parse-server";
import "dust" from "dustjs-linkedin";
// Pretty much every templating language supports compiling templates into functions
const myMailTemplates = {
"password_reset": {
"html": require('./views/password_reset_html.js')(dust), // Precompiled dust template
"text": require('./views/password_reset_text.js')(dust)
}
};
const server = new ParseServer({
...
mail: {
templates: myMailTemplates,
adapter: MailgunAdapter,
options: {
apiKey: "mailgun1234",
defaults: {
from: "[email protected]",
bcc: ["[email protected]"],
}
}
}
...
});
// At some later point I can also update the templates
server.mailProvider.getAdapter().setTemplates({ ... });
// Or setup a different mail service
server.setupMailService({
options: {
apiKey: "mailgun1234",
defaults: {
from: "[email protected]",
bcc: ["[email protected]"],
}
},
templates: myMailTemplates,
adapter: SendGridAdapter
});
// Or just set the adapter manually
server.mailProvider.setAdapter(
new MailgunAdapter({
options: {
apiKey: "mailgun1234",
defaults: {}
},
templates: null // Use the defaults, can also just leave this undefined
})
);
// ParseServer.js
import { default as MailProvider } from 'providers/MailProvider';
import { default as DEFAULT_MAIL_ADAPTER } from 'adapters/MailgunAdapter';
// These default templates live in the parse-server repo
const DEFAULT_MAIL_TEMPLATES = {
"password_reset": {
html: jade.compileFile("views/mail/password_reset.html.jade"),
text: jade.compileFile("views/mail/password_reset.txt.jade")
},
"email_verification": {
html: jade.compileFile("views/mail/email_verification.html.jade"),
text: jade.compileFile("views/mail/email_verification.txt.jade")
}
};
class ParseServer {
constructor(args) {
...
this.setupMailProvider(args.mail);
...
}
...
setupMailProvider(config) {
// Merge the default templates with the overrides
let templates = Object.assign({}, DEFAULT_MAIL_TEMPLATES, config.templates);
let adapterClass = config.adapter || DEFAULT_MAIL_ADAPTER;
let adapter = new adapterClass({ options: config.options, templates: templates });
this.mailProvider = new MailProvider(adapter);
}
...
}
// Some implementation, i.e. users.js sendPasswordReset
...
// This example assumes we've injected an instance of ParseServer to the request context.
// We can also continue using the singleton pattern, i.e. new MailProvider() would return the existing instance
// I don't really care, as long as we're explicit about it (not using modules.exports load order to enforce a singleton like we currently are for cache.js);
const mailAdapter = req.Parse.Server.mailProvider.getAdapter();
// Using a precompiled template
mailAdapter.sendMessage({
to: req.user.get('email'),
subject: "Reset yo' password",
templateName: "password_reset",
locals: { password_reset_link: "https://parse.com/verify?abcd1234" }
});
// Compiling a template a run time
mailAdapter.sendMessage({
to: req.user.get('email'),
from: null, // use default
cc: null, // use default
bcc: null // use default
subject: "Reset yo' password",
template: { html: jade.compileFile("views/password_reset.html.jade") },
locals: { password_reset_link: "https://parse.com/verify?abcd1234" }
});
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment