import boto3
import matplotlib.pyplot as plt
from email import encoders
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
def send_email(from_email, to_emails, subject, body_html, attachments=[], cc=[], bcc=[]):
attachment_ready_html = []
img_id = 0
mime_images = []
for l in body_html:
if l.startswith("<img"):
image_data = l[len("<img src='data:image/png;base64,"):-2]
mime_img = MIMEImage(base64.standard_b64decode(image_data))
mime_img.add_header('Content-ID', '<img-%d>' % img_id)
attachment_ready_html.append("<center><img src='cid:img-%d'></center>" % img_id)
img_id += 1
mime_images.append(mime_img)
else:
attachment_ready_html.append(l)
print("Added {} images".format(img_id))
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = ", ".join(to_emails)
body = MIMEText('\n'.join(attachment_ready_html), 'html')
for i in mime_images:
msg.attach(i)
msg.attach(body)
for raw_attachment in attachments:
attachment = MIMEApplication(open(raw_attachment, 'rb').read())
attachment.add_header('Content-Disposition', 'attachment', filename=raw_attachment)
msg.attach(attachment)
ses = boto3.client('ses', region_name='us-west-2')
ses.send_raw_email(
Source=msg['FROM'],
Destinations=to_emails,
RawMessage={'Data': msg.as_string()})
print "Sending Email."
Sending Email Overview
Sending an email from with Databricks is quite easy using Amazon SES. In order to do this, you'll have to make sure that you have access and proper permissioning with the AWS SES service. The permission you need is
ses:SendRawEmail
. You can set that up on the cluster IAM Role or you can set it up with an access key. See here for more information on access control on Amazon SES.Now Let's follow the steps that you'll need to complete in order to actually send an email.
Last refresh: Never