Skip to content
This repository was archived by the owner on Apr 8, 2021. It is now read-only.

Email templates #8

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
# parse-server-simple-mailgun-adapter
Used to send Parse Server password reset and email verification emails though Mailgun


## How to use
```
var server = ParseServer({
...
emailAdapter: {
module: 'parse-server-simple-mailgun-adapter',
options: {
// The address that your emails come from
fromAddress: '[email protected]',
// Your domain from mailgun.com
domain: 'mg.yourdomain.com',
// Your API key from mailgun.com
apiKey: 'key-0123456789abcdefghijklmnopqrstuv',
// Verification email subject
verificationSubject: 'Please verify your e-mail for %appname%',
// Verification email body
verificationBody: 'Hi,\n\nYou are being asked to confirm the e-mail address %email% with %appname%\n\nClick here to confirm it:\n%link%',
// Password reset email subject
passwordResetSubject: 'Password Reset Request for %appname%',
// Password reset email body
passwordResetBody: 'Hi,\n\nYou requested a password reset for %appname%.\n\nClick here to reset it:\n%link%'
}
}
...
});
```

## Variables

Customize the e-mail sent to your users when they reset their password or when we verify their email address. The following variables will be automatically filled in with their appropriate values:

`%username%` the user's display name

`%email%` the user's email address

`%appname%` your application's display name

`%link%` the link the user must click to perform the requested action
64 changes: 62 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,74 @@ var SimpleMailgunAdapter = mailgunOptions => {
if (!mailgunOptions || !mailgunOptions.apiKey || !mailgunOptions.domain || !mailgunOptions.fromAddress) {
throw 'SimpleMailgunAdapter requires an API Key, domain, and fromAddress.';
}

mailgunOptions.verificationSubject =
mailgunOptions.verificationSubject ||
'Please verify your e-mail for %appname%';
mailgunOptions.verificationBody =
mailgunOptions.verificationBody ||
'Hi,\n\nYou are being asked to confirm the e-mail address %email% ' +
'with %appname%\n\nClick here to confirm it:\n%link%';
mailgunOptions.passwordResetSubject =
mailgunOptions.passwordResetSubject ||
'Password Reset Request for %appname%';
mailgunOptions.passwordResetBody =
mailgunOptions.passwordResetBody ||
'Hi,\n\nYou requested a password reset for %appname%.\n\nClick here ' +
'to reset it:\n%link%';


var mailgun = Mailgun(mailgunOptions);

function fillVariables(text, options) {
text = text.replace("%username%", options.user.get("username"));
text = text.replace("%email%", options.user.get("email"));
text = text.replace("%appname%", options.appName);
text = text.replace("%link%", options.link);
return text;
}

var sendVerificationEmail = options => {
var data = {
from: mailgunOptions.fromAddress,
to: options.user.get("email"),
subject: fillVariables(mailgunOptions.verificationSubject, options),
text: fillVariables(mailgunOptions.verificationBody, options)
}
return new Promise((resolve, reject) => {
mailgun.messages().send(data, (err, body) => {
if (typeof err !== 'undefined') {
reject(err);
}
resolve(body);
});
});
}

var sendPasswordResetEmail = options => {
var data = {
from: mailgunOptions.fromAddress,
to: options.user.get("email"),
subject: fillVariables(mailgunOptions.passwordResetSubject, options),
text: fillVariables(mailgunOptions.passwordResetBody, options)
}
return new Promise((resolve, reject) => {
mailgun.messages().send(data, (err, body) => {
if (typeof err !== 'undefined') {
reject(err);
}
resolve(body);
});
});
}

var sendMail = mail => {
var data = {
from: mailgunOptions.fromAddress,
to: mail.to,
subject: mail.subject,
text: mail.text,
text: mail.text
}

return new Promise((resolve, reject) => {
mailgun.messages().send(data, (err, body) => {
if (typeof err !== 'undefined') {
Expand All @@ -26,6 +84,8 @@ var SimpleMailgunAdapter = mailgunOptions => {
}

return Object.freeze({
sendVerificationEmail: sendVerificationEmail,
sendPasswordResetEmail: sendPasswordResetEmail,
sendMail: sendMail
});
}
Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "parse-server-simple-mailgun-adapter",
"version": "1.0.0",
"name": "parse-server-mailgun-adapter-template",
"version": "1.1.1",
"description": "Used to send Parse Server password reset and email verification emails though Mailgun",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ParsePlatform/parse-server-simple-mailgun-adapter.git"
"url": "git+https://github.com/bcomeau/parse-server-simple-mailgun-adapter.git"
},
"keywords": [
"parse",
Expand All @@ -18,9 +18,12 @@
"author": "Drew Gross",
"license": "BSD-3-Clause",
"bugs": {
"url": "https://github.com/ParsePlatform/parse-server-simple-mailgun-adapter/issues"
"url": "https://github.com/bcomeau/parse-server-simple-mailgun-adapter/issues"
},
"homepage": "https://github.com/ParsePlatform/parse-server-simple-mailgun-adapter#readme",
"contributors": [
"Benjamin Comeau <[email protected]>"
],
"homepage": "https://github.com/bcomeau/parse-server-simple-mailgun-adapter#readme",
"dependencies": {
"mailgun-js": "^0.7.7"
}
Expand Down