Skip to content

Commit

Permalink
Added perms for ses
Browse files Browse the repository at this point in the history
  • Loading branch information
jnpco98 committed Jun 22, 2020
1 parent e8fb0f2 commit 68b902e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
10 changes: 8 additions & 2 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ provider:
STACK: ${self:service}
REGION: ${self:custom.region}
profile: serverless-admin
iamRoleStatements:
- Effect: "Allow"
Action:
- "ses:SendEmail"
- "ses:SendRawEmail"
Resource: "*"

custom:
region: us-east-1
Expand All @@ -20,10 +26,10 @@ plugins:
- serverless-offline

functions:
ascii-text:
send-mail:
handler: src/lambdas/endpoints/send-mail.handler
memorySize: 256
timeout: 30
timeout: 10
events:
- http:
path: send-mail
Expand Down
11 changes: 5 additions & 6 deletions src/lambdas/endpoints/send-mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createMailOutput } from '../output';

interface SendConfig {
companyName: string;
responseBot: string;
agent: string;
receiver: string;
}

Expand All @@ -27,7 +27,6 @@ interface RequestBody {

const DEFAULT_SUCCESS_MSG = 'Message has been sent.';
const DEFAULT_ERROR_PARAM_MSG = 'Invalid parameters.';
const DEFAULT_ERROR_MSG = 'Something went wrong with your email. Please try again later.';

const transporter = nodemailer.createTransport({
SES: new AWS.SES({ apiVersion: '2010-12-01' })
Expand All @@ -39,13 +38,13 @@ export async function handler(event: APIGatewayEvent, context: Context): Promise

if(!sendConfig || !messageConfig) return createResponse(422, { error: DEFAULT_ERROR_PARAM_MSG });

const { companyName = 'N/A', receiver, responseBot = 'DEFAULT_BOT' } = sendConfig;
const { companyName = 'N/A', receiver, agent } = sendConfig;
const { email = 'N/A', firstname = 'N/A', lastname = 'N/A', message = 'Empty message', subject = `[Contact Request] ${email}` } = messageConfig;

if(!receiver) return createResponse(422, { error: DEFAULT_ERROR_PARAM_MSG });
if(!receiver || !agent) return createResponse(422, { error: DEFAULT_ERROR_PARAM_MSG });

const mailOptions: Mail.Options = {
from: `${companyName} ${responseBot}`,
from: `${companyName} ${agent}`,
to: receiver,
subject,
html: createMailOutput({ email, message, name: `${firstname} ${lastname}` })
Expand All @@ -56,6 +55,6 @@ export async function handler(event: APIGatewayEvent, context: Context): Promise
return createResponse(200, { message: DEFAULT_SUCCESS_MSG, data });
} catch (e) {
console.error(e);
return createResponse(422, { error: DEFAULT_ERROR_MSG });
return createResponse(422, { error: e });
}
}
2 changes: 1 addition & 1 deletion src/lambdas/output.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const DEFAULT_OUTPUT = (name: string, email: string, message: string) =>
`<p>You have a new contact request</p><h3>Contact Details</h3> <ul> <li>Name: ${name}</li><li>Email: ${email}</li></ul> <h3>Message</h3> <p>${message}</p>`;
`<p>You have a new contact request</p><h3>Contact Details</h3> <ul> <li>Name: ${name}</li><li>Email: ${email}</li></ul> <h3>Message</h3> <p style="white-space: pre-wrap">${message}</p>`;

type MailOutputType = 'default';

Expand Down

0 comments on commit 68b902e

Please sign in to comment.