Here is an example Python script that sends an email using AWS Simple Email Service (SES) from an AWS Lambda function:
import boto3
def send_email(to_address, subject, body):
# Create an SES client
client = boto3.client('ses')
# Send the email
response = client.send_email(
Source='[email protected]',
Destination={
'ToAddresses': [
to_address,
]
},
Message={
'Subject': {
'Data': subject
},
'Body': {
'Text': {
'Data': body
}
}
}
)
return response
To use this script, you will need to have the AWS SDK for Python (Boto3) installed in your Lambda function’s environment. You will also need to have the necessary permissions to send emails using SES.
To send an email, you can call the send_email function and pass in the recipient’s email address, the subject line, and the email body. The function will return the response from the SES send_email API call, which includes information about the status of the email.
A slightly improved version of the script is given below where you can change things via parameters.
import boto3
import json
def lambda_landler( event, context ):
#grab the to, from , subject and body
to_address = event['to_address']
subject = event['subject']
body = event['body']
from_address = event['from_address']
#load the ses client
client = boto3.client('ses')
response = client.send_email(
Source= from_address,
Destination={
'ToAddresses':[
to_address
]
},
Message={
'Subject': {
'Data': subject
},
'Body':{
'Text':{
'Data': body
}
}
}
)
return {'status': 200, 'body': json.dumps( response ) }
Note that this is just a basic example, and there are many additional options and configurations available when using SES to send emails. For more information, you can refer to the AWS documentation for SES (https://aws.amazon.com/ses/).