def construct_simple_encoded_message(from_addr, to_addr, subject, body,
                                  other_headers={}, encoding='utf-8'):
    """The python email package makes it very difficult to send quoted-printable
    messages for charsets other than ascii and selected others.  As a result we
    need to do a few things manually here.  All inputs to this method are
    expected to be unicode or ascii.

    We should be able to pass in some arbitrary unicode stuff and get back
    a sensible encoded message:

        >>> m = construct_simple_encoded_message(u'*****@*****.**',
        ...     u'*****@*****.**',
        ...     u'Un Subj\xc3\xa9t',
        ...     u'A simple body with some non ascii t\xc3\xa9xt',
        ...     other_headers = {'X-Test': u't\xc3\xa9st'})
        >>> print m.as_string()
        From: [email protected]
        To: [email protected]
        Subject: Un =?utf-8?b?U3ViasODwql0?=
        Content-Transfer-Encoding: quoted-printable
        Content-Type: text/plain; charset="utf-8"
        X-Test: =?utf-8?b?dMODwqlzdA==?=
        <BLANKLINE>
        A simple body with some non ascii ...
    """

    m = Message()
    m['From'] = encode_header(from_addr, encoding)
    m['To'] = encode_header(to_addr, encoding)
    m['Subject'] = encode_header(subject, encoding)
    # Normally we wouldn't try to set these manually, but the email module
    # tries to be a little too smart here.
    m['Content-Transfer-Encoding'] = 'quoted-printable'
    if 'Content-Type' not in other_headers:
        m['Content-Type'] = 'text/plain; charset="%s"'%encoding
    for key, val in other_headers.items():
        m[key] = encode_header(val, encoding)
    body = body.encode(encoding)
#    body = body_encode(body, eol="\r\n")
    m.set_payload(body)
    return m
    def _convert_to_mbox_msg(self, msg):
        file_ids = list(msg.objectIds('File'))
        encoding = "utf-8"

        # true only if we have attachments
        if file_ids:
            enc_msg = MIMEMultipart()
            txt = MIMEText(msg.body.encode(encoding))
            enc_msg.attach(txt)
        else:
            enc_msg = Message()
            enc_msg.set_payload(msg.body.encode(encoding))

        enc_msg['From'] = encode_header(msg.from_addr, encoding)
        enc_msg['To'] = encode_header(self.context.mailto, encoding)
        enc_msg['Subject'] = encode_header(msg.subject, encoding).replace("\n", " ").strip()
        enc_msg['Date'] = encode_header(str(msg.date), encoding)
        enc_msg['Message-id'] = encode_header(msg.message_id, encoding)
        if msg.references:
            enc_msg['References'] = encode_header(" ".join(msg.references), encoding)
        if msg.in_reply_to:
            enc_msg['In-reply-to'] = encode_header(msg.in_reply_to, encoding)
                                    
        ctime = str(msg.date)
        enc_msg.set_unixfrom("From %s %s" % (parseaddr(msg.from_addr)[1], ctime))

        for file_id in file_ids:
            file = msg._getOb(file_id)
            data = file.data
            if not isinstance(data, basestring):
                data = str(data)
            content_type = file.getContentType()
            if content_type == 'message/rfc822':
                attachment = message_from_string(data)
            else:
                attachment = Message()
                attachment.add_header('Content-Disposition', 'attachment', filename=file.title)
                attachment.add_header('Content-Type', content_type)
                attachment.set_payload(data)
            enc_msg.attach(attachment)

        try:
            retval = enc_msg.as_string(unixfrom=True)
        except TypeError, e:
            raise