473,699 Members | 3,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

way to extract only the message from pop3

Hello All,

Using poplib in python I can extract only the headers using the .top,
there is a way to extract only the message text without the headers?

like remove the fields below:
"
Return-Path:
X-Original-To:
Received: from [
by (Postfix) with ESMTP id B32382613C
for Tue, 3 Apr 2007 09:54:28 -0300 (BRT)
Date: Tue, 03 Apr 2007 09:52:15 -0300
From: <@>
To:
Subject: test
Message-Id:
MIME-Version: 1.0
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit
X-Mailer: Becky! ver. 2.24.02 [en]
X-UIDL: !Dn!!HKT!!/k
Status: RO
"
and only get this:

this is a text message..
...

Thanks

Apr 3 '07 #1
9 9327
On Apr 3, 2:36 pm, "flit" <superf...@gmai l.comwrote:
Hello All,

Using poplib in python I can extract only the headers using the .top,
there is a way to extract only the message text without the headers?

like remove the fields below:
"
Return-Path:
X-Original-To:
Received: from [
by (Postfix) with ESMTP id B32382613C
for Tue, 3 Apr 2007 09:54:28 -0300 (BRT)
Date: Tue, 03 Apr 2007 09:52:15 -0300
From: <@>
To:
Subject: test
Message-Id:
MIME-Version: 1.0
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit
X-Mailer: Becky! ver. 2.24.02 [en]
X-UIDL: !Dn!!HKT!!/k
Status: RO
"
and only get this:

this is a text message..
..

Thanks


I found a tutorial on parsing email that should help you:

http://www.devshed.com/c/a/Python/Py...Email-Parsing/

Also see the email module:

http://www.python.org/doc/2.3.5/lib/module-email.html

Mike

Apr 3 '07 #2
ky******@gmail. com wrote:
I found a tutorial on parsing email that should help you:

http://www.devshed.com/c/a/Python/Py...Email-Parsing/
>
Also see the email module:

http://www.python.org/doc/2.3.5/lib/module-email.html

Mike
Well, I couldn't work with that stuff, especially with that tutorial, some
time ago. It worked better, when I took a look at the code, user "rogen"
wrote in his first posting here:

http://www.python-forum.de/topic-7507.html

It's really not beautiful code, but as soon as you understand, what he does,
you're almost there.

See You

H.
Apr 3 '07 #3
"flit" <su*******@gmai l.comwrote:
>
Using poplib in python I can extract only the headers using the .top,
there is a way to extract only the message text without the headers?
Only by using Python code. The other responses gave you good pointers
toward that path, but I thought I would point out the reason why.

The POP3 protocol is surprisingly primitive. There are only two commands
to fetch a message: RETR, which fetches the headers and the entire message,
and TOP, which fetches the headers and the first N lines of the message.
The key point is that both commands fetch the headers.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Apr 4 '07 #4
Yep you are right..
I made an filter to get the data in the message I want..
So it´s not the most beatiful code, but works. :)

On Apr 4, 4:11 am, Tim Roberts <t...@probo.com wrote:
"flit" <superf...@gmai l.comwrote:
Using poplib in python I can extract only the headers using the .top,
there is a way to extract only the message text without the headers?

Only by using Python code. The other responses gave you good pointers
toward that path, but I thought I would point out the reason why.

The POP3 protocol is surprisingly primitive. There are only two commands
to fetch a message: RETR, which fetches the headers and the entire message,
and TOP, which fetches the headers and the first N lines of the message.
The key point is that both commands fetch the headers.
--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.

Apr 4 '07 #5
flit wrote:
Hello All,

Using poplib in python I can extract only the headers using the .top,
there is a way to extract only the message text without the headers?
As mentioned before, you should use module "email":

------------------------------------

#!/usr/bin/env python

import poplib
import email
import os
import sys
import string
PROVIDER = "pop.YourMailPr ovider.de"
USER = "YourUserNa me"
PASSWORD = "YourPasswo rd"

try:
client = poplib.POP3(PRO VIDER)
except:
print "Error: Provider not found."
sys.exit(1)

client.user(USE R)
client.pass_(PA SSWORD)

nrof_mails = len(client.list ()[1])

for i in range(nrof_mail s):
lines = client.retr(i + 1)[1]
mailstring = string.join(lin es, "\n")
msg = email.message_f rom_string(mail string)

for part in msg.walk():
blockit = 0

if part.get_conten t_maintype() == "text" and blockit == 0:
blockit = 1
mycontent = part.get_payloa d()
mycontent = mycontent.decod e("quopri_codec ")
print mycontent
print

client.quit()

------------------------------------

See You

H.
Apr 5 '07 #6
flit wrote:
Hello All,

Using poplib in python I can extract only the headers using the .top,
there is a way to extract only the message text without the headers?
As mentioned before, you should use module "email":

------------------------------------

#!/usr/bin/env python

import poplib
import email
import os
import sys
import string
PROVIDER = "pop.YourMailPr ovider.de"
USER = "YourUserNa me"
PASSWORD = "YourPasswo rd"

try:
client = poplib.POP3(PRO VIDER)
except:
print "Error: Provider not found."
sys.exit(1)

client.user(USE R)
client.pass_(PA SSWORD)

nrof_mails = len(client.list ()[1])

for i in range(nrof_mail s):
lines = client.retr(i + 1)[1]
mailstring = string.join(lin es, "\n")
blockit = 0

msg = email.message_f rom_string(mail string)

for part in msg.walk():

if part.get_conten t_maintype() == "text" and blockit == 0:
blockit = 1
mycontent = part.get_payloa d()
mycontent = mycontent.decod e("quopri_codec ")
print mycontent
print

client.quit()

------------------------------------

See You

H.
Apr 5 '07 #7
En Thu, 05 Apr 2007 15:09:18 -0300, Collin Stocks <co**********@g mail.com>
escribió:
message=whole_m essage[len(headers):No ne]

You can omit the word None: it is just there for clarity purposes.
Uhm... I can't find any usage of slices including an explicit None in
code.google.com (except on the Python test suite), and really I don't
consider that to be more readable than whole_message[len(headers):]
But of course this is just a stylistic issue.

--
Gabriel Genellina

Apr 5 '07 #8
On 05/04/07, Collin Stocks <co**********@g mail.comwrote:
On 3 Apr 2007 12:36:10 -0700, flit <su*******@gmai l.comwrote:
Hello All,

Using poplib in python I can extract only the headers using the .top,
there is a way to extract only the message text without the headers?
so get two strings: only headers, and the whole message.
find the length of the headers, and chop that off the beginning of the whole
message:
message=whole_m essage[len(headers):No ne]
This way you have to perform 2 downloads, the headers and the whole
message. Then join them both into strings and subtract one from the
other by slicing or other means.

(other means? body = whole_message.r eplace(headers, '' ) or maybe not ! :) )

The body starts at the first blank line after the Subject: header, in
practice this is the first blank line. This is a good starting point
for something simple like my earlier suggestion:

msg = '\r\n'.join( M.retr(i+1)[1] ) # retrieve the email into string
hdrs,body = msg.split('\r\n \r\n',1) # split it into hdrs & body

If the original poster required the body to be seperated from the
headers (and I received a private reply from the OP to my original
post that suggested it probably was) then splitting a joined whole
message at the first blank line is sufficient and only requires 1
download without using the email module

If the OP required just the text parts extracted from the message then
it gets a bit trickier, the email module is the way to go but not
quite how a previous poster used it.

Consider an email that routed through my (python) SMTP servers and
filters today,.

Content: ['text/plain', 'text/html', 'message/delivery-status',
'text/plain', 'text/plain', 'text/plain', 'unknown', 'message/rfc822',
'text/plain', 'text/html']

Is text/html a text part or an html part for this exercise ? :)

You need to walk the parts and use something like

# part.get_conten t_maintype() requires a further call
# to get_content_sub type() , so use
# part.get_conten t_type() instead.

required = ['text/plain', 'text/tab-separated-values']
for part in EMAIL_OBJ.walk( ):
text_parts = []
if part.get_conten t_type() in required:
text_parts.appe nd(part)

print ('\r\n' + '='*76 +'\r\n').join(t ext_parts)
# print all the text parts seperated by a line of '='
# end

Whether you use the email module or not, you need to join the
retrieved message into a string. You can use \n but if you plan to
push the text back out in an email '\r\n' is required for the SMTP
sending part. Your client may or may not convert \n to \r\n at
sending time :)

HTH :)

--

Tim Williams
Apr 5 '07 #9
On 06/04/07, Tim Williams <ti*@tdw.netwro te:
Content: ['text/plain', 'text/html', 'message/delivery-status',
'text/plain', 'text/plain', 'text/plain', 'unknown', 'message/rfc822',
'text/plain', 'text/html']
I should explain that this was the content in a single email
>
# part.get_conten t_maintype() requires a further call
# to get_content_sub type() , so use
# part.get_conten t_type() instead.

required = ['text/plain', 'text/tab-separated-values']
for part in EMAIL_OBJ.walk( ):
text_parts = []
if part.get_conten t_type() in required:
text_parts.appe nd(part)

print ('\r\n' + '='*76 +'\r\n').join(t ext_parts)
# print all the text parts seperated by a line of '='
# end
Content: ['text/plain', 'text/html', 'message/delivery-status',
'text/plain', 'text/plain', 'text/plain', 'unknown', 'message/rfc822',
'text/plain', 'text/html']

Is text/html a text part or an html part for this exercise ? :)

You need to walk the parts and use something like

# part.get_conten t_maintype() requires a further call
# to get_content_sub type() , so use
# part.get_conten t_type() instead.

required = ['text/plain', 'text/tab-separated-values']
for part in EMAIL_OBJ.walk( ):
# text_parts = [] <== oops, this should be above the for.....
if part.get_conten t_type() in required:
text_parts.appe nd(part)

print ('\r\n' + '='*76 +'\r\n').join(t ext_parts)
# print all the text parts seperated by a line of '='
# end
Apr 5 '07 #10

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

Similar topics

3
8245
by: matt roberts | last post by:
Hi... I'm interested in using PHP to extract attachments from a standard unix ..mbox file...anybody done this or have a link to resources that might help? There is plenty on sending mail, but not much on reading/processing mail with php... Thx! matt
1
5955
by: Lev Altshuler | last post by:
Hi, I am trying to count email messages in the mailbox and read their headers. In case that there are some messages on the POP3 server and they haven't yet got to the Inbox, I get a number of messages. As soon as they have been in the Inbox, I get '0E0' as a number of messages. Does anyone know what feature of Net::POP3 I am not aware of, or where did I screw up in the code? use Net::POP3;
0
2604
by: Eric McDaniel | last post by:
This may be a question for ActiveState support, but just in case anyone out there knows what the problem is... My call to Net::POP3->new() works fine when run through the perl interpreter, but fails when I compile the script with PerlApp and run it. My system is Windows 2000, ActiveState Perl 5.8.0 build 806, PerlApp 5.2.0 build 520. Here is the script:
2
2316
by: Rui | last post by:
I need to write a proxy between pop3 client and pop3+ssl server. This proxy will receive pop3 commands from the client and will translate those commands for a pop3+ssl server, then it will receive returns from pop3+ssl server and it will send them to the primary client. How can I write this service in C#?
3
3117
by: Carlos Lozano | last post by:
Hi, I know I can create e-mails and send them using System.Web.Mail, but I was wondering how can I do it using the POP3 settings on Office Outlook or Outlook Express. The problem I have is some users are being denied access to relying directly to the smtp server, but they can send using outlook's POP3 settings. Thanks,
7
7631
by: erikcw | last post by:
Hi all, I'm trying to extract zip file (containing an xml file) from an email so I can process it. But I'm running up against some brick walls. I've been googling and reading all afternoon, and can't seem to figure it out. Here is what I have so far. p = POP3("mail.server.com")
1
457
by: rodny.romero | last post by:
Somebody can help me with an example of like Reading post office with VB.Net using protocol POP3, I have obtained the commandos or instructions that protocol POP3 supports, but I have not been able to arm a small form with some example that connectivity that consegui on the other hand. I am thankful if somebody has a complete example of connectivity to the server and execution of commandos of protocol POP3 (Login, USER, PASS, APOP...
0
3434
by: =?Utf-8?B?Q2hhcmxlcw==?= | last post by:
Like many people, I normally use Yahoo! Mail via the web and like to keep all my emails stored on the Yahoo! server. However sometimes I can’t get access to a PC/the web and I download my emails to a PDA/palmtop via POP3. The PDA uses Windows Mobile software. Since using the PDA, I have had the problem of emails disappearing from the Yahoo! server. However I think I now understand how to avoid this. It seems that Windows Mobile is...
11
10740
by: mp- | last post by:
I want to be able to allow people to check their email from my PHP online application. Given only the users 1) email address, 2) username (if applicable) and 3) password - how can I auto detect a remote IMAP/POP3 server so that I can connect and fetch mail for the user. (Ultimately, I don't want users to have to know what the IMAP/POP3 server name / port is .... I would like to auto-detect all of that). At first I thought I could use...
0
8704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9053
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8936
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8894
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7776
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.