Skip to content

Instantly share code, notes, and snippets.

@mkhorasani
Last active May 22, 2021 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkhorasani/f0b779b5591a67702971dab33f5b0a84 to your computer and use it in GitHub Desktop.
Save mkhorasani/f0b779b5591a67702971dab33f5b0a84 to your computer and use it in GitHub Desktop.
#Combining images
images = [Image.open(x) for x in ['line_chart.png','bar_chart.png','histogram.png']]
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
combined_image = Image.new('RGBA', (total_width, max_height))
offset = 0
for im in images:
combined_image.paste(im, (offset,0))
offset += im.size[0]
combined_image = combined_image.resize((int(0.5*total_width), int(0.5*max_height)))
combined_image.save('combined.png')
#Creating email
message = MIMEMultipart()
message['Subject'] = ('Exam Results for %s' % (df.loc[i]['Name'] + ' ' + df.loc[i]['Surname']))
message.attach(MIMEText('Dear %s please find your exam results below:' % (df.loc[i]['Name'])))
#Embedding image
message_body = MIMEText('<br><img src="cid:%s"><br>' % ('combined.png'), 'html')
message.attach(message_body)
file = open('combined.png', 'rb')
image = MIMEImage(file.read())
file.close()
image.add_header('Content-ID', '<{}>'.format('combined.png'))
message.attach(image)
#Sending email
smtp = smtplib.SMTP('smtp.gmail.com:587')
smtp.ehlo()
smtp.starttls()
smtp.login(email_user, email_pass)
smtp.sendmail(email_user,df.loc[i]['Email'],message.as_string())
smtp.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment