<div dir="ltr"><div>Hi everyone!</div>
<div> </div>
<div>i wrote the next code which suppose to go to my hotmail account, fetch the last email's image attachment and save it to a file:</div>
<div> </div>
<div> </div>
<div>import email, poplib, os, string</div>
<div>def get_messages_id(message_list):<br> items = []<br> for message in message_list[1]:<br> message_parts = string.split(message,' ')<br> message_id = message_parts[0]<br> items.append(message_id)<br>
# print 'message id is: %s\n' % message_id</div>
<div># print 'done getting ids'<br> return items</div>
<div><br>host = "<a href="http://pop3.live.com">pop3.live.com</a>"</div>
<div>detach_dir = 'D:\\test-attachments\\' # directory where to save attachments (default: current)<br>user = "<a href="mailto:matan.py@hotmail.com">matan.py@hotmail.com</a>"<br>pwd = "0okmnji9"</div>
<div>server = poplib.POP3_SSL(host)<br>server.user(user)<br>server.pass_(pwd)<br> <br>numMessages = len(server.list()[1])<br>#print numMessages</div>
<div># server.list() returns messages info from server in<br># form of response, message_list, size where message_list<br># is. a list of messages in form of 'message_id size'<br>message_list = server.list()</div>
<div># get the list of items id<br>items = get_messages_id(message_list)<br>items.reverse() # puts the last received mail's id in the first place in list<br>is_image = False</div>
<div># find the first mail in items that has an attachment and get the attachment<br>for emailid in items: <br> # if we found an email with an image we break<br> if is_image:<br> break<br> <br> total_email_message = server.retr(emailid)[1]<br>
messageText = string.join(total_email_message, "\n")<br> <br> mail = email.message_from_string(messageText) # parsing the mail content to get a mail object <br> <br> # Check if there are any attachments at all <br>
if mail.get_content_maintype() != 'multipart': <br> continue <br> <br> # we use walk to create a generator so we can iterate on the parts <br> for part in mail.walk(): <br> <br>
# skip any attachment which is not an image <br> if part.get_content_maintype() != 'image': <br> continue <br> <br> # is this part an attachment ? <br>
if part.get('Content-Disposition') is None: <br> continue <br> <br> filename = part.get_filename()<br> is_image = True <br> <br> # if there is no filename, we create one with a counter to avoid duplicates <br>
if not filename: <br> filename = 'part-%03d%s' % (1, 'bin') <br> <br> att_path = os.path.join(detach_dir, filename) <br> <br> # Check if its not already there and finally write the file <br>
if not os.path.isfile(att_path) : <br> fp = open(att_path, 'wb') <br> fp.write(part.get_payload(decode=True)) <br> fp.close()<br> <br>
# if we found an email with an image we break after first image downloaded<br> if is_image:<br> break</div>
<div>server.quit()<br>print 'Finished fetching the image' </div>
<div># THE END</div>
<div> </div>
<div>also available at: <a href="http://pastebin.org/53873">http://pastebin.org/53873</a></div>
<div> </div>
<div> </div>
<div>This works fine on Cpython but on IronPython 2.0.3 I get:</div>
<div> </div>
<div>Traceback (most recent call last):<br> File "get_first_attachment.py", line 21, in get_first_attachment.py<br> File "D:\IronPython 2.0.3\Lib\poplib.py", line 361, in __init__<br> File "D:\IronPython 2.0.3\Lib\poplib.py", line 137, in _getresp<br>
File "D:\IronPython 2.0.3\Lib\poplib.py", line 374, in _getline<br> File "D:\IronPython 2.0.3\Lib\poplib.py", line 364, in _fillBuffer<br>MemoryError: Exception of type 'System.OutOfMemoryException' was thrown.</div>
<div> </div>
<div>this comes out from line 21: <font size="2">
<p>server = poplib.POP3_SSL(host) </p>
<p>Any ideas why is that? How to fix it? Is it a known issue (tried to look but couldn't find)?</p>
<p>Thanks,</p>
<p> Matan</p></font></div></div>