Use ChatGPT to automatically reply to emails using AWS lambda functions and AWS Simple Email Service

Shruti Tirpude
3 min readJan 22, 2023

--

How amazing it would be if we can leverage chatGPT to respond to emails, imagine a scenario where a user gets an email, AWS lambda function reads it and using ChatGPT, a reply is automatically sent without human intervention. This functionality can be used for complete customer support where emails are processed by lambda functions to automatically create a JIRA/ADO tickets based on the email request, take actions and send an email reply back using chatGPT with the JIRA/ADO tickets.

In this article, we will go through the steps to integrate chatGPT to send the response to emails using SES(Simple Email Service) , lambda functions and ChatGPT API.

The following is a diagram of the whole flow where once an email is received by Amazon Workmail, a lambda function is triggered that processes the email text and using chatGPT API, a reply is generated to the email and ultimately using SES(Simple Email Service) ,a reply is sent to the email.

In the previous article, we are integrating AWS Workmail and lambda functions.

Step 1 : Setup AWS Workmail and lambda function through SAM cli

Follow steps in this article : https://medium.com/@stirpude/configure-aws-workmail-and-lambda-function-to-read-email-content-in-nodejs-b4ef1763e119

Once an email is received in AWS Workmail, the lambda function is triggered and it processes the email body.

Step 2: Create a ChatGPT api key through this console https://beta.openai.com/account/api-keys

Step 3 : Configure the chat gpt api key in the code and replace YOUR_CHATGPT_API_KEY with your actual chatGPT key and get response from chatGPT.

exports.generateEmailResponseFromChatGPT = (email) => {

//prompt for chatgpt
const prompt = `Respond to this email: ${email.emailBody}`;

const configuration = new Configuration({
apiKey: "YOUR_CHATGPT_API_KEY" });
const openai = new OpenAIApi(configuration);

return new Promise((resolve, reject) => {
openai.createCompletion({
prompt,
model: "text-davinci-002",
temperature: 0.5,
}).then((response) => {
resolve(response.data.choices[0].text);
}).catch((err) => {
reject(err);
});
});
};

Call the above generateResponse function in app.js file passing the parameter email body.

response = await emailResponse.generateEmailResponseFromChatGPT(rawMessageContentBuffer.messageContent);

Step 4 : Once email response is received from ChatGPT, use AWS SES(Simple Email Service ) to send an email with the response from ChatGPT to the email address.

In the following code, add your receiver email addresses in the field ToAddresses and the sender email address in Source field.

exports.sendEmail = (emailBody) => {
return new Promise((resolve, reject) => {
const ses = new AWS.SES({ apiVersion: "2010-12-01" });

const params = {
Destination: {
ToAddresses: [
'your list of receiver email addresses'
]
},
Message: {
Body: {
Text: {
Charset: 'UTF-8',
Data: JSON.stringify(emailBody)
}
},
Subject: {
Charset: 'UTF-8',
Data: 'SUBJECT_LINE'
}
},
Source: 'sender email address',
};

ses.sendEmail(params,(err,data) => {
if(err){
reject(err);
}
else {
resolve(data);
}
});

});
};

Calling the above sendEmail function in the app.js

const emailResponeWithoutTrail = response.replace(/\n/g,'');
const emailBodyResponse = await helper.sendEmail(emailResponeWithoutTrail);

And Voila ! The email response is sent to the email address configured :)

--

--

Shruti Tirpude

Software Engineer with an enthusiasm for AWS technologies like serverless.I enjoy solving real world complex problems through technology.