Last active
February 17, 2016 15:21
-
-
Save taylorstine/d280d5ddcad665fba595 to your computer and use it in GitHub Desktop.
emailSender function for parse-server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aws from 'aws-sdk' | |
import path from 'path' | |
import fs from 'fs' | |
aws.config.loadFromPath(path.resolve('credentials', 'aws_config.json')); | |
const ses = new aws.SES({apiVersion: '2010-12-01'}); | |
const resetEmail = fs.readFileSync(path.resolve('emails', 'reset-password.html'), 'utf8'); | |
const verifyEmail = fs.readFileSync(path.resolve('emails', 'verify-email.html'), 'utf8'); | |
import {Constants} from 'parse-server' | |
import Promise from 'bluebird' | |
export function sendEmail(type:string, link:string, email:string):Promise { | |
let emailContent:?string = null; | |
let subject:?string = null; | |
switch (type) { | |
case Constants.RESET_PASSWORD: | |
emailContent = resetEmail.replace('<%LINK_GOES_HERE%>', link); | |
subject = "Reset App password"; | |
break; | |
case Constants.VERIFY_EMAIL: | |
emailContent = verifyEmail.replace('<%LINK_GOES_HERE%>', link); | |
subject = "Verify App email"; | |
break; | |
default: | |
throw new Error("Invalid email type: " + type) | |
} | |
return new Promise((resolve, reject)=>{ | |
ses.sendEmail({ | |
Source: 'contact@app.com', | |
Destination: { | |
ToAddresses: [email] | |
}, | |
Message: { | |
Subject: { | |
Data: subject | |
}, | |
Body: { | |
Html : { | |
Data: emailContent, | |
Charset: "UTF-8" | |
} | |
} | |
} | |
}, function(err) { | |
if (err) { | |
throw new Error("Error sending email ", err); | |
return reject(err); | |
} | |
resolve(); | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment