[MERGE] An EmailMessage class for bzrlib (v2)
Adeodato Simó
dato at net.com.org.es
Mon Jul 9 14:52:41 BST 2007
Hello. I attach a new bundle with this issues addressed:
> Martin Pool has voted +1.
> I would like some documentation of what 'config' is here.
Changed. See below for your comment on non-mime messages.
=== modified file 'bzrlib/email_message.py'
--- bzrlib/email_message.py 2007-06-26 19:49:21 +0000
+++ bzrlib/email_message.py 2007-07-09 13:07:15 +0000
@@ -94,9 +94,10 @@
attachment_filename=None):
"""Create an email message and send it with SMTPConnection.
- See SMTPConnection.__init__(), EmailMessage.__init__() and
- EmailMessage.add_inline_attachment() for an explanation of the
- parameters.
+ :param config: config object to pass to SMTPConnection constructor.
+
+ See EmailMessage.__init__() and EmailMessage.add_inline_attachment()
+ for an explanation of the rest of parameters.
"""
msg = EmailMessage(from_address, to_address, subject, body)
if attachment is not None:
> John Arbash Meinel has voted +1 (conditional).
> This needs to be in a try/finally, so that a failure doesn't mess up the
> system:
Changed. Or, more appropriately, fixed. Looking at the original code:
+ old_send_email = SMTPConnection.send_email
+ EmailMessage.send(FakeConfig(), 'from at from.com', 'to at to.com',
+ 'subject', u'b\xf3dy', u'a\nb\nc\nd\ne\n', 'lines.txt')
+ SMTPConnection.send_email = old_send_email
SMTPConnection.send_email is saved, but it's not set to a new value!
(Which means the test sends real mail...) D'oh.
=== modified file 'bzrlib/tests/test_email_message.py'
--- bzrlib/tests/test_email_message.py 2007-06-26 19:49:21 +0000
+++ bzrlib/tests/test_email_message.py 2007-07-09 13:28:29 +0000
@@ -105,15 +105,18 @@
def get_user_option(self, option):
return None
- def send_email(msg):
+ def send_email(_self, msg):
msg.set_boundary(BOUNDARY)
self.assertEqualDiff(COMPOUND_MESSAGE % ('utf-8', 'plain'),
msg.as_string())
old_send_email = SMTPConnection.send_email
- EmailMessage.send(FakeConfig(), 'from at from.com', 'to at to.com',
- 'subject', u'b\xf3dy', u'a\nb\nc\nd\ne\n', 'lines.txt')
- SMTPConnection.send_email = old_send_email
+ try:
+ SMTPConnection.send_email = send_email
+ EmailMessage.send(FakeConfig(), 'from at from.com', 'to at to.com',
+ 'subject', u'b\xf3dy', u'a\nb\nc\nd\ne\n', 'lines.txt')
+ finally:
+ SMTPConnection.send_email = old_send_email
def test_address_to_encoded_header(self):
def decode(s):
> I think we want a way to set
> the attachment type from the simple 'send()' helper, because otherwise
> attachments show up as 'text/plain' rather than 'text/x-patch'. That is
> fairly minor.
Added. I changed the send_email() function in the test to return a
function instead, in order to test with different mime_subtypes. Please
let me know if that is okay.
=== modified file 'bzrlib/email_message.py'
--- bzrlib/email_message.py 2007-07-09 13:07:15 +0000
+++ bzrlib/email_message.py 2007-07-09 13:34:17 +0000
@@ -91,7 +91,7 @@
@staticmethod
def send(config, from_address, to_address, subject, body, attachment=None,
- attachment_filename=None):
+ attachment_filename=None, attachment_mime_subtype='plain'):
"""Create an email message and send it with SMTPConnection.
:param config: config object to pass to SMTPConnection constructor.
@@ -101,7 +101,8 @@
"""
msg = EmailMessage(from_address, to_address, subject, body)
if attachment is not None:
- msg.add_inline_attachment(attachment, attachment_filename)
+ msg.add_inline_attachment(attachment, attachment_filename,
+ attachment_mime_subtype)
SMTPConnection(config).send_email(msg)
@staticmethod
=== modified file 'bzrlib/tests/test_email_message.py'
--- bzrlib/tests/test_email_message.py 2007-07-09 13:28:29 +0000
+++ bzrlib/tests/test_email_message.py 2007-07-09 13:34:17 +0000
@@ -105,16 +105,23 @@
def get_user_option(self, option):
return None
- def send_email(_self, msg):
- msg.set_boundary(BOUNDARY)
- self.assertEqualDiff(COMPOUND_MESSAGE % ('utf-8', 'plain'),
- msg.as_string())
+ def send_email(mime_subtype='plain'):
+ def wrapper(_self, msg):
+ msg.set_boundary(BOUNDARY)
+ self.assertEqualDiff(COMPOUND_MESSAGE % ('utf-8',
+ mime_subtype), msg.as_string())
+ return wrapper
old_send_email = SMTPConnection.send_email
try:
- SMTPConnection.send_email = send_email
+ SMTPConnection.send_email = send_email()
EmailMessage.send(FakeConfig(), 'from at from.com', 'to at to.com',
'subject', u'b\xf3dy', u'a\nb\nc\nd\ne\n', 'lines.txt')
+
+ SMTPConnection.send_email = send_email('x-patch')
+ EmailMessage.send(FakeConfig(), 'from at from.com', 'to at to.com',
+ 'subject', u'b\xf3dy', u'a\nb\nc\nd\ne\n', 'lines.txt',
+ 'x-patch')
finally:
SMTPConnection.send_email = old_send_email
I also added a NEWS entry, or rather, expanded the existing one:
=== modified file 'NEWS'
--- NEWS 2007-06-25 17:46:47 +0000
+++ NEWS 2007-07-09 13:45:52 +0000
@@ -48,7 +48,8 @@
INTERNALS:
- * New SMTPConnection class to unify email handling. (Adeodato Simó)
+ * New SMTPConnection and EmailMessage classes to unify email handling.
+ (Adeodato Simó)
And now for the other part of your comments:
* Martin Pool [Mon, 09 Jul 2007 00:29:59 -0400]:
> It seems like we would still be able to send non-mime messages by
> constructing a new message object and sending it? We could add parameters
> to the static function to control this.
* John Arbash Meinel [Fri, 06 Jul 2007 14:36:53 -0400]:
> It would be nice if we could have a way to send an email as a single chunk
> (non mime) because that is what I believe PQM needs. (So it means that both
> bzr-email and bzr-pqm can share the email code).
> I don't think it is easy to do, though, because if you start with a single
> body, and then add an attachment, you have to start over (at least that is
> what I understood about e-mail, which is why bzr-email doesn't do it that
> way).
So it seems that non-MIME messages are a wanted feature. :-) I think
that shoehorning this into EmailMessage itself is not a good option,
because you cannot inherit from MIMEMultipart anymore, and have to use a
hidden object, proxying attribute access, etc. (If there's a better way
to achive that, by all means please tell me.)
However, along the lines of what Martin proposes, I think it'd be
feasible to have the staticmethod to act different whenever attachment
is None, creatting a plain email.Message instead of a bzrlib.EmailMessage.
With this, it's only necessary to refactor the code in
EmailMessage.__init__ to a separate method, and do that. How does that
sound?
When is the RC cut? I would like to see the current code merged for
0.18, and add the above for 0.19 if that's okay. Or maybe there's time
to get it into 0.18, though I'm not convinced.
Cheers,
--
Adeodato Simó dato at net.com.org.es
Debian Developer adeodato at debian.org
He has never been known to use a word that might send a reader to the
dictionary.
-- William Faulkner (about Ernest Hemingway)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bzr.email_message.diff
Type: text/x-diff
Size: 77728 bytes
Desc: not available
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20070709/3008b2e6/attachment-0001.bin
More information about the bazaar
mailing list