From 68b902e3acaa595789e947aeba60903f4ec1a018 Mon Sep 17 00:00:00 2001 From: ukiyo-roa Date: Mon, 22 Jun 2020 20:24:38 +1000 Subject: [PATCH] Added perms for ses --- serverless.yml | 10 ++++++++-- src/lambdas/endpoints/send-mail.ts | 11 +++++------ src/lambdas/output.ts | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/serverless.yml b/serverless.yml index 7b92b6b..58172d4 100644 --- a/serverless.yml +++ b/serverless.yml @@ -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 @@ -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 diff --git a/src/lambdas/endpoints/send-mail.ts b/src/lambdas/endpoints/send-mail.ts index 683773a..98c51f0 100644 --- a/src/lambdas/endpoints/send-mail.ts +++ b/src/lambdas/endpoints/send-mail.ts @@ -8,7 +8,7 @@ import { createMailOutput } from '../output'; interface SendConfig { companyName: string; - responseBot: string; + agent: string; receiver: string; } @@ -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' }) @@ -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}` }) @@ -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 }); } } \ No newline at end of file diff --git a/src/lambdas/output.ts b/src/lambdas/output.ts index 075f68b..d485604 100644 --- a/src/lambdas/output.ts +++ b/src/lambdas/output.ts @@ -1,5 +1,5 @@ const DEFAULT_OUTPUT = (name: string, email: string, message: string) => - `

You have a new contact request

Contact Details

Message

${message}

`; + `

You have a new contact request

Contact Details

Message

${message}

`; type MailOutputType = 'default';