Last active
January 25, 2023 21:19
-
-
Save tazarov/6d841fd450465ab8cfafbe9064c67089 to your computer and use it in GitHub Desktop.
Send email using AWS SES
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import smtplib | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
def test_send_mail(): | |
# Replace [email protected] with your "From" address. | |
# This address must be verified with Amazon SES. | |
SENDER = "admin@org" | |
# Replace [email protected] with a "To" address. If your account | |
# is still in the sandbox, this address must be verified. | |
RECIPIENT = "recepients@org" | |
# Replace smtp_username with your Amazon SES SMTP user name. | |
USERNAME_SMTP = "<CREDs>" | |
# Replace smtp_password with your Amazon SES SMTP password. | |
PASSWORD_SMTP = "<PWD>" | |
# If you're using Amazon SES in an AWS Region other than US West (Oregon), | |
# replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP | |
# endpoint in the appropriate region. | |
HOST = "email-smtp.eu-west-1.amazonaws.com" | |
PORT = 587 | |
# The subject line of the email. | |
SUBJECT = "Amazon SES Test (Python smtplib)" | |
# The email body for recipients with non-HTML email clients. | |
BODY_TEXT = ("Amazon SES Test (Python smtplib)\r\n" | |
"This email was sent with Amazon SES using the " | |
"Python smtplib library.") | |
# The HTML body of the email. | |
BODY_HTML = """<html> | |
<head></head> | |
<body> | |
<h1>Amazon SES Test (Python smtplib)</h1> | |
<p>This email was sent with | |
<a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the | |
<a href='https://docs.python.org/3/library/smtplib.html'> | |
Python smtplib library</a>.</p> | |
</body> | |
</html> | |
""" | |
# Create message container - the correct MIME type is multipart/alternative. | |
msg = MIMEMultipart('alternative') | |
msg['Subject'] = SUBJECT | |
msg['From'] = SENDER | |
msg['To'] = RECIPIENT | |
# Record the MIME types of both parts - text/plain and text/html. | |
part1 = MIMEText(BODY_TEXT, 'plain') | |
part2 = MIMEText(BODY_HTML, 'html') | |
# Attach parts into message container. | |
# According to RFC 2046, the last part of a multipart message, in this case | |
# the HTML message, is best and preferred. | |
msg.attach(part1) | |
msg.attach(part2) | |
# Try to send the message. | |
try: | |
server = smtplib.SMTP(HOST, PORT) | |
server.ehlo() | |
server.starttls() | |
# stmplib docs recommend calling ehlo() before & after starttls() | |
server.ehlo() | |
server.login(USERNAME_SMTP, PASSWORD_SMTP) | |
server.sendmail(SENDER, RECIPIENT, msg.as_string()) | |
server.close() | |
# Display an error message if something goes wrong. | |
except Exception as e: | |
print("Error: ", e) | |
else: | |
print("Email sent!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment