Emmet Brown
Emmet Brown

Reputation: 456

How can I send in-body images in a mail using python?

how can i send in-body attached images in a mail using python? I found this: http://docs.python.org/library/email-examples.html but it has not an example with images.

Thanks!

Upvotes: 2

Views: 3492

Answers (2)

Gareth Rees
Gareth Rees

Reputation: 65854

Take a look at the big example of "how to send the entire contents of a directory as an email message". The image in the file fp is converted into the message part msg here:

msg = MIMEImage(fp.read(), _subtype=subtype)

and then the message part is attached to the outer message here:

msg.add_header('Content-Disposition', 'attachment', filename=filename)
outer.attach(msg)

If you want the image to appear inline rather than as an attachment, you should set its Content-Disposition to inline instead of attachment.

(If you want to create HTML messages that display attached images, then you need to use the multipart/related MIME type defined in RFC 2387. Ask if you need help with this.)

Upvotes: 2

nmichaels
nmichaels

Reputation: 50981

Attach the image and use html to display it. See the MIME example from your link for how to attach it. An <img> tag will do for display in most clients. Make sure you use the appropriate MIME type for html. Your link tells you how to do that too.

Upvotes: 0

Related Questions