Created
July 10, 2020 07:19
-
-
Save vinodjayachandran/8a0d7f8dd0a8470c2ba5a84be3d8574c to your computer and use it in GitHub Desktop.
[AWS-SES][Python]Send e-mail with attachment using command line arguments
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 os | |
import boto3 | |
import argparse | |
from botocore.exceptions import ClientError | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.mime.application import MIMEApplication | |
parser = argparse.ArgumentParser(description='Send e-mail through aws-ses') | |
parser.add_argument('--recipient',required=True, help='Recipients to whom mail needs to be sent') | |
parser.add_argument('--sender',required=True, help='Sender aka From for e-mail') | |
parser.add_argument('--subject',required=True, help='E-mail subject') | |
parser.add_argument('--attachmentPath', help='File path for e-mail attachment') | |
parser.add_argument('--region',required=True,help='AWS-SES region') | |
parser.add_argument('--bodyHtml',help='E-mail body as HTML') | |
parser.add_argument('--body',help='Email Body as text') | |
cmdArgs = vars(parser.parse_args()); | |
# Replace sender@example.com with your "From" address. | |
# This address must be verified with Amazon SES. | |
SENDER = cmdArgs.get("sender","ops@boomerangcommerce.com"); | |
# Replace recipient@example.com with a "To" address. If your account | |
# is still in the sandbox, this address must be verified. | |
RECIPIENT = cmdArgs.get("recipient", "pi-dev@boomerangcommerce.com").split(",") | |
# If necessary, replace us-east-1 with the AWS Region you're using for Amazon SES. | |
AWS_REGION = cmdArgs.get("region", 'us-east-1') | |
# The subject line for the email. | |
SUBJECT = cmdArgs.get("subject","") | |
# The full path to the file that will be attached to the email. | |
ATTACHMENT = cmdArgs.get("attachmentPath","") | |
# The email body for recipients with non-HTML email clients. | |
BODY_TEXT = cmdArgs.get("body","") | |
# The HTML body of the email. | |
BODY_HTML = cmdArgs.get("bodyHtml","") | |
# 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" | |
# 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(RECIPIENT) | |
# 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) | |
try: | |
#Provide the contents of the email. | |
response = client.send_raw_email( | |
Source=SENDER, | |
Destinations=[ | |
], | |
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']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment