Handling Encoded Messages with Python and imaplib

Over the last several days I have been working on writing a script that parses auto-generated help desk tickets and stores their contents to a database. While testing I encountered several emails that appear to be encoded and causing the script to fail. Below is an example of one of the RFC822s:

"[(b'9255 (RFC822 {12558}', b'Delivered-To: XXXXXXXXX\r\nReceived: by 10.220.77.132 with SMTP id g4csp176213vck;\r\n Mon, 28 Jul 2014 09:37:05 -0700 (PDT)\r\nX-Received: by 10.67.30.130 with SMTP id ke2mr39896936pad.44.1406565425185;\r\n Mon, 28 Jul 2014 09:37:05 -0700 (PDT)\r\nReturn-Path: \r\nReceived: from XXXXXXXXX (XXXXXXXXX [74.125.149.112])\r\n by XXXXXXXXX with SMTP id yh3si18379315pab.170.2014.07.28.09.37.04\r\n for ;\r\n Mon, 28 Jul 2014 09:37:04 -0700 (PDT)\r\nReceived-SPF: none (XXXXXXXXX: XXXXXXXXX does not designate permitted sender hosts) client-ip=74.125.149.141;\r\nAuthentication-Results: XXXXXXXXX;\r\n spf=neutral (XXXXXXXXX: XXXXXXXXX does not designate permitted sender hosts) v\r\nReceived: from XXXXXXXXX ([74.125.149.141]) by XXXXXXXXX ([74.125.148.10]) with SMTP;\r\n\tMon, 28 Jul 2014 16:37:04 GMT\r\nReceived: from XXXXXXXXX ([209.85.213.178]) (using TLSv1) by XXXXXXXXX ([74.125.148.12]) with SXXXXXXXXX; Mon, 28 Jul 2014 09:37:04 PDT\r\nReceived: by XXXXXXXXX with SMTP id uq10sf3897971igb.11\r\n for ; Mon, 28 Jul 2014 09:37:03 -0700 (PDT)\r\nX-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n d=1e100.net; s=20130820;\r\n h=x-gm-message-state:mime-version:from:to:date:subject:message-id\r\n :x-original-sender:x-original-authentication-results:precedence\r\n :mailing-list:list-id:list-post:list-help:list-archive\r\n :list-unsubscribe:content-type:content-transfer-encoding;\r\n bh=H+FlcmWQAFURCHnDFK/bNHUOvofUAPB8bcDYlBceyxE=;\r\n b=LoR8D1MK8eoDG9DLkP9gkfR82+EGUIEeOTLpqymqxyx9HJl0C9BW6iwPD7OgrJFbV4\r\n xWYumML6RCinpcZc4d6VCDSw+akXLdhiol+lbWJBZWvgN4BQPgHJwCF6EaHYf3h8j4tq\r\n /KAZIkXowz4/WKW8STri4BVjlA2a4LPwV/wazP+I9Kvr1yz433ymd+iCY1V0NexTI+cb\r\n 9m3IyL8sqB0+Efyu+XQrR2y7ZdXDPwdzGS/WNHJBtKga5xPDtPga+21pozVMCbuCc/cj\r\n Cx9me6cVo19PrNKIOtSimDZ1u6ELdpVr4iprYQSaT8arYYiCPHJE34OFPlqspTxjm1eI\r\n ngyg==\r\nX-Gm-Message-State: ALoCoQkb908wRLWedDE+CtRzjD6VwC6Nja6duttyoVAdf+TFFn+uCxFB0Kwd5jk411YWdMD2G6HuFeRj2y3q7EzTe/vTvPLfymDIkHwZQa1r1zQ8I1B254t6v01ourR8InF/41aPGnnD\r\nX-Received: by 10.42.48.74 with SMTP id r10mr26049776icf.18.1406565423564;\r\n Mon, 28 Jul 2014 09:37:03 -0700 (PDT)\r\nX-Received: by 10.42.48.74 with SMTP id r10mr26049775icf.18.1406565423537;\r\n Mon, 28 Jul 2014 09:37:03 -0700 (PDT)\r\nX-BeenThere: XXXXXXXXX\r\nReceived: by 10.50.153.15 with SMTP id vc15ls1961411igb.42.gmail; Mon, 28 Jul\r\n 2014 09:37:03 -0700 (PDT)\r\nX-Received: by 10.66.254.37 with SMTP id af5mr39703901pad.113.1406565423331;\r\n Mon, 28 Jul 2014 09:37:03 -0700 (PDT)\r\nReceived: from XXXXXXXXX (XXXXXXXXX [74.125.149.158])\r\n by XXXXXXXXX with SMTP id da9si9190520pdb.425.2014.07.28.09.37.02\r\n for ;\r\n Mon, 28 Jul 2014 09:37:03 -0700 (PDT)\r\nReceived-SPF: none (XXXXXXXXX: XXXXXXXXX does not designate permitted sender hosts) client-ip=207.211.31.47;\r\nReceived: from XXXXXXXXX ([207.211.31.47]) by XXXXXXXXX ([74.125.148.10]) with SMTP;\r\n\tMon, 28 Jul 2014 16:37:02 GMT\r\nReceived: from XXXXXXXXX (XXXXXXXXX\r\n [129.135.112.43]) (Using TLS) by XXXXXXXXX; Mon, 28 Jul\r\n 2014 12:37:01 -0400\r\nReceived: from XXXXXXXXX (129.135.128.210) by XXXXXXXXX\r\n (129.135.112.45) with Microsoft SMTP Server id 14.3.181.6; Mon, 28 Jul 2014\r\n 11:36:58 -0500\r\nReceived: from ITSDC50 ([127.0.0.1]) by XXXXXXXXX with Microsoft\r\n SMTPSVC(6.0.3790.4675);\t Mon, 28 Jul 2014 11:36:58 -0500\r\nMIME-Version: 1.0\r\nFrom: \r\nTo: \r\nDate: Mon, 28 Jul 2014 11:36:58 -0500\r\nSubject: Dispatching IT/Cares Case: SC-118656-7031\r\nMessage-ID: \r\nX-OriginalArrivalTime: 28 Jul 2014 16:36:58.0498 (UTC) FILETIME=[26792E20:01CFAA82]\r\nX-MC-Unique: 114072812370105901\r\nX-pstn-levels: (S:85.19264/99.90000 CV:99.9000 FC:95.5390 LC:95.5390 R:95.9108 P:95.9108 M:97.0282 C:98.6951 )\r\nX-pstn-dkim: 0 skipped:not-enabled\r\nX-pstn-settings: 1 (0.1500:0.1500) cv gt6 gt5 gt4 gt3 gt2 gt1\r\nX-pstn-addresses: from [1094/49]\r\nX-pstn-nxpr: disp=neutral, envrcpt=XXXXXXXXX\r\nX-pstn-nxp: bodyHash=9500f76054cf97c2a0eec20f8940768958faf6c3, headerHash=eb9362a172738328a8b8a8ae406c42a63f5545f9, keyName=4, rcptHash=e0dd4695780dcb1818e78b482447ac976870bcbe, sourceip=207.211.31.47, version=1\r\nX-Original-Sender: XXXXXXXXX\r\nX-Original-Authentication-Results: XXXXXXXXX; spf=neutral\r\n (XXXXXXXXX: XXXXXXXXX does not designate permitted sender\r\n hosts) smtp.mail=XXXXXXXXX\r\nPrecedence: list\r\nMailing-list: list XXXXXXXXX contact XXXXXXXXX\r\nList-ID: \r\nX-Google-Group-Id: 511158325204\r\nList-Post: , \r\nList-Help: , \r\nList-Archive: \r\nList-Unsubscribe: ,\r\n \r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding: base64\r\nX-pstn-neptune: 0/0/0.00/0\r\nX-pstn-levels: (S:65.87536/99.90000 CV:99.9000 FC:95.5390 LC:95.5390 R:95.9108 P:95.9108 M:97.0282 C:98.6951 )\r\nX-pstn-dkim: 0 skipped:not-enabled\r\nX-pstn-settings: 5 (2.0000:0.0200) s cv fc lc gt6 gt5 gt4 GT3 gt2 gt1 ft lt r p m c \r\nX-pstn-addresses: from [db-null] \r\nX-pstn-nxpr: disp=neutral, envrcpt=XXXXXXXXX\r\nX-pstn-nxp: bodyHash=45f4f2e59005199791055b3d1f937e1d3fb7d7ca, headerHash=ca981838d5783da04d9d38e3fffc3f5907100fcf, keyName=4, rcptHash=4f3dee680a09495dc5b095849a4225f49c4a45f4, sourceip=74.125.149.141, version=1\r\n\r\nQ2FzZSBOdW1iZXI6ICAgICAgICAgU0MtMTE4NjU2LTcwMzENClNldmVyaXR5IExldmVsOiAg\r\nICAgIE5vcm1hbA0KQWNjb3VudCBOYW1lOiAgICAgICAgSENSIE1hbm9yY2FyZQ0KU2l0ZSBO\r\nYW1lOiAgICAgICAgICAgMzAxDQpDbGllbnQgTmFtZTogICAgICAgICBBbWFuZGEgUGVucm9k\r\nDQpDbGllbnQgUGhvbmU6ICAgICAgICANCkNsaWVudCBNYWlsUGF0aDogICAgIGFtYW5kYS5w\r\nZW5yb2RAaGNyLW1hbm9yY2FyZS5jb20NCkNhc2UgUHJvZHVjdDogICAgICAgIEhDUi1GaWVs\r\nZCBEZXBsb3ltZW50DQpDYXNlIEtleXdvcmQ6ICAgICAgICBGRC1BU0QNCg0KDQoNClBsZWFz\r\nZSBDbGljayBCZWxvdyB0byBVcGRhdGUgQ2FzZTogDQoNCg0KUHJvYmxlbSBEZXNjcmlwdGlv\r\nbg0KKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqK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XNzYWdlcyBm\r\ncm9tIEFTRCAoZXhjZXB0IGZvciB3d3cuYXNkLXVzYS5jb20sIHdoaWNoIG1hbmFnZXMgZW1h\r\naWwgcHJlZmVyZW5jZXMgdGhyb3VnaCBhIHNlcGFyYXRlIHByb2Nlc3MpIGF0IHRoaXMgZW1h\r\naWwgYWRkcmVzcyBieSBmb3J3YXJkaW5nIHRoaXMgbWVzc2FnZSB0byBsZWF2ZW1lYWxvbmVA\r\nYXNkLXVzYS5jb20uICBJZiB5b3UgZG8gc28sIHRoZSBzZW5kZXIgb2YgdGhpcyBlbWFpbCB3\r\naWxsIGJlIG5vdGlmaWVkIHByb21wdGx5IGFuZCB5b3Ugd2lsbCBub3QgYmUgY29udGFjdGVk\r\nIGFnYWluLiAgT3VyIHByaW5jaXBhbCBwb3N0YWwgYWRkcmVzcyBpcyA3NzUgR29kZGFyZCBD\r\nb3VydCBBbHBoYXJldHRhLCBHQSAgMzAwMDUuDQoNCg==\r\n'), b')']"

Is the body of the email encoded? and if so, how should I handling decoding it?

THe body is encoded (Content-Transfer-Encoding: base64) which is not the same as encrypted. Pasting the first set of characters into an online decoder

Q2FzZSBOdW1iZXI6ICAgICAgICAgU0MtMTE4NjU2LTcwMzENClNldmVyaXR5IExldmVsOiAg

gets decoded as

Case Number:         SC-118656-7031
Severity Level:  

Python has libraries to decode base64, but I'd be disappointed if imaplib didn't have a builtin to simplify this.

Decoding emails in Python e.g. for GMail and imapclient lib · GitHub, :param message_body: Raw 7-bit message body input e.g. from imaplib. Double encoded in quoted-printable and latin-1. :return: Message body as unicode  Select a mailbox. Returned data is the count of messages in mailbox (EXISTS response). The default mailbox is 'INBOX'. If the readonly flag is set, modifications to the mailbox are not allowed. IMAP4.send (data) ¶ Sends data to the remote server. You may override this method. Raises an auditing event imaplib.send with arguments self, data.

You can use the email package for this. You have a list there, and the first item of the list is a tuple, of which the second element is the whole email message. Let’s say you have that bytes object in a variable called msg_bytes. You can then parse the message using:

import email.parser
msg = email.parser.BytesParser().parsebytes(msg_bytes)

You can then access different parts of the message (refer to the documentation at email.message.Message):

# get a bytes object containing the base64-decoded message
textbytes = msg.get_payload(decode=True)

# get the content charset
content_charset = msg.get_content_charset()

# decode the text to obtain a string object
text = textbytes.decode(content_charset)

This will be able to deal with most, if not all, valid emails.

imaplib — IMAP4 protocol client, This is a subclass derived from IMAP4 that connects over an SSL encrypted socket (to use this Note that IMAP4 message numbers change as the mailbox changes; in particular, Authenticate command — requires response processing​. I'm using pythons imaplib to connect to my gmail account. I want to retrieve the top 15 messages (unread or read, it doesn't matter) and display just the subjects and sender name (or address) but

Try Imbox, here you don't need to fix codecs

Because imaplib is a very excessive low level library and returns results which are hard to work with

Installation

pip install imbox

Usage

from imbox import Imbox

with Imbox('imap.gmail.com',
        username='username',
        password='password',
        ssl=True,
        ssl_context=None,
        starttls=False) as imbox:

    all_inbox_messages = imbox.messages()
    for uid, message in all_inbox_messages:
        message.sent_from
        message.sent_to
        message.body

email.header: Internationalized headers, In the current API encoding and decoding of headers is handled transparently by RFC 2822 is the base standard that describes the format of email messages. The message_parts parameter to fetch() lets you be quite fine-grained. Here's a simple example of how to fetch just the header: import imaplib from email.parser import HeaderParser conn = imaplib.IMAP4('my.host.com') conn.login('my@username.com', 'mypassword') conn.select() conn.search(None, 'ALL') # returns a nice list of messages

To get the base64 decoded email generated by Imaplib, it can easily be decoded using base64 module and it should involve following steps.

# import python base64 and re modules
import base64
import re

# Save the encoded part in a variable, e.g re.search grabs the encoded part after string "version=1\r\n\r\n" and saves to the variable named 'decoded'.
decoded = re.search('version=1\r\n\r\n(.*)', email, re.DOTALL)

# prints the decoded message
print(base64.b64decode(decoded.group(1)))

If you want to grab the encoded part between two strings then search expression will be.

decoded = re.search('string1(.*)string2', email)

Where 'email' variable contains the whole email content generated by Imaplib.

imaplib - IMAP4 client library, imaplib implements a client for communicating with Internet Message Access Protocol The first, IMAP4, uses clear text sockets; IMAP4_SSL uses encrypted You probably do not want to store email passwords in clear text, but handling  The following are code examples for showing how to use imaplib.IMAP4().They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Connecting to Gmail with IMAP and Python, Python has an IMAP library: imaplib that allows us to IMAP very easily, Double encoded in quoted-printable and latin-1 :return: Message body as and we are already going through new emails reading titles and bodies,  imaplib implements a client for communicating with Internet Message Access Protocol (IMAP) version 4 servers. The IMAP protocol defines a set of commands sent to the server and the responses delivered back to the client.

imaplib, imaplib implements a client for communicating with Internet Message Access uses clear text sockets; IMAP4_SSL uses encrypted communication over SSL You probably do not want to store email passwords in clear text, but handling  The message_set options to commands below is a string specifying one or more messages to be acted upon. It may be a simple message number ('1'), a range of message numbers ('2:4'), or a group of non-contiguous ranges separated by commas ('1:3,6:9'). A range can contain an asterisk to indicate an infinite upper bound ('3:*').

imap library does not like foreign languages for email subjects , Is this an issue with the encoding in imaplibrary? You received this message because you are subscribed to the Google Groups Python imaplib; but it may be ok for simple tasks. I have been trying to write my own keyword to handle just getting the subject but man the decoding is driving me crazy. The message_set options to commands below is a string specifying one or more messages to be acted upon. It may be a simple message number ( '1' ), a range of message numbers ( '2:4' ), or a group of non-contiguous ranges separated by commas ( '1:3,6:9' ). A range can contain an asterisk to indicate an infinite upper bound ( '3:*' ).

Comments
  • I have been attempting to use the following statements, and what I believe to be their equivalents from the official library documentation (docs.python.org/3/library/email.parser.html and docs.python.org/3/library/email.message.html) and receive the following errors: AttributeError: 'Parser' object has no attribute 'parsebytes', AttributeError: 'Parser' object has no attribute 'get_payload', AttributeError: 'Parser' object has no attribute 'get_content_charset'
  • I've tried on a fresh installation of 3.4.1 on Windows and from python3 from Ubuntu repositories.
  • @socialdtk Sorry, I made a mistake. It must be BytesParser instead of Parser.
  • I will suggest others to use the above package. It works flawlessly. it is also simpler compared to imap library.