Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old October 23rd, 2003, 02:36 PM
specialkay212 specialkay212 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 10 specialkay212 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
trying to use e-mail module

I am trying to supply a text e-mail file to python at the command line, and have the e-mail module print out the sender and recipient of the message. I'm having trouble finding the right functions to use in python's e-mail documentation. Any ideas?

Reply With Quote
  #2  
Old October 23rd, 2003, 03:42 PM
specialkay212 specialkay212 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 10 specialkay212 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
So far I've got:

import sys
from email.Parser import Parser

fp = open(sys.argv[1])
parser=Parser()
msg = parser.parse(fp)
fp.close()

----------

This reads in the email from the file... but how do I get the 'From', 'To', and 'Subject'?

Reply With Quote
  #3  
Old October 23rd, 2003, 04:52 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Ok this should work, i havn't tested it but from what i can make out of the doc's (head still spinning) the get_all() function should return the MIME headers from a parsed email..

Quote:
get_all( name[, failobj])

Return a list of all the values for the field named name. If there are no such named headers in the message, failobj is returned (defaults to None)


cross your fingers..

Code:
import sys 
import email 

fp = open(sys.argv[1]) 
parser=email.Parser() 
msg = parser.parse(fp)
subject = msg.get_all('From', None)
subject = msg.get_all('To', None)
subject = msg.get_all('Subject', None)
fp.close()


you might also find parseaddr() interest, there do seem to be allot of different ways to go about doing this

Quote:
parseaddr( address)

Parse address - which should be the value of some address-containing field such as To: or Cc: - into its constituent realname and email address parts. Returns a tuple of that information, unless the parse fails, in which case a 2-tuple of ('', '') is returned.


Mark.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : October 23rd, 2003 at 05:02 PM.

Reply With Quote
  #4  
Old October 23rd, 2003, 05:16 PM
specialkay212 specialkay212 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 10 specialkay212 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks

Ahh thank you. My problem was that I was not using 'Subject'. That doc file is ridiculous

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > trying to use e-mail module


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway