Create Send Email Function

This is the final function, with missions:

  • Received SQS message and sending response to user
  • Save that response to S3.

Why i need this function?

  • Because i want to save sending email response for debugging our email agent based on response email.
  • Easier for debug

Let’s create the lambda function

You can review step 8.1, 8.2 to upload source code

You can download it index.zip or you can build them with my github repository here. After got that file, you need to upload it to lambda Code > Upload from > .zip file

Next, we need to config environment variable for our function to interact with other service

lambda14

We have 2 variables

KeyValueDescription
SOURCE_EMAILsupport@YOUR_DOMAINThis is the support email you want to reply to your user
BUCKET_NAMEai-powered-email-auto-repliesThis bucket for putting email response

Click Save

lambda15

Update environment variables successfully

lambda16

Details about our function

ConfigurationValueDescription
function_nameSendEmailLambdaThe unique name identifier for the Lambda function
roleSendEmailFunctionRoleIAM role that defines the permissions and access policies for the function
runtimenodejs20.xThe execution environment and version for the Lambda function (Node.js 20)
handlerindex.handlerThe entry point for the function
memory_size128Amount of memory allocated to the function in MB (128MB)
timeout30Maximum execution time allowed for the function in seconds
  1. Send email via SES
const data: MessageData = JSON.parse(sqsData.body);
const params: SendEmailCommandInput = {
    Destination: {
        ToAddresses: [data.sender],
    },
    Message: {
        Body: {
            Text: { Data: data.text },
        },
        Subject: { Data: data.subject },
    },
    Source: env.sourceEmail,
};
const command = new SendEmailCommand(params);
await ses.send(command);
  1. Save response to S3
await s3.send(
    new PutObjectCommand({
        Bucket: env.bucketName,
        Key: `generated-email/${content.key}.json`,
        Body: content.data,
        ContentType: 'application/json'
    })
);

Full source code

import { SESClient, SendEmailCommand, SendEmailCommandInput } from "@aws-sdk/client-ses";
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import { Handler, SQSEvent } from 'aws-lambda';
import { Logger } from '@aws-lambda-powertools/logger';

const logger = new Logger();

require('dotenv').config();

type MessageData = {
    messageId: string;
    sender: string;
    subject: string;
    text: string;
};

type S3Data = {
    key: string;
    data: string;
};

const env = {
    sourceEmail: process.env.SOURCE_EMAIL || "",
    bucketName: process.env.BUCKET_NAME || ""
}

const ses = new SESClient();
const s3 = new S3Client();

export const handler: Handler = async (event: SQSEvent): Promise<void> => {
    try {
        const content = await sendEmailToCustomer(event);
        if (content) {
            await saveResponseToS3(content);
        }
    }
    catch (error) {
        logger.error(error as string);
    }
};

const sendEmailToCustomer = async (event: SQSEvent): Promise<S3Data | null> => {
    // logger.info('Sending email to customer', event.Records.toString());
    const sqsData = event.Records[0];
    // logger.info('Body', sqsData.body);
    if (sqsData) {
        const data: MessageData = JSON.parse(sqsData.body);
        const params: SendEmailCommandInput = {
            Destination: {
                ToAddresses: [data.sender],
            },
            Message: {
                Body: {
                    Text: { Data: data.text },
                },
                Subject: { Data: data.subject },
            },
            Source: env.sourceEmail,
        };
        const command = new SendEmailCommand(params);
        await ses.send(command);
        return { key: data.messageId, data: JSON.stringify(params) };
    }
    return null;
}

const saveResponseToS3 = async (content: S3Data) => {
    // logger.info('Saving response to S3', content);
    await s3.send(
        new PutObjectCommand({
            Bucket: env.bucketName,
            Key: `generated-email/${content.key}.json`,
            Body: content.data,
            ContentType: 'application/json'
        })
    );
}