Question

I'm attempting to return ONLY the message content of a message that I am fetching from an IMAP server. I feel like I got this to work at one point without an issue, but I'm currently getting a bunch of garbage returned. Here is the content I get back from the server currently (including a bunch of garbage that I don't want).

My code is something like the following:

server.select('INBOX')
status, ids = server.search(None, 'UnSeen')

latest_id = ids[0]
print 'latest id is', latest_id
status, msg_data = server.fetch(latest_id, '(RFC822)')
raw_data = msg_data[0][1]
print 'raw msg data: ', raw_data

My goal is to isolate the character string "Abc" out of this:

raw msg data: Delivered-To: Received: by with SMTP id k5csp25808iba; Tue, 22 May 2012 16:52:37 -0700 (PDT) Return-Path: <3xSa8TyIUAIYBIBIEJFFGFA.BFBACICAAIB.dGgfq0IEl2373.5ysmo.qyyqvo.myw@grandcentral.bounces.google.com> Received-SPF: pass (google.com: domain of 3xSa8TyIUAIYBIBIEJFFGFA.BFBACICAAIB.dGgfq0IEl2373.5ysmo.qyyqvo.myw@grandcentral.bounces.google.com designates as permitted sender) client-ip=10.52.24.68; Authentication-Results: mr.google.com; spf=pass (google.com: domain of 3xSa8TyIUAIYBIBIEJFFGFA.BFBACICAAIB.dGgfq0IEl2373.5ysmo.qyyqvo.myw@grandcentral.bounces.google.com designates as permitted sender) smtp.mail=3xSa8TyIUAIYBIBIEJFFGFA.BFBACICAAIB.dGgfq0IEl2373.5ysmo.qyyqvo.myw@grandcentral.bounces.google.com; dkim=pass header.i=3xSa8TyIUAIYBIBIEJFFGFA.BFBACICAAIB.dGgfq0IEl2373.5ysmo.qyyqvo.myw@grandcentral.bounces.google.com Received: from mr.google.com ([10.52.24.68]) by with SMTP id s4mr15641011vdf.3.1337730757212 (num_hops = 1); Tue, 22 May 2012 16:52:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:references:message-id:date:subject:from:to :content-type; bh=KIYz0SegimNSAAbGsi5167k+DIHSSlVjHp4RK92QeKo=; b=K2//5fPk82ocgnfM6iY4abv/RnxBzlbpnhSosuzkhf8cASjQmyLI0fshrY/xFWY1kh dQHWDqnpdPW5JhF8kODPkX6nDUCTmA81954ROjmqorUHqoqrqNgcJg17M5PPhfMCduhm HaavvpGomS8M29A2Yw81p6+BDSLHDOsVaqXKM1SuuBjslBYZZNEz3YNDsZWHWop64O6V STWCiuXhdAwxqD3Ruuh1ZTbxrmHMimKZfGtF5wahvy8prRQibdBd21eW05fk7PHwalg9 vrsKpGD/xYzpGzEc7nUzIvirUlHFebN3r7JqO463ZKpWgKX/zEdG/uO2OFdxsYdqThow EBDg== MIME-Version: 1.0 Received: by 10.52.24.68 with SMTP id s4mr10817316vdf.3.1337730757202; Tue, 22 May 2012 16:52:37 -0700 (PDT) References: <+18184955650.77d431b4360d0f0785312b85196039cf7bf69257@txt.voice.google.com> Message-ID: <+18184955650.d58cd371f90b1e89b989489c4752e917791953de@txt.voice.google.com> Date: Tue, 22 May 2012 23:52:37 +0000 Subject: SMS from From: "" <.T6WVgq84bs@txt.voice.google.com> To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed; delsp=yes

Abc

Was it helpful?

Solution

I was able to find a reference for the strings passed to server.fetch() (as mentioned above) here. This answered my question, since I can isolate the string I want via the following:

status, msg_data = server.fetch(some_id, '(UID BODY[TEXT])')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top