import os import boto3 from botocore.exceptions import ClientError from email.message import EmailMessage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication from email.mime.image import MIMEImage from email import charset # Replace sender@example.com with your "From" address. # This address must be verified with Amazon SES. SENDER = "FanSifter@fansifter.com" # Replace recipient@example.com with a "To" address. If your account # is still in the sandbox, this address must be verified. RECIPIENT = "toomas@fansifter.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 = "eu-west-1" # The subject line for the email. SUBJECT = "test stuff" # The full path to the file that will be attached to the email. ATTACHMENT = "path/to/customers-to-contact.xlsx" # The email body for recipients with non-HTML email clients. BODY_TEXT = "Hello,\r\nPlease see the attached file for a list of customers to contact." # The HTML body of the email. with open('/home/toomas/projects/fansifter-email-templates/Welcome/welcome.html') as hb: BODY_HTML = hb.read() # The character encoding for the email. CHARSET = "utf-8" charset.add_charset('utf-8', charset.SHORTEST, '8bit') # 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') #msg = EmailMessage() # Add subject, from and to lines. msg['Subject'] = SUBJECT msg['From'] = SENDER msg['To'] = 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, 'html', CHARSET) # ch = charset.Charset('utf-8') # ch.body_encoding = '8bit' # htmlpart.set_charset(ch) htmlpart.replace_header('Content-Transfer-Encoding', '8bit') # 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()) img = MIMEImage(open('/home/toomas/projects/fansifter-email-templates/Welcome/g14.png', 'rb').read()) img["Content-ID"] = '' #img_part = EmailMessage() #img_part.add_related(img) # 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)) #msg_body.attach(img_part) # Attach the multipart/alternative child container to the multipart/mixed # parent container. msg.attach(msg_body) msg.attach(img) #msg.set_content(msg_body) #msg.add_related(img) # Add the attachment to the parent container. #msg.attach(img_part) #print(msg) try: #Provide the contents of the email. response = client.send_raw_email( Source=SENDER, ReturnPathArn="arn:aws:ses:eu-west-1:776891437216:identity/hello@fansifter.com", Destinations=[ RECIPIENT ], 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'])