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!
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.)
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.
Related
I need to save Outlook-Mails with the attachments in the msg-file in Python. Currently working with win32com.client I use: message.SaveAs(path + name) which gives me a nice .msg file, but that does not include attachments (if attachments existent). Attached files are visible using message.Attachments.Count and message.Attachments, but how can I create a .msg-file with the attachments included to store as one file which works when messages are exported straight from Outlook?
how can I create a .msg-file with the attachments included to store as one file which works when messages are exported straight from Outlook?
The Outlook object model doesn't provide anything for that. Potentially, the best that you could do, is save the attached files along with your mail items (msg). Use the Attachment.SaveAsFile method which saves the attachment to the specified path.
I need to send an encoded and decoded image along with some metadata via HTTP.
I would like to send the images as binary data instead of encoding them as base64 as encoding & decoding adds unnecessary latency.
So for example, the encoded image may look like this:
img = open(img_file, 'rb').read()
and the decoded image may look like this:
img = cv2.imread(img_file)
Assume I also need to send some additional information in POST request, such as the image name for example.
What is the most efficient way to send these? What would the code look like in Python? What content-type or other headers would I need to use?
I've found some examples like this online, but they only send a single image and therefore set the content-type as image/jpeg, but I'm wondering what happens when you have additional fields to send.
If you want to send additional fields you have a few options:
Base64 encode the image data and embed it in a json string with all your extra data
Add custom HTTP headers with your fields in
Add your fields to the image metadata itself
I know you said you didn't want to do 1, but how do you know it is adding unnecessary latency if you've never tried it? I expect it's far less than the latency of an HTTP request. Option 2 is risky as the headers can get stripped or changed by network infrastructure and your users might not expect to find data in the headers. Option 3 depends a bit what the data is and whether it makes sense for it to be inside the image (and again whether your users know to look for it there)
In my chat application, only the text is can be sent right now. I'm trying to add a feature in which the images can also be sent. However, there is one point I'm stuck in. When receiving the data, how can I discriminate between photo and text? I'm asking this because these two are completely different procedures. In one of them, we encode it with UTF-8 and send, while in the other we send bytes. On the server side, how can I discriminate them?
I was able to add a send-photo feature on the client side as shown below. When I try it, it succesfully sends image bytes. The only thing I need to is to discriminate the text from bytes on the server side.
As my code is too long, I prefer not to add all of it here. You can access it through my github https://github.com/suleymanyaman/randomchatserver
Client
def sendphoto():
dlg = QFileDialog()
dlg.setFileMode(QFileDialog.AnyFile)
img_dir = QStringListModel()
if dlg.exec_():
img_dir = dlg.selectedFiles()[0]
data = open(r'{}'.format(img_dir),'rb').read()
s.send(data)
Server
while 1:
msg = client.recv(100000000).decode("utf-8")
Once it's on the network, everything is bytes. To add support for images, you just need to send some message that says "An image is coming next." Your protocol hopefully already has some "control messages" that you can use for this.
If you want to keep the protocol "readable" (i.e. you prefer all bytes to be sensible UTF-8), you could use base 64 encoding or similar to turn your images into "text" before sending them. But that probably isn't necessary.
I am trying to do some text mining on emails which I have exported from my email client (Mail in OS X) just by copying and pasting to a rtf file.
When I attempt to run tf-idf on the files either in python or rapidminer I get features which are clearly not in the message content itself. I wonder where they come from or how I can get rid of them. Perhaps from the headers? For example features such as: fonttbl, colortbl,cocoa rtf,paperw etc. Clearly they are some properties of the email. Where do they come from and how can I remove that more the files or extract only the email contents from the original email messages?
Perhaps this is an encoding issue??
Thanks!
As part of some email batch processing, we need to decode and clean up the messages. One critical part of that process is separating the mail bodies of a message and the mail attachments. The trickiest part is to determine when a Conent-Disposition: inline part is to be considered a message body alternative or a file.
So far, this code seems to handle most of the cases:
from email import message_from_string
def split_parts(raw):
msg = message_from_string(raw)
bodies = []
files = []
for sub in msg.walk():
if sub.is_multipart():
continue
cd = sub.get("Content-Disposition", "")
if cd.startswith("attachment") or (cd.startswith("inline") and
sub.get_filename()):
files.append(sub)
else:
bodies.append(sub)
return bodies, files
Note the reliance on the inline parts to have a filename specified in the headers, which Outlook seems to do for all its multipart/related messages. The Content-ID could also be used as a hint, but according to the RFC 2387 it is not such an indicator.
Therefore, if an embedded image is encoded as a message part that has Content-Disposition: inline, defines a Content-ID and doesn't have a filename then the above code can mistakenly classify it as a message body alternative.
From what I've read from the RFC's, there's not much hope on finding an easy check (specially since coding according to the RFCs is almost useless in the real world, because nobody does it); but I was wondering how big the chances are to hit the misclassification case.
Rationale
I could have a set of functions to treat each multipart/* case and let them indirectly recurse. However, we don't care so much about a faithful display; as a matter of fact, we filter all HTML messages through tidy. Instead, we are more interested in chosing one of the message body alternatives and saving as many attachments as possible, even if they are intended to be embedded.
Furthermore, some user agents do really weird things when composing multipart/alternative messages with embedded attachments that are not intended to be displayed inline (such as PDF files), as a result of the user dragging and dropping an arbitrary file into the composition window.
I'm not quite following you, but, if you want bodies, I would assume anything with a text/plain or text/html content type, with an inline content disposition, with no file name or no content-id, could be a body part.