﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
13433	EmailMessage mangles body text	Leo Shklovskii	Leo Shklovskii	"This is a fun one. If I do the following code in Django:

{{{
from django.core.mail import EmailMessage
message = EmailMessage('blah', 'From puppies','bob@hope.com', ['bob@hope.com'])
message.send()
}}}

The message that appears on the other end has this in the body:
{{{
>From puppies
}}}

A `From` at the beginning of any lines in the body gets a `>` prepended to it.

Digging deep into Python's innards reveals that this is a somewhat esoteric protection in case you're writing out Unix mailbox files. The specific issue is here: http://docs.python.org/release/2.6.2/library/email.generator.html#email.generator.Generator and involves the `mangle_from_` flag.

The hugely more likely case is that if you're using Django's `EmailMessage` class you're sending emails rather than writing Unix mailbox files and are running into this bug that way.

One proposed solution would be to override the `__str()__` methods on `django.core.mail.SafeMIMEText` and `django.core.mail.SafeMIMEMultipart`. That method by default calls `as_string()` which in turn creates a `Generator` with `mangle_from_` set to `True` - see `email.message.Message`. If the implementation in the Django classes looked something like this, it would work fine:
{{{
def __str__(self):
        from email.generator import Generator
        fp = StringIO()
        g = Generator(fp, mangle_from_ = False)
        g.flatten(self, unixfrom=self.unixfrom)
        return fp.getvalue()
}}}

Happy to do patches/test/docs as appropriate but I'd like to have a core committer take a look at it and bless the approach (or punt the bug to 1.3/wontfix/etc...)
"		closed	Core (Mail)	1.2-beta		fixed	mangle_from_		Accepted	1	0	0	1	0	0
