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

Already on GitHub? Sign in to your account

Cannot send Mime Version Email from File #2606

zfbot opened this Issue Sep 28, 2012 · 9 comments


None yet
6 participants

zfbot commented Sep 28, 2012

Jira Information

Original Issue:ZF2-581
Issue Type:Bug


This actually has more to do with Zend\Mail\Storage\Message

Here are the email headers giving the problems (I truncated them, since it contains an image) -> http://pastebin.com/x0iPrF48
This email was created using this example -> http://zf2.readthedocs.org/en/latest/modules/zend.mail.file.options.html

So the email is stored in a file and later I open it with $message = new Zend\Mail\Storage\Message(array('file' => $filename));
When you attempt to send the email using Zend\Mail\Message you are given this error:

Caught Exception
Zend\Mail\Exception\RuntimeException does not match header format!
This is a message in Mime Format. If you see this, your mail reader does not support this format.

After trying several things out I decided to open the email saved to file and remove the line regarding it being in Mime Format. To my surprise, the email was instantly sent without a problem. Looking at the email in my inbox, I see the Mime Format line was automagically added back and everything worked as expected.

This does not happen when the email is not in Mime Format, it works perfectly and as expected.

zfbot commented Sep 28, 2012

This issue was ported from the ZF2 Jira Issue Tracker at

Known GitHub users mentioned in the original message or comment:
@diemuzi, @weierophinney


ralphschindler commented Feb 8, 2013

@dimuzi, is this still an issue?


diemuzi commented Feb 8, 2013

Let's close it for now, this was a long time ago and things have changed. When I get some more time I'll try again and if it's still an issue I'll open a new bug


ralphschindler commented Feb 8, 2013

Thanks @diemuzi

timdev commented Aug 28, 2013

@diemuzi, @ralphschindler - this is still an issue today. The issue appears to be related to the difference in values between Zend\Mail\Headers::EOL ('\r\n') and Zend\Mime\Mime::LINEEND (\n).

I'm not conversant enough with the relevant RFCs to say for sure what's wrong, but it seems suspicious that these two libraries would define line endings differently.

What ends up happening is that mail written to disk uses '\r\n', but Zend\Mime\Decode::splitMessage() splits headers/body on "\n\n" (by default). In my case, it's splitting in the middle of the body, which uses "\n" internally.

Then, splitMessage() calls Headers::fromString(), which eventually encounters the default body and throws an exception.

A workaround in userland is to eschew Storage\Message and do things manually in order to specify the fourth parameter to splitMessage:

$file = 'some/file/containing/message.txt'
$rawMsg = file_get_contents($file);

$message = new \Zend\Mail\Message();
$headers = null;
$content = null;

\Zend\Mime\Decode::splitMessage($rawMsg, $headers, $content, \Zend\Mail\Headers::EOL);

$boundary = $headers->get('contenttype')->getParameter('boundary');

$mimeMsg = \Zend\Mime\Message::createFromMessage($content, $boundary);


Sorry the best I can do is describe the behavior. I'm not sure what the standards-friendly fix for this might be.


weierophinney commented Aug 28, 2013

@timdev header lines, as defined in the RFCs, do use \r\n; message bodies typically use \n, though they can use either style.


diemuzi commented Aug 28, 2013

@weierophinney , @timdev , @ralphschindler

Actually I reopened this bug over six months ago and it hasn't had any attention since then.


There you will find more examples and problems I've faced.

timdev commented Aug 28, 2013

@weierophinney Thanks for RTFMing for me. I should have done so yesterday, but it was late. I've done a bit of reading today to try to catch up.

It seems the crux here is that \Zend\Mail\Message::toString() separates headers from body using "\r\n\r\n", which is correct. toString() gets called, for instance, in \Zend\Mail\Transport\File::send()

But then, when reading that message back from the file, you inevitably hit \Zend\Mime\Decode::splitMessage(). (If you start with the constructor to \Zend\Mail\Storage\Message, it gets invoked in Storage\Part's constructor. splitMessage() (by default), checks for "\n\n" first. So if the body text is plain text 8-bit encoded, and contains "\n\n", things break.

An easy way to see this in action is to tweak this line in the test.

$body = "body\n\nwith newlines";

causes the test to fail.

If I have some time this weekend, I may be able to dig in further. But I'm afraid any attempt at a fix I make is likely to cause regressions elsewhere (I'm scared of mail).

Now after another three years this Bug is still there. A quick fix seems to be

$rawMsg = preg_replace('~\R~u', "\r\n", $rawMsg);

but i don't like to do things this way. Is there any better way to fix this?
I hope i don't need to import classes from PEAR to seperate Attachments from Mails in ZF3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment