Skip to content

Commit

Permalink
handle no-html edge case gracefully, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
cconcannon committed Jul 21, 2021
1 parent 41cc6af commit 61e38d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The first command will build the source of your application. The second command

## Configure Receiving Rule in SES

**As mentioned above, you must already have an SES-verified domain in `us-east-1` before you can set up a Receiving Rule for that domain. See [AWS's SES documentation for details on verifying a domain](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/receiving-email-getting-started-verify.html).
**As mentioned above, you must already have an SES-verified domain in `us-east-1` before you can set up a Receiving Rule for that domain. See [AWS's SES documentation for details on verifying a domain](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/receiving-email-getting-started-verify.html).**

After the Cloudformation application stack is successfully deployed, do the following:
1. Log into your AWS console and open your SES configuration home page
Expand Down
34 changes: 24 additions & 10 deletions src/handlers/ses-email-forwarding.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,14 @@ exports.handler = async (event, context) => {

const file = await s3.getObject({Bucket: bucket, Key: object}).promise();
const parsed = await simpleParser(file.Body);
const bodyObject = makeBodyObject(parsed);

const forwardingParams = {
Destination: {
ToAddresses: [process.env.FORWARD_ADDRESS]
},
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data: parsed.html
},
Text: {
Charset: "UTF-8",
Data: parsed.text
}
},
Body: bodyObject,
Subject: {
Charset: "UTF-8",
Data: `to: ${parsed.to.text} sub: ${parsed.subject}`
Expand All @@ -47,6 +39,28 @@ exports.handler = async (event, context) => {
return;
};

function makeBodyObject(parsedEmail) {
let body = {};

if (parsedEmail.html) {
bodyObject = {
Html: {
Charset: "UTF-8",
Data: `${parsedEmail.html}`
}
};
} else {
bodyObject = {
Text: {
Charset: "UTF-8",
Data: `${parsedEmail.text}`
}
};
}

return bodyObject;
}

async function sendEmail(params) {
return new Promise ((resolve, reject) => {
ses.sendEmail(params, function(err, data) {
Expand Down

0 comments on commit 61e38d5

Please sign in to comment.