import tarfile
import email

def iterate_emails(tar_path):
    """Extract individual email messages from the tar file located at 
       tar_path. Returns a generator object. """
    tar = tarfile.open(tar_path)
    emails = (f for f in tar if f.isfile())
    for info in emails:
        f = tar.extractfile(info)
        ## parse contents of compressed file into an Email-object:
        msg = email.message_from_binary_file(f)
        yield msg
        f.close()
        
        
def mail_text(msg):
    """Decode and extract the headers and body from the Email message 
       object given as msg, and return them as a single string. """
    headers = []
    for k, v in msg.items():
        headers.append(k)
        if type(v) is str:
            headers.append(v)
    text_parts = (p for p in msg.walk() 
                  if p.get_content_type().startswith('text'))
    contents = []
    for txt in text_parts:
        charset = txt.get_content_charset()
        try:
            ## decode MIME encoding
            payload = txt.get_payload(decode=True)
            try:
                payload = payload.decode(charset)
            except:
                ## if the charset from the header doesn't work, force UTF-8
                payload = payload.decode('utf-8', 'replace')
            contents.append(payload)
        except:
            contents.append(txt.get_payload())
    return " ".join(headers + contents)