Reputation: 3866
I am using python standard email parsing library to parse the raw email that I am getting from amazon ses mail service.
Below is my code for the same.
import json
import email
from email.Utils import parseaddr
def parse(raw_email):
message = email.message_from_string(raw_email)
text_plain = None
text_html = None
for part in message.walk():
if part.get_content_type() == 'text/plain' and text_plain is None:
text_plain = part.get_payload()
if part.get_content_type() == 'text/html' and text_html is None:
text_html = part.get_payload()
parsed_email_object = {
'to': parseaddr(message.get('To'))[1],
'from': parseaddr(message.get('From'))[1],
'delivered to': parseaddr(message.get('Delivered-To'))[1],
'subject': message.get('Subject'),
'text_plain': text_plain,
'text_html': text_html,
}
json_string = json.dumps(parsed_email_object)
return json_string
when I am parsing my raw email, it is not parsing 100%, it is giving me unwanted characters like this
this is a replyo from the gmail indbo asdf asdf asdfa sdfa=
sd sdfa sdfa fasd
=C2=A0dfa sf asdf
a sdfas
<= div>f asdf=C2=A0
Is there anything else like some decoding option to parse it correctly.
Upvotes: 1
Views: 709
Reputation: 1502
Making my comment as an answer so that it gets noticed.
part.get_payload(decode=True).decode(part.get_content_charset())
This will solve the issue of encoding
Upvotes: 2