#10321: Add support for sending binary DATA and Message objects to smtplib
diff --git a/Doc/includes/email-mime.py b/Doc/includes/email-mime.py
index f64df83..8ddd486 100644
--- a/Doc/includes/email-mime.py
+++ b/Doc/includes/email-mime.py
@@ -27,5 +27,5 @@
# Send the email via our own SMTP server.
s = smtplib.SMTP()
-s.sendmail(me, family, msg.as_string())
+s.sendmail(msg)
s.quit()
diff --git a/Doc/includes/email-simple.py b/Doc/includes/email-simple.py
index 689511e..b069ab0 100644
--- a/Doc/includes/email-simple.py
+++ b/Doc/includes/email-simple.py
@@ -17,8 +17,7 @@
msg['From'] = me
msg['To'] = you
-# Send the message via our own SMTP server, but don't include the
-# envelope header.
+# Send the message via our own SMTP server.
s = smtplib.SMTP()
-s.sendmail(me, [you], msg.as_string())
+s.sendmail(msg)
s.quit()
diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst
index 0c65290..858301d 100644
--- a/Doc/library/smtplib.rst
+++ b/Doc/library/smtplib.rst
@@ -274,9 +274,14 @@
.. note::
The *from_addr* and *to_addrs* parameters are used to construct the message
- envelope used by the transport agents. The :class:`SMTP` does not modify the
+ envelope used by the transport agents. ``sendmail`` does not modify the
message headers in any way.
+ msg may be a string containing characters in the ASCII range, or a byte
+ string. A string is encoded to bytes using the ascii codec, and lone ``\r``
+ and ``\n`` characters are converted to ``\r\n`` characters. A byte string
+ is not modified.
+
If there has been no previous ``EHLO`` or ``HELO`` command this session, this
method tries ESMTP ``EHLO`` first. If the server does ESMTP, message size and
each of the specified options will be passed to it (if the option is in the
@@ -311,6 +316,27 @@
Unless otherwise noted, the connection will be open even after an exception is
raised.
+ .. versionchanged:: 3.2 *msg* may be a byte string.
+
+
+.. method:: SMTP.send_message(msg, from_addr=None, to_addrs=None, mail_options=[], rcpt_options=[])
+
+ This is a convenience method for calling :meth:`sendmail` with the message
+ represented by an :class:`email.message.Message` object. The arguments have
+ the same meaning as for :meth:`sendmail`, except that *msg* is a ``Message``
+ object.
+
+ If *from_addr* is ``None``, ``send_message`` sets its value to the value of
+ the :mailheader:`From` header from *msg*. If *to_addrs* is ``None``,
+ ``send_message`` combines the values (if any) of the :mailheader:`To`,
+ :mailheader:`CC`, and :mailheader:`Bcc` fields from *msg*. Regardless of
+ the values of *from_addr* and *to_addrs*, ``send_message`` deletes any Bcc
+ field from *msg*. It then serializes *msg* using
+ :class:`~email.generator.BytesGenerator` with ``\r\n`` as the *linesep*, and
+ calls :meth:`sendmail` to transmit the resulting message.
+
+ .. versionadded:: 3.2
+
.. method:: SMTP.quit()
@@ -366,5 +392,5 @@
.. note::
In general, you will want to use the :mod:`email` package's features to
- construct an email message, which you can then convert to a string and send
- via :meth:`sendmail`; see :ref:`email-examples`.
+ construct an email message, which you can then send
+ via :meth:`~smtplib.SMTP.send_message`; see :ref:`email-examples`.
diff --git a/Doc/whatsnew/3.2.rst b/Doc/whatsnew/3.2.rst
index 87f9e94..613331f 100644
--- a/Doc/whatsnew/3.2.rst
+++ b/Doc/whatsnew/3.2.rst
@@ -540,6 +540,14 @@
(Contributed by Neil Schemenauer and Nick Coghlan; :issue:`5178`.)
+* The :mod:`smtplib` :class:`~smtplib.SMTP` class now accepts a byte string
+ for the *msg* argument to the :meth:`~smtplib.SMTP.sendmail` method,
+ and a new method, :meth:`~smtplib.SMTP.send_message` accepts a
+ :class:`~email.message.Message` object and can optionally obtain the
+ *from_addr* and *to_addrs* addresses directly from the object.
+
+ (Contributed by R. David Murray, :issue:`10321`.)
+
Multi-threading
===============