diff --git a/README.md b/README.md index 7e32c3b..8a005c9 100644 --- a/README.md +++ b/README.md @@ -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: 'no-reply@yourdomain.com', + // 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 \ No newline at end of file diff --git a/index.js b/index.js index 690b7dc..49137f4 100644 --- a/index.js +++ b/index.js @@ -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') { @@ -26,6 +84,8 @@ var SimpleMailgunAdapter = mailgunOptions => { } return Object.freeze({ + sendVerificationEmail: sendVerificationEmail, + sendPasswordResetEmail: sendPasswordResetEmail, sendMail: sendMail }); } diff --git a/package.json b/package.json index 7c44b1c..2eafc9a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "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": { @@ -8,7 +8,7 @@ }, "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", @@ -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 " + ], + "homepage": "https://github.com/bcomeau/parse-server-simple-mailgun-adapter#readme", "dependencies": { "mailgun-js": "^0.7.7" }