multipart/alternative inside multipart/mixed #32

Closed
rkintzi opened this Issue Mar 12, 2013 · 1 comment

Projects

None yet

1 participant

@rkintzi
rkintzi commented Mar 12, 2013

Following program creates an email message with a body in plain text and in html and with a file attached:

# encoding=utf-8
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.message import Message, Attachment
import transaction

mailer = Mailer.from_settings(dict(queue_path="mail"), prefix='')

m = Message(
        subject = u"Subject",
        recipients = ["r@gdzies-tam-w.pl"],
        body = u"body",
        html = u"<html><header><meta http-equiv='content-type' content='text/html; charset=UTF-8'></header><body>html</body></html>",
        sender = "k@gdzies-tam-w.pl",
        )
m.attach(Attachment(filename="file.txt", content_type="text/plain", data="file content", transfer_encoding='base64'))
mailer.send_to_queue(m)
transaction.commit()

For me this email should be encoded as a multipart/mixed message with multipart/alternative as one of two parts. Something like that:

Content-Type: multipart/mixed; boundary="===============8519163806756713431=="
MIME-Version: 1.0
Message-Id: <20130312132042.32331.40709.repoze.sendmail@jamuna>
Date: Tue, 12 Mar 2013 13:20:42 -0000
X-Actually-From: =?utf-8?q?k=40gdzies-tam-w=2Epl?=
X-Actually-To: =?utf-8?q?r=40gdzies-tam-w=2Epl?=

--===============8519163806756713431==
Content-Type: multipart/alternative;
 boundary="===============5600202058031675837=="
MIME-Version: 1.0

--===============5600202058031675837==
MIME-Version: 1.0
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: 7bit

<html><header><meta http-equiv='content-type' content='text/html; charset=UTF-8'></header><body>html</body></html>
--===============5600202058031675837==
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit

body
--===============5600202058031675837==--
--===============8519163806756713431==
MIME-Version: 1.0
Content-Dispsition: attachment; filename="file.txt"
Content-Transfer-Encoding: base64
Content-Type: text/plain; name="file.txt"; charset="us-ascii"

ZmlsZSBjb250ZW50

--===============8519163806756713431==--

Instead we got:

Content-Type: multipart/mixed; boundary="===============3167731697154423418=="
MIME-Version: 1.0
From: k@gdzies-tam-w.pl
Subject: Subject
To: r@gdzies-tam-w.pl
Message-Id: <20130312125944.32031.34617.repoze.sendmail@jamuna>
Date: Tue, 12 Mar 2013 12:59:44 -0000
X-Actually-From: =?utf-8?q?k=40gdzies-tam-w=2Epl?=
X-Actually-To: =?utf-8?q?r=40gdzies-tam-w=2Epl?=

--===============3167731697154423418==
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit

body
--===============3167731697154423418==
MIME-Version: 1.0
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: 7bit

<html><header><meta http-equiv='content-type' content='text/html; charset=UTF-8'></header><body>html</body></html>
--===============3167731697154423418==
MIME-Version: 1.0
Content-Disposition: attachment; filename="file.txt"
Content-Transfer-Encoding: base64
Content-Type: text/plain; name="file.txt"; charset="us-ascii"

ZmlsZSBjb250ZW50

--===============3167731697154423418==--
@rkintzi rkintzi added a commit to rkintzi/pyramid_mailer that referenced this issue Mar 12, 2013
@rkintzi rkintzi Try to fix two issues:
 * Issue #32 (Pylons#32)
 * Issue #29 (Pylons#29)
a060338
@rkintzi
rkintzi commented Mar 12, 2013

Could someone check my commit [1] and tell if it goes in right direction.

I have implemented two functions to_message() and attachment_to_message(). First one converts a pyramid_mailler.message.Message object to email.Message object. Second does the same with pyramid_mailler.message.Attachment object. These functions do not depends on pyramid_mailer.response module (with exception of encode_string() function). Instead I used standard email.mime module.

I've added three lines of code to change an encoder used for utf-8 charset (to force quted-printable in message body). This probably should be done only if some special option is present in configuration.

I should remove unused code, too.

[1] rkintzi@a060338

@rpatterson rpatterson added a commit that closed this issue Mar 19, 2013
@rpatterson rpatterson Fix messages with both HTML and plain text bodies that also have atta…
…chments:

Closes #32.  Note that, as opposed to the issue description, per
RFC2046 the HTML part should be last inside the multipart/alternative.
367398d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment