Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ImageMail
# Example for sending an email with an attached image using smtplib
#
# IMPORTANT: You need to enter your email login in the main() function.
# The example is prepared for GMail, but other providers
# should be possible by changing the mail server.
import smtplib
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email import encoders
import Image
from io import BytesIO
def get_attachment(img):
bytes = BytesIO()
img.save(bytes, format='JPEG')
msg = MIMEBase('image', 'jpeg')
msg.set_payload(bytes.getvalue())
encoders.encode_base64(msg)
msg.add_header('Content-Disposition', 'attachment',
filename='image.jpeg')
return msg
def main():
### CHANGE THESE VALUES:
to = 'example@example.com'
subject = 'Image from Pythonista'
gmail_user = 'YOUR_GMAIL_ADDRESS'
gmail_pwd = 'YOUR_PASSWORD'
#Load a sample image, modify as needed:
image = Image.open('Test_Lenna')
print 'Connecting...'
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
print 'Preparing message...'
outer = MIMEMultipart()
outer['Subject'] = subject
outer['To'] = to
outer['From'] = gmail_user
outer.preamble = 'You will not see this in a MIME-aware email reader.\n'
attachment = get_attachment(image)
outer.attach(attachment)
composed = outer.as_string()
print 'Sending...'
smtpserver.sendmail(gmail_user, to, composed)
smtpserver.close()
print 'Done.'
if __name__ == '__main__':
main()
@pawelratajczak

This comment has been minimized.

Copy link

commented Nov 14, 2012

Hello Ole. Great script. My question is how to add more types of image formats such as png, etc?

@omz

This comment has been minimized.

Copy link
Owner Author

commented Nov 15, 2012

@pawelratajczak Basically, you would have to set the format to 'PNG' in line 16, modify the mime type to 'image', 'png' in line 17, and change the filename to something like image.png in line 21.

@omz

This comment has been minimized.

Copy link
Owner Author

commented Nov 15, 2012

Basically, you would just have to replace jpeg with png in lines 16 to 21 (while preserving the capitalization).

@CinnamonRage

This comment has been minimized.

Copy link

commented May 7, 2013

I've tried using this but can't get it working with iCloud email. I've tried switching to SMTP_SSL('smtp.mail.me.com', 587) as Apple says SSL is required with no luck.

@jlubc

This comment has been minimized.

Copy link

commented Jan 5, 2014

after i modify the script, I am enjoying it with a shortcut on my iphone. How do I share it with my collegues who don't have pythonista? Anyway I can export the script so that my collegues can use it without pythonista?

@jamesacampbell

This comment has been minimized.

Copy link

commented Apr 25, 2014

Great work! I am using it to email post to Flickr. Thanks!

@rabdeb

This comment has been minimized.

Copy link

commented Aug 28, 2014

How about emailing a .csv file created by another pythonista script? The files reside in the same directory as the python scripts. How can that be done?

@zencuke

This comment has been minimized.

Copy link

commented Nov 17, 2014

I just started working on doing a csv file right. I'll post when I get it working. Meanwhile I think the key is to change the get_attachment function to use MIMEText instead of MIMEBase to fill the msg variable. MIMEText is used to create a text file attachment.

It needs to read the entire file into a variable and pass it as the argument to MIMEText. Something like:

'''
def get_attachment(file):
   fp = open('file.csv', 'rb')
   # Create a text/plain message
   msg = MIMEText(fp.read())
   fp.close()
   return msg
'''
@zencuke

This comment has been minimized.

Copy link

commented Nov 17, 2014

Sorry. There are several typos in that but I hope they are obvious. I'll post a working example when I get one working.

@zencuke

This comment has been minimized.

Copy link

commented Nov 17, 2014

Close but no cigar. MIMEText sent the text file as the email message instead of as an attachment. That's not what O wanted and probably not what you (rabdeb) wanted. More research clearly needed. ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.