3

Can someone please tell me how to get the from address from email.message_from_string() in Python? Thanks! I tried

message = email.message_from_string(email_text)
from = message['From']

However, that did not seem to work.

Here is the email I am parsing:

Received: (qmail 16903 invoked by uid 30297); 27 Jul 2013 15:16:51 -0000
Received: from unknown (HELO p3pismtp01-015.prod.phx3.secureserver.net) ([10.6.12.15])
          (envelope-sender <[email protected]>)
          by p3plsmtp10-03.prod.phx3.secureserver.net (qmail-1.03) with SMTP
          for <[email protected]>; 27 Jul 2013 15:16:51 -0000
X-IronPort-Anti-Spam-Result: AkYDACPh81FKfVKyk2dsb2JhbABbgkQCgUVUvQmBEAgWDgEBAQEHCwsJFAQkghsJAQEEAUABGx4DAQsGBQEBAQECBQM4IgERAQUBHAYTh30BAwkGmxaMT4J/hBcKGScNZId0AQUMj3iEBQOXX49nFimDAYFVIA
Received: from mail-we0-f178.google.com ([74.125.82.178])
  by p3pismtp01-015.prod.phx3.secureserver.net with ESMTP; 27 Jul 2013 08:16:51 -0700
Received: by mail-we0-f178.google.com with SMTP id u57so2691391wes.23
        for <[email protected]>; Sat, 27 Jul 2013 08:16:50 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=mime-version:in-reply-to:references:date:message-id:subject:from:to
         :content-type;
        bh=yU9O8/uUeQbclKzg3n9YD5bu4chhx/D44TL6gOzd2ag=;
        b=aFXVUYwpBwvVfAzzTQSbpAIR1rbMq/I32DPy1DFLpgqgbtIrmUCbp/EQJyvqupxXSe
         chZmaH7ALuMurnlTbpjOLqHBZx550U9gmYl/+tk6Ql7HgYOsYW+0eIpeyWGfgAlgyz3O
         ISyiKyjhjLVs6Cw01LJW9n2zgssNqcE8uMqpqnVagl33bbKbI3ODWCvnmjhfwQBVHhjm
         /CMXJ1dUIfcleFHRdH26lWBEm2Z54dYe06EPwJ6zF7LGFRBakF120kY+gUXpnrlfwZi9
         XyFevTI6lvL6+W28b8fxUisGzfL9zmvPb9wHyOpHix5kuvj06Z3FvFhIer03La/Fhd7k
         EWtA==
MIME-Version: 1.0
X-Received: by 10.180.20.228 with SMTP id q4mr2165577wie.60.1374938210558;
 Sat, 27 Jul 2013 08:16:50 -0700 (PDT)
Received: by 10.216.164.9 with HTTP; Sat, 27 Jul 2013 08:16:50 -0700 (PDT)
In-Reply-To: <15669247.20130725035929.51f0a2a1834095.64964363@mail333.us4.mandrillapp.com>
References: <15669247.20130725035929.51f0a2a1834095.64964363@mail333.us4.mandrillapp.com>
Date: Sat, 27 Jul 2013 11:16:50 -0400
Message-ID: <CAND88j9txyLT7YOqcrSoRYZ9-SDHaCwBsc-ZNR_1+iYPrM0N5w@mail.gmail.com>
Subject: Re: New message regarding your stay on 2013-06-19 to 2013-06-29 at
 the The Marriot <SRID2>
From: Taylor Lovett <[email protected]>
To: Taylor Lovett <[email protected]>
Content-Type: multipart/alternative; boundary=f46d04218485a0ea0404e27fbf5b
X-Nonspam: None

--f46d04218485a0ea0404e27fbf5b
Content-Type: text/plain; charset=ISO-8859-1

Test email!

--f46d04218485a0ea0404e27fbf5b
Content-Type: text/html; charset=ISO-8859-1
0

1 Answer 1

6

from is a reserved keyword. Use a different variable name. Your code otherwise works fine:

>>> import email
>>> message = email.message_from_string(email_text)
>>> message['From']
'Taylor Lovett <[email protected]>'
>>> from = message['From']
  File "<stdin>", line 1
    from = message['From']
         ^
SyntaxError: invalid syntax
>>> from_address = message['From']
>>> from_address
'Taylor Lovett <[email protected]>'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.