473,386 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

parsing downloaded mail via POP3

I have the following script:

emails = []
for msg in messagesInfo:
msgNum = int(msg.split()[0])
msgSize = int(msg.split()[1])
if(msgSize < 20000):
message = server.retr(msgNum)[1]
Message = join(message, “\n”)
emails.append(message)
It downloads messages for me via my POP3 server, however, the message
format (attached below) includes ridiculous amounts of data and I just
want to return the from, subject, and body. Any pointers on how to do this?

/// sample message downloaded

fe5.bluebottle.com (fe5 [209.144.225.81])', '\t by
bluebottle-be1.bluebottle.com (Cyrus v2.2.8) with LMTPA;', '\t Tue, 21
Mar 2006 23:47:22 -0600', 'X-Sieve: CMU Sieve 2.2', 'Received: from
fe7.bluebottle.com (fe7 [209.144.225.70])', '\tby fe5.bluebottle.com
(8.13.4/8.13.4) with ESMTP id k2M5hhkd023264', '\tfor
<op**********@bluebottle.com>; Tue, 21 Mar 2006 23:44:35 -0600',
'Received: from smtp-relay.wharton.upenn.edu
(smtp-relay.wharton.upenn.edu [130.91.161.218])', '\tby
fe7.bluebottle.com (8.13.4/8.13.4) with ESMTP id k2M5hea4022775',
'\t(version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO)',
'\tfor <op**********@bluebottle.com>; Tue, 21 Mar 2006 23:43:41 -0600',
'Received: from FAIRMOUNT.wharton.upenn.edu
(fairmount2.wharton.Upenn.Edu [128.91.87.58])', '\tby
smtp-relay.wharton.upenn.edu (8.13.1/8.13.1) with ESMTP id
k2M5heQv007094', '\tfor <op**********@bluebottle.com>; Wed, 22 Mar 2006
00:43:40 -0500', 'X-DomainKeys: Sendmail DomainKeys Filter v0.3.2
smtp-relay.wharton.upenn.edu k2M5heQv007094', 'DomainKey-Signature:
a=rsa-sha1; s=smtp-relay; d=wharton.upenn.edu; c=nofws; q=dns;',
'\tb=TZ7xn8PLJNMsq8iCl7eqlME0EDnCC7fKUvpKmALqe1FQ5 gG/fG+V/bomQMKyblplJ',
'\tlg6wTqPoeao6lkM4yu+Rw==', 'Received: from webmail1.wharton.upenn.edu
([172.16.32.58]) by FAIRMOUNT.wharton.upenn.edu with Microsoft
SMTPSVC(6.0.3790.1830);', '\t Wed, 22 Mar 2006 00:43:39 -0500',
'Received: from [165.123.150.168] ([165.123.150.168]) by
webmail1.wharton.upenn.edu over TLS secured channel with Microsoft
SMTPSVC(6.0.3790.1830);', '\t Wed, 22 Mar 2006 00:43:39 -0500',
'User-Agent: Microsoft-Entourage/11.0.0.040405', 'Date: Wed, 22 Mar 2006
00:43:37 -0500', 'Subject: KNOCKITY-----KNOCK-----WHOS-----THERE',
'From: Kevin Feng <fe***@wharton.upenn.edu>', 'To:
"op**********@bluebottle.com" <op**********@bluebottle.com>',
'Message-ID: <C0464E39.4E34%fe***@wharton.upenn.edu>', 'Mime-version:
1.0', 'Content-type: text/plain;', '\tcharset="US-ASCII"',
'Content-transfer-encoding: 7bit', 'X-OriginalArrivalTime: 22 Mar 2006
05:43:39.0441 (UTC) FILETIME=[921A4210:01C64D73]', 'X-Virus-Scanned:
ClamAV version 0.88, clamav-milter version 0.87 on fe7.bluebottle.com',
'X-Virus-Status: Clean', 'Trusted-Delivery-Validation-State: Not
validated', '', 'ANITA-----ANITA WHO----ANITA BETTER JOKE', '', ''], 2266)
Mar 22 '06 #1
1 2233
Kevin F wrote:
I have the following script:

emails = []
for msg in messagesInfo:
msgNum = int(msg.split()[0])
msgSize = int(msg.split()[1])
if(msgSize < 20000):
message = server.retr(msgNum)[1]
Message = join(message, "\n")
emails.append(message)
It downloads messages for me via my POP3 server, however, the message
format (attached below) includes ridiculous amounts of data and I just
want to return the from, subject, and body. Any pointers on how to do this?


Have you tried server.top ?

MAX_SUMMARY_LINES = 20

def get_headers(self):
server = poplib.POP3(self.server_name)
server.user(self.user_name)
server.pass_(self.password)
hdrs = []
try:
msgCount, msgBytes = server.stat()
for i in range(msgCount):
msgNum = i+1
hdr, message, octets = server.top(msgNum,
MAX_SUMMARY_LINES)
hdrs.append(message)
finally:
server.quit()

alternatively, something like:
import poplib, email
from email.Utils import getaddresses, parseaddr

def download_mail(self):
server = poplib.POP3(self.server_name)
server.user(self.user_name)
server.pass_(self.password)
try:
msgCount, msgBytes = server.stat()
self.messages = []
for i in range(msgCount):
msgNum = i+1
hdr, message, octets = server.retr(msgNum)
mail_msg = '\n'.join( message)
self.messages.append(
email.message_from_string(mail_msg) )
finally:
server.quit()

def print_headers(self):
for message in self.messages:
print '#' * 80
print parseaddr( message['from'] )
print message['subject']
print message['date']
print getaddresses( message.get_all('to', []) )
print getaddresses( message.get_all('cc', []) )

Gerard

Mar 22 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dont bother | last post by:
Hi, I have written this small piece of code. I am a brand new player of Python. I had asked some people for help, unfortunately not many helped. Here is the code I have: import email import...
5
by: Ken Nistler | last post by:
Hello, I was wondering if anyone can help me out. I am trying to send mail from a c# app thru my ISP pop3 mail server. I figured it would be similar to using SMTP, but use my username and...
4
by: Matt Porter | last post by:
I'm writing a c# web app and I can't find a POP3 mail component. I found the SMTP component, but not a POP3 one. Seems like there should be one. What is the name of the POP3 component? or do I...
6
by: erdem kemer | last post by:
i cant send mail to yahoo mail or hotmail while i can send my other mail accounts (pop3) is it becouse yahoo and hotmail is web-based mail here is the code MailMessage mailMsg = new...
2
by: Ken Yu | last post by:
Hi, I want to make a program for receive E-mail by POP3, and forward to another E-mail Account, if the E-mail with Attachment , will delete the attachment before forward, where can i find more...
1
by: bobano | last post by:
Hi everyone, I am writing a POP3 Client program in Perl. You connect to a POP3 Server and have a running conversation with the mail server using commands from the RFC 1939 Post Office Protocol....
2
by: Abhi | last post by:
i need to create a user in web mail using dotnet coad by using webmailserver api ex: xxx is my webmail then i want to create to a user in that webmail xxx ex: yyy@xxx.com how i should do in...
4
by: =?Utf-8?B?QWxwYW5h?= | last post by:
I am making a thin email client and want to get emails from a pop3 server...Is there any built in support in C# to get emails from a pop3 server and parse the email to show up on the UI ?
0
by: Ahmed, Shakir | last post by:
Thanks everyone who tried to help me to parse incoming email from an exchange server: Now, I am getting following error; I am not sure where I am doing wrong. I appreciate any help how to resolve...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.