MichaelSc
MichaelSc

Reputation: 127

Using GAE python to receive email, but the Body of the message contains unexpected information

I am receiving email into a GAE python application. The 'to' and 'sender' field contents are as expected, but the body contains additional information before the actual message body. How do I get just the actual message without the added message info?

The added info is the following;

From nobody Thu Dec 11 13:48:29 2014 content-transfer-encoding: 7bit MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8"

My code is the following;

    message = mail.InboundEmailMessage(self.request.body)
    a, b = message.to.split('<',1)
    recip, c = b.split('@', 1)
    logging.debug("The email was to: %s" % recip.upper())
    if recip.upper() == "MESSENGER":
        self.process_Messenger(message)
    if recip.upper() == "SUPPORT":
        #Will add code to forward the email to actual support message box and send a reply.
        logging.debug("We received an email for SUPPORT")
    return

def process_Messenger(self, message):
    logging.debug("In process_Messenger code")
    # Email subjects to Messenger should start with 'Re: ' plus the assemblyid 
    .
    .
    # Construct the message
    messageid = LHMessage.construct_message(my_lhmessage, "assemblyid", message.body, "threadid", "sender")
    .
    .

The code for the construct_message is;

    def construct_message(self, assemblyid, pmessage, threadid, sender):
            logging.debug("In construct_message code")
            message = str(pmessage)
            logging.debug("Processing message: %s" % message)

And the debug message is;

Processing message: From nobody Thu Dec 11 13:48:29 2014 content-transfer-encoding: 7bit MIME-Version: 1.0 Content-Type: text/plain; charset="....

Upvotes: 0

Views: 88

Answers (1)

ChrisC73
ChrisC73

Reputation: 1853

The GAE documentation here is somewhat misleading as you are dealing with an InboundEmailMessage, which inherits from EmailMessage, but does not contain a nice text body as expected it seems: (From link: body: "The plaintext body content of the message.")

You can use the 'bodies' attribute instead, which splits the message into text and html bodies.

I have used this as follows:

text_bodies = message.bodies('text/plain')
for content_type, body in text_bodies:
  text = body.decode()

See this link for more information.

Upvotes: 1

Related Questions