Skip to content

Instantly share code, notes, and snippets.

@surinkim
Created October 18, 2020 06:16
Show Gist options
  • Save surinkim/5190f14bc8724179550c7e337b21d759 to your computer and use it in GitHub Desktop.
Save surinkim/5190f14bc8724179550c7e337b21d759 to your computer and use it in GitHub Desktop.
import os
import sys
import boto3
from botocore.exceptions import ClientError
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
# Replace sender@example.com with your "From" address.
# This address must be verified with Amazon SES.
SENDER = "Tester1 <test1@test.com>"
# Replace recipient@example.com with a "To" address. If your account
# is still in the sandbox, this address must be verified.
recipients = ['test1@test.com', 'test2@test.com']
# 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 = "ConfigSet"
# If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES.
AWS_REGION = "ap-northeast-2"
# The subject line for the email.
#SUBJECT = "An error occurred in the India studio."
SUBJECT = sys.argv[1]
# The full path to the file that will be attached to the email.
ATTACHMENT = ".\\test.log"
# The email body for recipients with non-HTML email clients.
BODY_TEXT = "An error occurred in the studio. Please check the attached file for details."
# The HTML body of the email.
BODY_HTML = """\
<html>
<head></head>
<body>
<h1></h1>
<p>An error occurred in the studio. Please check the attached file for details.</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)
# Create a multipart/mixed parent container.
msg = MIMEMultipart('mixed')
# Add subject, from and to lines.
msg['Subject'] = SUBJECT
msg['From'] = SENDER
msg['To'] = ', '.join(recipients)
# Create a multipart/alternative child container.
msg_body = MIMEMultipart('alternative')
# Encode the text and HTML content and set the character encoding. This step is
# necessary if you're sending a message with characters outside the ASCII range.
textpart = MIMEText(BODY_TEXT.encode(CHARSET), 'plain', CHARSET)
htmlpart = MIMEText(BODY_HTML.encode(CHARSET), 'html', CHARSET)
# Add the text and HTML parts to the child container.
msg_body.attach(textpart)
msg_body.attach(htmlpart)
# Define the attachment part and encode it using MIMEApplication.
att = MIMEApplication(open(ATTACHMENT, 'rb').read())
# Add a header to tell the email client to treat this part as an attachment,
# and to give the attachment a name.
att.add_header('Content-Disposition','attachment',filename=os.path.basename(ATTACHMENT))
# Attach the multipart/alternative child container to the multipart/mixed
# parent container.
msg.attach(msg_body)
# Add the attachment to the parent container.
msg.attach(att)
#print(msg)
try:
#Provide the contents of the email.
response = client.send_raw_email(
Source=SENDER,
# Destinations=[
# RECIPIENT
# ],
Destinations=recipients,
RawMessage={
'Data':msg.as_string(),
},
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'])
@codifyer
Copy link

Thank you. This really helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment