parse-server-mailgun
Allows your Parse Server app to send template-based emails through Mailgun. Just add your template files (plain text and html) to the email adapter's configuration.
Installation
npm install --save parse-server-mailgun
Configure test utility
To quickly test the adapter with your own Mailgun api key and domain, create a configuration file mailgun.json
in the module's root directory:
Example:
{
"apiKey": "your mailgun api key",
"fromAddress": "Team <noreply@yourdomain.com>",
"domain": "yourdomain.com",
"recipient": "your@email.com",
"username": "you",
"appName": "YourApp"
}
Then, inside the module's root directory, run node ./src/mailgun-tester
Adapter configuration
As is the case with the default Mailgun adapter that comes with the Parse Server, you need to set a fromAddres, and the domain and apiKey provided by Mailgun.
In addition, you also need to configure the templates you want to use.
You must provide at least a plain-text version for each template. The html versions are optional.
const resolve = require('path').resolve;
var server = ParseServer({
...otherOptions,
verifyUserEmails: true,
emailAdapter: {
module: 'parse-server-mailgun',
options: {
fromAddress: 'YourApp <noreply@yourapp.com>',
domain: 'example.com',
apiKey: 'key-mykey',
templates: {
passwordResetEmail: {
subject: 'Reset your password',
pathPlainText: resolve(__dirname, 'path/to/templates/password_reset_email.txt'),
pathHtml: resolve(__dirname, 'path/to/templates/password_reset_email.html'),
callback: (user) => { return { firstName: user.get('firstName') }}
},
verificationEmail: {
subject: 'Confirm your account',
pathPlainText: resolve(__dirname, 'path/to/templates/verification_email.txt'),
pathHtml: resolve(__dirname, 'path/to/templates/verification_email.html'),
callback: (user) => { return { firstName: user.get('firstName') }}
},
customEmailAlert: {
subject: 'Urgent notification!',
pathPlainText: resolve(__dirname, 'path/to/templates/custom_alert.txt'),
pathHtml: resolve(__dirname, 'path/to/templates/custom_alert.html'),
}
}
}
}
});
Templates
The Parse Server only uses the MailgunAdapter for two use cases: password reset and email address verification.
With a few lines of code, it's also possible to use the MailgunAdapter directly, so that you can send any other template-based email,
provided it has been configured as shown in the example configuration above.
const { AppCache } = require('parse-server/lib/cache');
const MailgunAdapter = AppCache.get('yourAppId').userController.adapter;
MailgunAdapter.send({
templateName: 'customEmailAlert',
subject: 'Important: action required',
fromAddress: 'Alerts <noreply@yourapp.com>',
recipient: 'user@email.com',
variables: { alert: 'New posts' }
extra: {
attachments: []
replyTo: 'reply-to-address'
}
});
Version 2.4.0 switched from templating with lodash.template to using the excellent Mustache library. This allows for a lot more flexibility in your template code. For example, you can now pass an array as one of the template variables:
MailgunAdapter.send({
variables: {
stooges: [
{ name: "Moe" },
{ name: "Larry" },
{ name: "Curly" }
]
}
});
So that your templates can iterate over it like this:
{{#stooges}}
<b>{{name}}</b>
{{/stooges}}
Which will result in the following output:
<b>Moe</b>
<b>Larry</b>
<b>Curly</b>
Sample templates and template variables
In the test directory, there are a few examples to get you started.
For password reset and address verification messages, you can use the following template variables by default:
{{{link}}}
- the reset or verification link provided by the Parse Server (rendered as unescaped content){{appName}}
- as is defined in your Parse Server configuration object{{username}}
- the Parse.User object's username property{{email}}
- the Parse.User object's email property
Additional variables can be introduced by adding a callback.
An example is shown in the configuration above. The relevant Parse.User object is passed as an argument. The return value must be a plain object where the property names exactly match their template counterparts.
Note: the callback options only applies to the password reset and email address verification use cases.
For any other use case, you use the MailgunAdapter
directly and pass any variable you need to the send
method as explained in the code sample above.