0

When using Email library, is there a way to get the internet email address without the name, so for "Jon Doe"<jon.doe@gmail.comm>, how can I just get jon.doe@gmail.com.

I am loading a mime message from file with email.message_from_binary_file() and then getting the FROM header by accessing the "from" key:

message = email.message_from_binary_file()
from_header = message["from"]

I can't seem to find in Email docs how I can parse this down further to just the email address?

EDIT: Current solution that I don't think is very elegant and might be error-prone is to do a regex likes so:

from_header = re.findall('\S+@\S+', message["from"])[0][1:-1]

So get the first (and only) element from regex list and then remove the opening and closing brackets ("<" & ">")

wjandrea
  • 28,235
  • 9
  • 60
  • 81
kravb
  • 417
  • 4
  • 16
  • 1
    Does this answer your question? [Get sender email address with Python IMAP](https://stackoverflow.com/questions/5174770/get-sender-email-address-with-python-imap) – BallpointBen Feb 23 '21 at 19:51
  • @BallpointBen Yes, that is exactly what I needed. Thank you. – kravb Feb 23 '21 at 20:08

0 Answers0