gmail icon indicating copy to clipboard operation
gmail copied to clipboard

Retrieve FROM email address of a message

Open KartikDomadiya opened this issue 10 years ago • 2 comments

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 ?

KartikDomadiya avatar Jun 24 '14 11:06 KartikDomadiya

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 ...

chocobn69 avatar Jun 24 '14 12:06 chocobn69

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.

KartikDomadiya avatar Jun 24 '14 12:06 KartikDomadiya