Send Email for the SES without credentials

Enes Cetinkaya
3 min readJun 23, 2023

--

Hello,
Today I will show you how to use a fantastic service from AWS, the SES (Simple Email Service). The best part? You don’t need passwords to perform this task. Let’s get started!

For the first step, you need to install python3, pip and boto3 on the EC2. You then create a permission on EC2. This permission is a key that AWS provides to grant us a special level of access.

{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "ses:SendEmail" ], "Resource": "*" } ] }

Once you obtain this superpower, you throw it onto EC2 and voila! Now your AWS services will work more securely and correctly.

For the second step, you create “Configuration Sets”. These sets are like rulebooks that dictate how your emails will be processed and tracked. After you create this rulebook, you’ll use it in our code to send emails.

And finally, you customize parts of your SES code to our liking. By changing the “SENDER” section, you declare who is sending your emails. You add the rulebook you created in the previous step to the “CONFIGURATION_SET” section. And in the “AWS_REGION” section, you specify which geographic region our AWS services will be provided from.

import boto3 from botocore.exceptions import ClientError # Replace sender@example.com with your "From" address. # This address must be verified with Amazon SES. SENDER = "Sender Name <info@enescetinkaya.net>" # Replace recipient@example.com with a "To" address. If your account # is still in the sandbox, this address must be verified. RECIPIENT = "enes@enescetinkaya.net" # Specify a configuration set. If you do not want to use a configuration # set, comment the following variable, and the # ConfigurationSetName=CONFIGURATION_SET argument below. CONFIGURATION_SET = "MyFirstConfugrationSet" # If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES. AWS_REGION = "us-east-1" # The subject line for the email. SUBJECT = "Amazon SES Test (SDK for Python)" # The email body for recipients with non-HTML email clients. BODY_TEXT = ("Amazon SES Test (Python)\r\n" "This email was sent with Amazon SES using the " "AWS SDK for Python (Boto)." ) # The HTML body of the email. BODY_HTML = """<html> <head></head> <body> <h1>Amazon SES Test (SDK for Python)</h1> <p>This email was sent with <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the <a href='https://aws.amazon.com/sdk-for-python/'> AWS SDK for Python (Boto)</a>.</p> </body> </html> """ # The character encoding for the email. CHARSET = "UTF-8" # Create a new SES resource and specify a region. client = boto3.client('ses',region_name=AWS_REGION) # Try to send the email. try: #Provide the contents of the email. response = client.send_email( Destination={ 'ToAddresses': [ RECIPIENT, ], }, Message={ 'Body': { 'Html': { 'Charset': CHARSET, 'Data': BODY_HTML, }, 'Text': { 'Charset': CHARSET, 'Data': BODY_TEXT, }, }, 'Subject': { 'Charset': CHARSET, 'Data': SUBJECT, }, }, Source=SENDER, # If you are not using a configuration set, comment or delete the # following line ConfigurationSetName=CONFIGURATION_SET, ) # Display an error if something goes wrong. except ClientError as e: print(e.response['Error']['Message']) else: print("Email sent! Message ID:"), print(response['MessageId'])

And that’s it! Now you can send emails smoothly using the AWS SES service. AWS SES is an easy and flexible service to use. I hope this guide helps you and assists in your coding adventures!

Happy coding!

Originally published at https://dev.to on June 23, 2023.

--

--

Enes Cetinkaya
Enes Cetinkaya

Written by Enes Cetinkaya

Platform Engineer | AWS Community Builder | Core Member DevOpsTurkey | AWS Certificated