What is Aws SES
AWS SES( Simple Email Service ) is email service that is used by developers to programatically send emails inside their applications . It can be used to send transactional , marketing or mass commnication emails .
What is SMTP
The email protocol SMTP (Simple Mail Transfer Protocol) is used to transfer email messages from one email account to another across the internet.
Email protocols, like POP and IMAP, are sets of rules that allow different email clients and accounts to effortlessly exchange information. SMTP is one of the most prevalent. It’s also the sole protocol dedicated to sending emails. SMTP is used by most email clients to “push” or send messages from a sender to a recipient, including Outlook, Apple Mail, Gmail, and Yahoo Mail.
AWS SES also provides us with a smtp endpoint which we can use to send emails .
Sending Emails using Aws SES and smtplib .
The Python standard library provides a builtin module called smtplib which makes working with the SMTP protocol very much of ease .
To get started we will need the address of aws smtp server, port , user id and password .
The Hostname name and port can be found the smtp section of the SES Dashboard .
Click on Create SMTP credentials to generate password and userId , save the credentials securely . Do not hardcode the credentials in your application code . But store them in .env file like :
SES_USER_ID="your_user_id" SES_PASSWORD="your_password" SES_HOST_ADDRESS="your_ses_hostname"
To properly test your ses integration you need to have verified the sender email address in verified identities If Your account is in sandbox , then the recievers address needs to be verified also
Using smtplib .
As we have got every thing to get started , lets code the program down .
When sending emails with Python, make sure your SMTP connection is encrypted so that your message and login credentials are not easily accessible to others. To encrypt an SMTP connection, SSL (Secure Sockets Layer) and TLS (Transport Layer Security) protocols can be used.
import os import ssl from smtplib import SMTP def send_email_with_ses(sender,reciever,message) : # getting the credentials fron evironemnt host = os.environ.get("SES_HOST_ADDRESS") user = os.environ.get("SES_USER_ID") password = os.environ.get("SES_PASSWORD") # setting up ssl context context = ssl.create_default_context() # creating an unsecure smtp connection with SMPT(host,port) as server : # securing using tls server.starttls(context=context) # authenticating with the server to prove our identity server.login(user=user, password=password) # sending a plain text email server.sendmail(sender, reciever, message) if __name__ == "__main__" : # testing our function . message = "testing email" send_email_with_ses("aadbid@some_mail.com","reciever@some_mail.com",message)
If you have put right credentials and verified identities the code should run as is .
If you want to learn more related to smtp and sending emails using python check out the following articles
- Complete guide to sending emails using python - by real python
- Everything you need to know about SMTP -by postmark
Thanks for reading . Happy coding .