Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected newline in header key #2

Closed
ivs opened this issue Oct 13, 2016 · 8 comments
Closed

Unexpected newline in header key #2

ivs opened this issue Oct 13, 2016 · 8 comments

Comments

@ivs
Copy link

ivs commented Oct 13, 2016

Hi!

I've got "Unexpected newline in header key" on several emails from my mailbox.
I did a little research and have found "\r\nContent-Type..." input to parse_header func.
There's a really dirty workaround, but it works:

pub fn parse_header(raw_data1: &[u8]) -> Result<(MailHeader, usize), MailParseError> {
    let mut index = 0;
    while raw_data1[index] == 13 || raw_data1[index] == 10 {
        index += 1;
    }

    let raw_data = &raw_data1[index..];

    let mut it = raw_data.iter();
// ...
                key: &raw_data[0..v],
                value: &raw_data[ix_value_start..ix_value_end],
            },
                ix + index))
        }

How to fix it correctly?

@staktrace
Copy link
Owner

Can you provide the email you are trying to parse? Feel free to remove any personal information, I just want to see the part upto the end of the headers.

@staktrace
Copy link
Owner

Closing for lack of information.

@arnej
Copy link

arnej commented Oct 5, 2018

Hi

I think I have the same issue here with some mails (or most real world mails I tested).

This is an example mail header part of a mail which has this issue (replaced personal mail address):

Return-Path: <noreply@steampowered.com>
X-Original-To: xxx@xxx.de
Delivered-To: xxx@xxx.de
Received: from smtp06.steampowered.com (smtp06.steampowered.com [208.64.202.48])
	by mailing.xxx.de (Postfix) with ESMTPS id C851EBD24D
	for <xxx@xxx.de>; Tue,  2 Oct 2018 23:50:32 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=steampowered.com; s=smtp;
	h=Date:Message-Id:Content-Type:Subject:MIME-Version:Reply-To:From:To; bh=y598MrfeVkZbRVxU6AWIyW8YNw2KhslI1iRUpo7yfCc=;
	b=tqUUN1Z21q2n4VNts08HU8zzrRXOCa15XPv9eCgEXhTMhap5KT3nwf0IHPzuC/7Vs3u4xBoO/f6kVP9Ho1xvLHrax0L4sRCJgb8h0RW4L8CCeHwerSTyNiK89UKi8ggbCg+ioTiUWu/FiovUSALTWn+M5nwAtT24m0FsEY9rgCA=;
Received: from [208.64.202.21] (helo=valvesoftware.com)
	by smtp06.steampowered.com with smtp (Exim 4.82)
	(envelope-from <noreply@steampowered.com>)
	id 1g7SYs-0005va-O0
	for xxx@xxx.de; Tue, 02 Oct 2018 14:50:34 -0700
To: xxx@xxx.de
From: "Steam" <noreply@steampowered.com>
Reply-To: <noreply@steampowered.com>
Errors-To: <noreply@steampowered.com>
X-Steam-Message-Type: CEmailWishlistItemOnSale
MIME-Version: 1.0
Subject: An item on your Steam wishlist is on sale!
Content-Type: multipart/alternative;
 boundary="np5bb3e829f3727"
Message-Id: <E1g7SYs-0005va-O0@smtp06.steampowered.com>
Date: Tue, 02 Oct 2018 14:50:34 -0700

This is a multi-part message in MIME format.
--np5bb3e829f3727
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Please let me know if you need more information.

@staktrace
Copy link
Owner

What version of mailparse are you using to parse that mail? With the latest version it parses fine for me, I don't see the error message you refer to.

@arnej
Copy link

arnej commented Oct 6, 2018

I used 0.6.4 to parse it.
The mail was fetched with imap 0.8.1 from my dovecot server and I tried to parse it with the following code:

match imap_session.fetch("369", "body[text]") {
    Ok(msgs) => for msg in &msgs {
        print!("{:?}", msg);
        match parse_mail(msg.body().unwrap()) {
            Ok(mail) => {
                println!("Parsed mail:");
                println!("{}", mail.get_body().unwrap());
            }
            Err(e) => println!("Error parsing mail: {}", e)
        }
    },
    Err(e) => println!("Error Fetching mail: {}", e),
};

@staktrace
Copy link
Owner

Are you sure there's no stray characters coming from msg.body()? One thing to try is to dump the msg.body().unwrap() result into a file and pass that file as an argument to my standalone test app at https://github.com/staktrace/mailparse-bin - if it fails with that then we can see what's different in what you pasted above vs in the file.

@arnej
Copy link

arnej commented Oct 6, 2018

It could well be that there are stray characters in that case. I tried a different approach now, which seems to work:

match imap_session.fetch("369", "rfc822") {
    Ok(msgs) => for msg in &msgs {
        print!("{:?}", msg);
        match parse_mail(msg.rfc822().unwrap()) {
            Ok(mail) => {
                println!("Parsed mail:");
                println!("{}", mail.subparts[0].get_body().unwrap());
            }
            Err(e) => println!("Error parsing mail: {}", e)
        }
    },
    Err(e) => println!("Error Fetching mail: {}", e),
};

Using this, I can see the real content of the mails and don't see the 'Unexpected newline in header key' message anymore. I tested this with ~30 mails and all seems to work fine.

Thank you very much for your help.
I hope that this information can be useful when someone else tries to use mailparse in combination with rust-imap 😄

@staktrace
Copy link
Owner

Great, thanks for the update!

@staktrace staktrace reopened this Oct 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants