gmail
gmail copied to clipboard
Retrieve FROM email address of a message
I couldn't find a way to retrieve FROM email address of a message. After analysing, I just figured out a way to get it. I just parsed the headers dict., split the required string and extracted the email address.
Here is a code snippet that I've added in Message class in message.py :
def get_from_address(self):
authHeader = self.headers["Authentication-Results"]
emailString = authHeader.split(";")[1]
tempList = emailString.split("=")
return tempList[len(tempList)-1]
It works. But its purely a dirty fix, I agree. Is there any better alternative ?
I'm using something like:
def _parseAdress(self, addr):
return email.utils.parseaddr(addr)
fr = self._parseAdress(message.fr)
to = self._parseAdress(message.to)
caracteristics['a_from'] = fr[1]
caracteristics['name_from'] = self._cleanSubject(fr[0])
We should implement something smarter if needed
If it helps ...
Yes. But problem with your approach is addr
contains only name instead of email and name both.
If we get both in fr
then its clean to use `email.utils.parseaddr'. But this is not happening in my case.