Discussion:
how to get the subject of email?
luofeiyu
2014-08-10 01:41:44 UTC
Permalink
I am in python3.4

typ, data = x.con.fetch(b'1', '(RFC822)') #get the first email
text = data[0][1]
message = email.message_from_string(text).get('subject')

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Python34\lib\email\__init__.py", line 40, in message_from_string
return Parser(*args, **kws).parsestr(s)
File "D:\Python34\lib\email\parser.py", line 70, in parsestr
return self.parse(StringIO(text), headersonly=headersonly)
TypeError: initial_value must be str or None, not bytes

message = email.message_from_string(str(text)).get('subject')
message # nothing displayed
John Gordon
2014-08-10 02:01:06 UTC
Permalink
Post by luofeiyu
message = email.message_from_string(str(text)).get('subject')
message # nothing displayed
Try using email.message_from_bytes() instead.

Also have a look at
http://stackoverflow.com/questions/19508393/python-email-parsing-issue
for a question very similar to yours. Perhaps something in the code
will help.
--
John Gordon Imagine what it must be like for a real medical doctor to
gordon at panix.com watch 'House', or a real serial killer to watch 'Dexter'.
luofeiyu
2014-08-10 05:37:22 UTC
Permalink
message = email.message_from_bytes(text)

I get it ,
print(message['Subject']) #can get the subject

But how can i get the body content of message?

no , message['Body'] or message['Content']
Post by John Gordon
Post by luofeiyu
message = email.message_from_string(str(text)).get('subject')
message # nothing displayed
Try using email.message_from_bytes() instead.
Also have a look at
http://stackoverflow.com/questions/19508393/python-email-parsing-issue
for a question very similar to yours. Perhaps something in the code
will help.
luofeiyu
2014-08-10 05:52:01 UTC
Permalink
think you ,i get it .
message = email.message_from_bytes(text)
print(message['Subject']) #can get the subject

But how can i get the body content of message?

message['Body'] or message['Content'] can get none.
Post by John Gordon
Post by luofeiyu
message = email.message_from_string(str(text)).get('subject')
message # nothing displayed
Try using email.message_from_bytes() instead.
Also have a look at
http://stackoverflow.com/questions/19508393/python-email-parsing-issue
for a question very similar to yours. Perhaps something in the code
will help.
Mark Lawrence
2014-08-10 10:28:06 UTC
Permalink
Post by luofeiyu
I am in python3.4
typ, data = x.con.fetch(b'1', '(RFC822)') #get the first email
text = data[0][1]
message = email.message_from_string(text).get('subject')
File "<stdin>", line 1, in <module>
File "D:\Python34\lib\email\__init__.py", line 40, in
message_from_string
return Parser(*args, **kws).parsestr(s)
File "D:\Python34\lib\email\parser.py", line 70, in parsestr
return self.parse(StringIO(text), headersonly=headersonly)
TypeError: initial_value must be str or None, not bytes
message = email.message_from_string(str(text)).get('subject')
message # nothing displayed
Could the answer to your numerous questions be in the docs, in this case
https://docs.python.org/3/library/email.html ?
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence
Loading...