blob: ec8dc9da45a204977ef56df8ffc255510498cecc [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`smtplib` --- SMTP protocol client
2=======================================
3
4.. module:: smtplib
5 :synopsis: SMTP protocol client (requires sockets).
6.. sectionauthor:: Eric S. Raymond <esr@snark.thyrsus.com>
7
8
9.. index::
10 pair: SMTP; protocol
11 single: Simple Mail Transfer Protocol
12
Raymond Hettinger469271d2011-01-27 20:38:46 +000013**Source code:** :source:`Lib/smtplib.py`
14
15--------------
16
Georg Brandl116aa622007-08-15 14:28:22 +000017The :mod:`smtplib` module defines an SMTP client session object that can be used
18to send mail to any Internet machine with an SMTP or ESMTP listener daemon. For
19details of SMTP and ESMTP operation, consult :rfc:`821` (Simple Mail Transfer
20Protocol) and :rfc:`1869` (SMTP Service Extensions).
21
22
Senthil Kumaran3d23fd62011-07-30 10:56:50 +080023.. class:: SMTP(host='', port=0, local_hostname=None[, timeout], source_address=None)
Georg Brandl116aa622007-08-15 14:28:22 +000024
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000025 A :class:`SMTP` instance encapsulates an SMTP connection. It has methods
26 that support a full repertoire of SMTP and ESMTP operations. If the optional
R David Murray021362d2013-06-23 16:05:44 -040027 host and port parameters are given, the SMTP :meth:`connect` method is
28 called with those parameters during initialization. If specified,
29 *local_hostname* is used as the FQDN of the local host in the HELO/EHLO
30 command. Otherwise, the local hostname is found using
31 :func:`socket.getfqdn`. If the :meth:`connect` call returns anything other
32 than a success code, an :exc:`SMTPConnectError` is raised. The optional
33 *timeout* parameter specifies a timeout in seconds for blocking operations
34 like the connection attempt (if not specified, the global default timeout
35 setting will be used). The optional source_address parameter allows to bind
36 to some specific source address in a machine with multiple network
37 interfaces, and/or to some specific source TCP port. It takes a 2-tuple
38 (host, port), for the socket to bind to as its source address before
39 connecting. If omitted (or if host or port are ``''`` and/or 0 respectively)
40 the OS default behavior will be used.
Georg Brandl116aa622007-08-15 14:28:22 +000041
42 For normal use, you should only require the initialization/connect,
Jesus Ceac73f8632012-12-26 16:47:03 +010043 :meth:`sendmail`, and :meth:`~smtplib.quit` methods.
44 An example is included below.
Georg Brandl116aa622007-08-15 14:28:22 +000045
Barry Warsaw1f5c9582011-03-15 15:04:44 -040046 The :class:`SMTP` class supports the :keyword:`with` statement. When used
47 like this, the SMTP ``QUIT`` command is issued automatically when the
48 :keyword:`with` statement exits. E.g.::
49
50 >>> from smtplib import SMTP
51 >>> with SMTP("domain.org") as smtp:
52 ... smtp.noop()
53 ...
54 (250, b'Ok')
55 >>>
56
Antoine Pitrou45456a02011-04-26 18:53:42 +020057 .. versionchanged:: 3.3
Barry Warsaw1f5c9582011-03-15 15:04:44 -040058 Support for the :keyword:`with` statement was added.
59
Senthil Kumaranb351a482011-07-31 09:14:17 +080060 .. versionchanged:: 3.3
61 source_address argument was added.
Georg Brandl116aa622007-08-15 14:28:22 +000062
R David Murray36beb662013-06-23 15:47:50 -040063.. class:: SMTP_SSL(host='', port=0, local_hostname=None, keyfile=None, \
64 certfile=None [, timeout], context=None, \
65 source_address=None)
Georg Brandl116aa622007-08-15 14:28:22 +000066
67 A :class:`SMTP_SSL` instance behaves exactly the same as instances of
68 :class:`SMTP`. :class:`SMTP_SSL` should be used for situations where SSL is
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000069 required from the beginning of the connection and using :meth:`starttls` is
70 not appropriate. If *host* is not specified, the local host is used. If
R David Murray36beb662013-06-23 15:47:50 -040071 *port* is zero, the standard SMTP-over-SSL port (465) is used. The optional
72 arguments *local_hostname* and *source_address* have the same meaning as
R David Murray021362d2013-06-23 16:05:44 -040073 they do in the :class:`SMTP` class. *keyfile* and *certfile* are also
74 optional, and can contain a PEM formatted private key and certificate chain
75 file for the SSL connection. *context* also optional, can contain a
76 SSLContext, and is an alternative to keyfile and certfile; If it is
77 specified both keyfile and certfile must be None. The optional *timeout*
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000078 parameter specifies a timeout in seconds for blocking operations like the
Georg Brandlf78e02b2008-06-10 17:40:04 +000079 connection attempt (if not specified, the global default timeout setting
Senthil Kumaran3d23fd62011-07-30 10:56:50 +080080 will be used). The optional source_address parameter allows to bind to some
81 specific source address in a machine with multiple network interfaces,
82 and/or to some specific source tcp port. It takes a 2-tuple (host, port),
83 for the socket to bind to as its source address before connecting. If
Senthil Kumaranb351a482011-07-31 09:14:17 +080084 omitted (or if host or port are ``''`` and/or 0 respectively) the OS default
Senthil Kumaran3d23fd62011-07-30 10:56:50 +080085 behavior will be used.
Georg Brandl116aa622007-08-15 14:28:22 +000086
Antoine Pitroue0650202011-05-18 18:03:09 +020087 .. versionchanged:: 3.3
88 *context* was added.
89
Senthil Kumaranb351a482011-07-31 09:14:17 +080090 .. versionchanged:: 3.3
91 source_address argument was added.
Georg Brandl116aa622007-08-15 14:28:22 +000092
Christian Heimesa5768f72013-12-02 20:44:17 +010093 .. versionchanged:: 3.4
94 The class now supports hostname check with
95 :attr:`SSLContext.check_hostname` and *Server Name Indicator* (see
96 :data:`~ssl.HAS_SNI`).
Senthil Kumaran3d23fd62011-07-30 10:56:50 +080097
98.. class:: LMTP(host='', port=LMTP_PORT, local_hostname=None, source_address=None)
Georg Brandl116aa622007-08-15 14:28:22 +000099
100 The LMTP protocol, which is very similar to ESMTP, is heavily based on the
Senthil Kumaran3d23fd62011-07-30 10:56:50 +0800101 standard SMTP client. It's common to use Unix sockets for LMTP, so our
102 :meth:`connect` method must support that as well as a regular host:port
Senthil Kumaranb351a482011-07-31 09:14:17 +0800103 server. The optional arguments local_hostname and source_address have the
R David Murray021362d2013-06-23 16:05:44 -0400104 same meaning as they do in the :class:`SMTP` class. To specify a Unix
105 socket, you must use an absolute path for *host*, starting with a '/'.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
R David Murray021362d2013-06-23 16:05:44 -0400107 Authentication is supported, using the regular SMTP mechanism. When using a
108 Unix socket, LMTP generally don't support or require any authentication, but
109 your mileage might vary.
Georg Brandl116aa622007-08-15 14:28:22 +0000110
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112A nice selection of exceptions is defined as well:
113
114
115.. exception:: SMTPException
116
R David Murray8a345962013-04-14 06:46:35 -0400117 Subclass of :exc:`OSError` that is the base exception class for all
Ned Deily7cf5e612013-08-13 01:15:14 -0700118 the other exceptions provided by this module.
Georg Brandl116aa622007-08-15 14:28:22 +0000119
Larry Hastings3732ed22014-03-15 21:13:56 -0700120 .. versionchanged:: 3.4
121 SMTPException became subclass of :exc:`OSError`
122
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124.. exception:: SMTPServerDisconnected
125
126 This exception is raised when the server unexpectedly disconnects, or when an
127 attempt is made to use the :class:`SMTP` instance before connecting it to a
128 server.
129
130
131.. exception:: SMTPResponseException
132
133 Base class for all exceptions that include an SMTP error code. These exceptions
134 are generated in some instances when the SMTP server returns an error code. The
135 error code is stored in the :attr:`smtp_code` attribute of the error, and the
136 :attr:`smtp_error` attribute is set to the error message.
137
138
139.. exception:: SMTPSenderRefused
140
141 Sender address refused. In addition to the attributes set by on all
142 :exc:`SMTPResponseException` exceptions, this sets 'sender' to the string that
143 the SMTP server refused.
144
145
146.. exception:: SMTPRecipientsRefused
147
148 All recipient addresses refused. The errors for each recipient are accessible
149 through the attribute :attr:`recipients`, which is a dictionary of exactly the
150 same sort as :meth:`SMTP.sendmail` returns.
151
152
153.. exception:: SMTPDataError
154
155 The SMTP server refused to accept the message data.
156
157
158.. exception:: SMTPConnectError
159
160 Error occurred during establishment of a connection with the server.
161
162
163.. exception:: SMTPHeloError
164
165 The server refused our ``HELO`` message.
166
167
168.. exception:: SMTPAuthenticationError
169
170 SMTP authentication went wrong. Most probably the server didn't accept the
171 username/password combination provided.
172
173
174.. seealso::
175
176 :rfc:`821` - Simple Mail Transfer Protocol
177 Protocol definition for SMTP. This document covers the model, operating
178 procedure, and protocol details for SMTP.
179
180 :rfc:`1869` - SMTP Service Extensions
181 Definition of the ESMTP extensions for SMTP. This describes a framework for
182 extending SMTP with new commands, supporting dynamic discovery of the commands
183 provided by the server, and defines a few additional commands.
184
185
186.. _smtp-objects:
187
188SMTP Objects
189------------
190
191An :class:`SMTP` instance has the following methods:
192
193
194.. method:: SMTP.set_debuglevel(level)
195
196 Set the debug output level. A true value for *level* results in debug messages
197 for connection and for all messages sent to and received from the server.
198
199
Georg Brandl18244152009-09-02 20:34:52 +0000200.. method:: SMTP.docmd(cmd, args='')
Georg Brandl116aa622007-08-15 14:28:22 +0000201
Georg Brandl18244152009-09-02 20:34:52 +0000202 Send a command *cmd* to the server. The optional argument *args* is simply
Georg Brandl116aa622007-08-15 14:28:22 +0000203 concatenated to the command, separated by a space.
204
205 This returns a 2-tuple composed of a numeric response code and the actual
206 response line (multiline responses are joined into one long line.)
207
208 In normal operation it should not be necessary to call this method explicitly.
209 It is used to implement other methods and may be useful for testing private
210 extensions.
211
212 If the connection to the server is lost while waiting for the reply,
213 :exc:`SMTPServerDisconnected` will be raised.
214
215
R David Murray14ee3cf2013-04-13 14:40:33 -0400216.. method:: SMTP.connect(host='localhost', port=0)
217
218 Connect to a host on a given port. The defaults are to connect to the local
219 host at the standard SMTP port (25). If the hostname ends with a colon (``':'``)
220 followed by a number, that suffix will be stripped off and the number
221 interpreted as the port number to use. This method is automatically invoked by
222 the constructor if a host is specified during instantiation. Returns a
223 2-tuple of the response code and message sent by the server in its
224 connection response.
225
226
Georg Brandl18244152009-09-02 20:34:52 +0000227.. method:: SMTP.helo(name='')
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229 Identify yourself to the SMTP server using ``HELO``. The hostname argument
230 defaults to the fully qualified domain name of the local host.
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000231 The message returned by the server is stored as the :attr:`helo_resp` attribute
232 of the object.
Georg Brandl116aa622007-08-15 14:28:22 +0000233
234 In normal operation it should not be necessary to call this method explicitly.
235 It will be implicitly called by the :meth:`sendmail` when necessary.
236
237
Georg Brandl18244152009-09-02 20:34:52 +0000238.. method:: SMTP.ehlo(name='')
Georg Brandl116aa622007-08-15 14:28:22 +0000239
240 Identify yourself to an ESMTP server using ``EHLO``. The hostname argument
241 defaults to the fully qualified domain name of the local host. Examine the
Georg Brandl48310cd2009-01-03 21:18:54 +0000242 response for ESMTP option and store them for use by :meth:`has_extn`.
243 Also sets several informational attributes: the message returned by
244 the server is stored as the :attr:`ehlo_resp` attribute, :attr:`does_esmtp`
Benjamin Petersonae5360b2008-09-08 23:05:23 +0000245 is set to true or false depending on whether the server supports ESMTP, and
246 :attr:`esmtp_features` will be a dictionary containing the names of the
247 SMTP service extensions this server supports, and their
248 parameters (if any).
Georg Brandl116aa622007-08-15 14:28:22 +0000249
250 Unless you wish to use :meth:`has_extn` before sending mail, it should not be
251 necessary to call this method explicitly. It will be implicitly called by
252 :meth:`sendmail` when necessary.
253
Christian Heimes679db4a2008-01-18 09:56:22 +0000254.. method:: SMTP.ehlo_or_helo_if_needed()
255
256 This method call :meth:`ehlo` and or :meth:`helo` if there has been no
257 previous ``EHLO`` or ``HELO`` command this session. It tries ESMTP ``EHLO``
258 first.
259
Georg Brandl1f01deb2009-01-03 22:47:39 +0000260 :exc:`SMTPHeloError`
Christian Heimes679db4a2008-01-18 09:56:22 +0000261 The server didn't reply properly to the ``HELO`` greeting.
262
Georg Brandl116aa622007-08-15 14:28:22 +0000263.. method:: SMTP.has_extn(name)
264
265 Return :const:`True` if *name* is in the set of SMTP service extensions returned
266 by the server, :const:`False` otherwise. Case is ignored.
267
268
269.. method:: SMTP.verify(address)
270
271 Check the validity of an address on this server using SMTP ``VRFY``. Returns a
272 tuple consisting of code 250 and a full :rfc:`822` address (including human
273 name) if the user address is valid. Otherwise returns an SMTP error code of 400
274 or greater and an error string.
275
276 .. note::
277
278 Many sites disable SMTP ``VRFY`` in order to foil spammers.
279
280
281.. method:: SMTP.login(user, password)
282
283 Log in on an SMTP server that requires authentication. The arguments are the
284 username and the password to authenticate with. If there has been no previous
285 ``EHLO`` or ``HELO`` command this session, this method tries ESMTP ``EHLO``
286 first. This method will return normally if the authentication was successful, or
287 may raise the following exceptions:
288
289 :exc:`SMTPHeloError`
290 The server didn't reply properly to the ``HELO`` greeting.
291
292 :exc:`SMTPAuthenticationError`
293 The server didn't accept the username/password combination.
294
295 :exc:`SMTPException`
296 No suitable authentication method was found.
297
298
Antoine Pitroue0650202011-05-18 18:03:09 +0200299.. method:: SMTP.starttls(keyfile=None, certfile=None, context=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000300
301 Put the SMTP connection in TLS (Transport Layer Security) mode. All SMTP
302 commands that follow will be encrypted. You should then call :meth:`ehlo`
303 again.
304
305 If *keyfile* and *certfile* are provided, these are passed to the :mod:`socket`
306 module's :func:`ssl` function.
307
Antoine Pitroue0650202011-05-18 18:03:09 +0200308 Optional *context* parameter is a :class:`ssl.SSLContext` object; This is an alternative to
309 using a keyfile and a certfile and if specified both *keyfile* and *certfile* should be None.
310
Christian Heimes679db4a2008-01-18 09:56:22 +0000311 If there has been no previous ``EHLO`` or ``HELO`` command this session,
312 this method tries ESMTP ``EHLO`` first.
313
Christian Heimes679db4a2008-01-18 09:56:22 +0000314 :exc:`SMTPHeloError`
315 The server didn't reply properly to the ``HELO`` greeting.
316
317 :exc:`SMTPException`
318 The server does not support the STARTTLS extension.
319
Christian Heimes679db4a2008-01-18 09:56:22 +0000320 :exc:`RuntimeError`
Ezio Melotti0639d5a2009-12-19 23:26:38 +0000321 SSL/TLS support is not available to your Python interpreter.
Christian Heimes679db4a2008-01-18 09:56:22 +0000322
Antoine Pitroue0650202011-05-18 18:03:09 +0200323 .. versionchanged:: 3.3
324 *context* was added.
325
Christian Heimesa5768f72013-12-02 20:44:17 +0100326 .. versionchanged:: 3.4
327 The method now supports hostname check with
328 :attr:`SSLContext.check_hostname` and *Server Name Indicator* (see
329 :data:`~ssl.HAS_SNI`).
330
Georg Brandl116aa622007-08-15 14:28:22 +0000331
Georg Brandl18244152009-09-02 20:34:52 +0000332.. method:: SMTP.sendmail(from_addr, to_addrs, msg, mail_options=[], rcpt_options=[])
Georg Brandl116aa622007-08-15 14:28:22 +0000333
334 Send mail. The required arguments are an :rfc:`822` from-address string, a list
335 of :rfc:`822` to-address strings (a bare string will be treated as a list with 1
336 address), and a message string. The caller may pass a list of ESMTP options
337 (such as ``8bitmime``) to be used in ``MAIL FROM`` commands as *mail_options*.
338 ESMTP options (such as ``DSN`` commands) that should be used with all ``RCPT``
339 commands can be passed as *rcpt_options*. (If you need to use different ESMTP
340 options to different recipients you have to use the low-level methods such as
341 :meth:`mail`, :meth:`rcpt` and :meth:`data` to send the message.)
342
343 .. note::
344
345 The *from_addr* and *to_addrs* parameters are used to construct the message
R. David Murray7dff9e02010-11-08 17:15:13 +0000346 envelope used by the transport agents. ``sendmail`` does not modify the
Georg Brandl116aa622007-08-15 14:28:22 +0000347 message headers in any way.
348
Benjamin Petersona6c4a102011-12-30 23:08:09 -0600349 *msg* may be a string containing characters in the ASCII range, or a byte
R. David Murray7dff9e02010-11-08 17:15:13 +0000350 string. A string is encoded to bytes using the ascii codec, and lone ``\r``
Benjamin Petersona6c4a102011-12-30 23:08:09 -0600351 and ``\n`` characters are converted to ``\r\n`` characters. A byte string is
352 not modified.
R. David Murray7dff9e02010-11-08 17:15:13 +0000353
Georg Brandl116aa622007-08-15 14:28:22 +0000354 If there has been no previous ``EHLO`` or ``HELO`` command this session, this
355 method tries ESMTP ``EHLO`` first. If the server does ESMTP, message size and
356 each of the specified options will be passed to it (if the option is in the
357 feature set the server advertises). If ``EHLO`` fails, ``HELO`` will be tried
358 and ESMTP options suppressed.
359
360 This method will return normally if the mail is accepted for at least one
Georg Brandl7cb13192010-08-03 12:06:29 +0000361 recipient. Otherwise it will raise an exception. That is, if this method does
362 not raise an exception, then someone should get your mail. If this method does
363 not raise an exception, it returns a dictionary, with one entry for each
Georg Brandl116aa622007-08-15 14:28:22 +0000364 recipient that was refused. Each entry contains a tuple of the SMTP error code
365 and the accompanying error message sent by the server.
366
367 This method may raise the following exceptions:
368
369 :exc:`SMTPRecipientsRefused`
370 All recipients were refused. Nobody got the mail. The :attr:`recipients`
371 attribute of the exception object is a dictionary with information about the
372 refused recipients (like the one returned when at least one recipient was
373 accepted).
374
375 :exc:`SMTPHeloError`
376 The server didn't reply properly to the ``HELO`` greeting.
377
378 :exc:`SMTPSenderRefused`
379 The server didn't accept the *from_addr*.
380
381 :exc:`SMTPDataError`
382 The server replied with an unexpected error code (other than a refusal of a
383 recipient).
384
385 Unless otherwise noted, the connection will be open even after an exception is
386 raised.
387
Georg Brandl61063cc2012-06-24 22:48:30 +0200388 .. versionchanged:: 3.2
389 *msg* may be a byte string.
R. David Murray7dff9e02010-11-08 17:15:13 +0000390
391
R David Murrayac4e5ab2011-07-02 21:03:19 -0400392.. method:: SMTP.send_message(msg, from_addr=None, to_addrs=None, \
393 mail_options=[], rcpt_options=[])
R. David Murray7dff9e02010-11-08 17:15:13 +0000394
395 This is a convenience method for calling :meth:`sendmail` with the message
396 represented by an :class:`email.message.Message` object. The arguments have
397 the same meaning as for :meth:`sendmail`, except that *msg* is a ``Message``
398 object.
399
R David Murrayac4e5ab2011-07-02 21:03:19 -0400400 If *from_addr* is ``None`` or *to_addrs* is ``None``, ``send_message`` fills
401 those arguments with addresses extracted from the headers of *msg* as
402 specified in :rfc:`2822`\: *from_addr* is set to the :mailheader:`Sender`
403 field if it is present, and otherwise to the :mailheader:`From` field.
404 *to_adresses* combines the values (if any) of the :mailheader:`To`,
405 :mailheader:`Cc`, and :mailheader:`Bcc` fields from *msg*. If exactly one
406 set of :mailheader:`Resent-*` headers appear in the message, the regular
407 headers are ignored and the :mailheader:`Resent-*` headers are used instead.
408 If the message contains more than one set of :mailheader:`Resent-*` headers,
409 a :exc:`ValueError` is raised, since there is no way to unambiguously detect
410 the most recent set of :mailheader:`Resent-` headers.
411
412 ``send_message`` serializes *msg* using
R. David Murray7dff9e02010-11-08 17:15:13 +0000413 :class:`~email.generator.BytesGenerator` with ``\r\n`` as the *linesep*, and
R David Murrayac4e5ab2011-07-02 21:03:19 -0400414 calls :meth:`sendmail` to transmit the resulting message. Regardless of the
415 values of *from_addr* and *to_addrs*, ``send_message`` does not transmit any
416 :mailheader:`Bcc` or :mailheader:`Resent-Bcc` headers that may appear
417 in *msg*.
R. David Murray7dff9e02010-11-08 17:15:13 +0000418
419 .. versionadded:: 3.2
420
Georg Brandl116aa622007-08-15 14:28:22 +0000421
422.. method:: SMTP.quit()
423
Christian Heimesba4af492008-03-28 00:55:15 +0000424 Terminate the SMTP session and close the connection. Return the result of
425 the SMTP ``QUIT`` command.
426
Georg Brandl116aa622007-08-15 14:28:22 +0000427
428Low-level methods corresponding to the standard SMTP/ESMTP commands ``HELP``,
429``RSET``, ``NOOP``, ``MAIL``, ``RCPT``, and ``DATA`` are also supported.
430Normally these do not need to be called directly, so they are not documented
431here. For details, consult the module code.
432
433
434.. _smtp-example:
435
436SMTP Example
437------------
438
439This example prompts the user for addresses needed in the message envelope ('To'
440and 'From' addresses), and the message to be delivered. Note that the headers
441to be included with the message must be included in the message as entered; this
442example doesn't do any processing of the :rfc:`822` headers. In particular, the
443'To' and 'From' addresses must be included in the message headers explicitly. ::
444
445 import smtplib
446
Georg Brandl116aa622007-08-15 14:28:22 +0000447 def prompt(prompt):
Georg Brandl8d5c3922007-12-02 22:48:17 +0000448 return input(prompt).strip()
Georg Brandl116aa622007-08-15 14:28:22 +0000449
450 fromaddr = prompt("From: ")
451 toaddrs = prompt("To: ").split()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000452 print("Enter message, end with ^D (Unix) or ^Z (Windows):")
Georg Brandl116aa622007-08-15 14:28:22 +0000453
454 # Add the From: and To: headers at the start!
455 msg = ("From: %s\r\nTo: %s\r\n\r\n"
456 % (fromaddr, ", ".join(toaddrs)))
Collin Winter46334482007-09-10 00:49:57 +0000457 while True:
Georg Brandl116aa622007-08-15 14:28:22 +0000458 try:
Georg Brandl8d5c3922007-12-02 22:48:17 +0000459 line = input()
Georg Brandl116aa622007-08-15 14:28:22 +0000460 except EOFError:
461 break
462 if not line:
463 break
464 msg = msg + line
465
Georg Brandl6911e3c2007-09-04 07:15:32 +0000466 print("Message length is", len(msg))
Georg Brandl116aa622007-08-15 14:28:22 +0000467
468 server = smtplib.SMTP('localhost')
469 server.set_debuglevel(1)
470 server.sendmail(fromaddr, toaddrs, msg)
471 server.quit()
472
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000473.. note::
474
475 In general, you will want to use the :mod:`email` package's features to
R. David Murray7dff9e02010-11-08 17:15:13 +0000476 construct an email message, which you can then send
477 via :meth:`~smtplib.SMTP.send_message`; see :ref:`email-examples`.