Barry Warsaw | 4c4bec8 | 1998-12-22 03:02:20 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
Barry Warsaw | a1ae884 | 2000-07-09 21:24:31 +0000 | [diff] [blame] | 3 | '''SMTP/ESMTP client class. |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 5 | This should follow RFC 821 (SMTP) and RFC 1869 (ESMTP). |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 7 | Notes: |
| 8 | |
| 9 | Please remember, when doing ESMTP, that the names of the SMTP service |
Barry Warsaw | 4c4bec8 | 1998-12-22 03:02:20 +0000 | [diff] [blame] | 10 | extensions are NOT the same thing as the option keywords for the RCPT |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 11 | and MAIL commands! |
| 12 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 13 | Example: |
| 14 | |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 15 | >>> import smtplib |
| 16 | >>> s=smtplib.SMTP("localhost") |
| 17 | >>> print s.help() |
| 18 | This is Sendmail version 8.8.4 |
| 19 | Topics: |
| 20 | HELO EHLO MAIL RCPT DATA |
| 21 | RSET NOOP QUIT HELP VRFY |
| 22 | EXPN VERB ETRN DSN |
| 23 | For more info use "HELP <topic>". |
| 24 | To report bugs in the implementation send email to |
| 25 | sendmail-bugs@sendmail.org. |
| 26 | For local information send email to Postmaster at your site. |
| 27 | End of HELP info |
| 28 | >>> s.putcmd("vrfy","someone@here") |
| 29 | >>> s.getreply() |
| 30 | (250, "Somebody OverHere <somebody@here.my.org>") |
| 31 | >>> s.quit() |
Barry Warsaw | a1ae884 | 2000-07-09 21:24:31 +0000 | [diff] [blame] | 32 | ''' |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 34 | # Author: The Dragon De Monsyne <dragondm@integral.org> |
| 35 | # ESMTP support, test code and doc fixes added by |
| 36 | # Eric S. Raymond <esr@thyrsus.com> |
| 37 | # Better RFC 821 compliance (MAIL and RCPT, and CRLF in data) |
| 38 | # by Carey Evans <c.evans@clear.net.nz>, for picky mail servers. |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 39 | # |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 40 | # This was modified from the Python 1.5 library HTTP lib. |
| 41 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 42 | import socket |
Barry Warsaw | 0720177 | 1998-12-22 20:37:36 +0000 | [diff] [blame] | 43 | import re |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 44 | import rfc822 |
Jeremy Hylton | 31bb8ce | 1998-08-13 19:57:46 +0000 | [diff] [blame] | 45 | import types |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 46 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 47 | __all__ = ["SMTPException","SMTPServerDisconnected","SMTPResponseException", |
| 48 | "SMTPSenderRefused","SMTPRecipientsRefused","SMTPDataError", |
| 49 | "SMTPConnectError","SMTPHeloError","quoteaddr","quotedata", |
| 50 | "SMTP"] |
| 51 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 52 | SMTP_PORT = 25 |
| 53 | CRLF="\r\n" |
| 54 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 55 | # Exception classes used by this module. |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 56 | class SMTPException(Exception): |
| 57 | """Base class for all exceptions raised by this module.""" |
| 58 | |
| 59 | class SMTPServerDisconnected(SMTPException): |
| 60 | """Not connected to any SMTP server. |
| 61 | |
| 62 | This exception is raised when the server unexpectedly disconnects, |
| 63 | or when an attempt is made to use the SMTP instance before |
| 64 | connecting it to a server. |
| 65 | """ |
| 66 | |
| 67 | class SMTPResponseException(SMTPException): |
| 68 | """Base class for all exceptions that include an SMTP error code. |
| 69 | |
| 70 | These exceptions are generated in some instances when the SMTP |
| 71 | server returns an error code. The error code is stored in the |
| 72 | `smtp_code' attribute of the error, and the `smtp_error' attribute |
| 73 | is set to the error message. |
| 74 | """ |
| 75 | |
| 76 | def __init__(self, code, msg): |
| 77 | self.smtp_code = code |
| 78 | self.smtp_error = msg |
| 79 | self.args = (code, msg) |
| 80 | |
| 81 | class SMTPSenderRefused(SMTPResponseException): |
| 82 | """Sender address refused. |
| 83 | In addition to the attributes set by on all SMTPResponseException |
Barry Warsaw | d25c1b7 | 1999-11-28 17:11:06 +0000 | [diff] [blame] | 84 | exceptions, this sets `sender' to the string that the SMTP refused. |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 85 | """ |
| 86 | |
| 87 | def __init__(self, code, msg, sender): |
| 88 | self.smtp_code = code |
| 89 | self.smtp_error = msg |
| 90 | self.sender = sender |
| 91 | self.args = (code, msg, sender) |
| 92 | |
Guido van Rossum | 20c9228 | 1999-04-21 16:52:20 +0000 | [diff] [blame] | 93 | class SMTPRecipientsRefused(SMTPException): |
Barry Warsaw | d25c1b7 | 1999-11-28 17:11:06 +0000 | [diff] [blame] | 94 | """All recipient addresses refused. |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 95 | The errors for each recipient are accessible through the attribute |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 96 | 'recipients', which is a dictionary of exactly the same sort as |
| 97 | SMTP.sendmail() returns. |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 98 | """ |
| 99 | |
| 100 | def __init__(self, recipients): |
| 101 | self.recipients = recipients |
| 102 | self.args = ( recipients,) |
| 103 | |
| 104 | |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 105 | class SMTPDataError(SMTPResponseException): |
| 106 | """The SMTP server didn't accept the data.""" |
| 107 | |
| 108 | class SMTPConnectError(SMTPResponseException): |
Barry Warsaw | d25c1b7 | 1999-11-28 17:11:06 +0000 | [diff] [blame] | 109 | """Error during connection establishment.""" |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 110 | |
| 111 | class SMTPHeloError(SMTPResponseException): |
Barry Warsaw | d25c1b7 | 1999-11-28 17:11:06 +0000 | [diff] [blame] | 112 | """The server refused our HELO reply.""" |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 113 | |
Peter Schneider-Kamp | 7bc82bb | 2000-08-10 14:02:23 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | 69a79bc | 1998-07-13 15:18:49 +0000 | [diff] [blame] | 115 | def quoteaddr(addr): |
| 116 | """Quote a subset of the email addresses defined by RFC 821. |
| 117 | |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 118 | Should be able to handle anything rfc822.parseaddr can handle. |
| 119 | """ |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 120 | m=None |
Guido van Rossum | 69a79bc | 1998-07-13 15:18:49 +0000 | [diff] [blame] | 121 | try: |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 122 | m=rfc822.parseaddr(addr)[1] |
| 123 | except AttributeError: |
| 124 | pass |
| 125 | if not m: |
| 126 | #something weird here.. punt -ddm |
| 127 | return addr |
| 128 | else: |
| 129 | return "<%s>" % m |
Guido van Rossum | 69a79bc | 1998-07-13 15:18:49 +0000 | [diff] [blame] | 130 | |
| 131 | def quotedata(data): |
| 132 | """Quote data for email. |
| 133 | |
Barry Warsaw | d25c1b7 | 1999-11-28 17:11:06 +0000 | [diff] [blame] | 134 | Double leading '.', and change Unix newline '\\n', or Mac '\\r' into |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 135 | Internet CRLF end-of-line. |
| 136 | """ |
Guido van Rossum | 69a79bc | 1998-07-13 15:18:49 +0000 | [diff] [blame] | 137 | return re.sub(r'(?m)^\.', '..', |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 138 | re.sub(r'(?:\r\n|\n|\r(?!\n))', CRLF, data)) |
Guido van Rossum | 69a79bc | 1998-07-13 15:18:49 +0000 | [diff] [blame] | 139 | |
Peter Schneider-Kamp | 7bc82bb | 2000-08-10 14:02:23 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 141 | class SMTP: |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 142 | """This class manages a connection to an SMTP or ESMTP server. |
| 143 | SMTP Objects: |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 144 | SMTP objects have the following attributes: |
| 145 | helo_resp |
| 146 | This is the message given by the server in response to the |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 147 | most recent HELO command. |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 149 | ehlo_resp |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 150 | This is the message given by the server in response to the |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 151 | most recent EHLO command. This is usually multiline. |
| 152 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 153 | does_esmtp |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 154 | This is a True value _after you do an EHLO command_, if the |
| 155 | server supports ESMTP. |
| 156 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 157 | esmtp_features |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 158 | This is a dictionary, which, if the server supports ESMTP, |
Barry Warsaw | d25c1b7 | 1999-11-28 17:11:06 +0000 | [diff] [blame] | 159 | will _after you do an EHLO command_, contain the names of the |
| 160 | SMTP service extensions this server supports, and their |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 161 | parameters (if any). |
Barry Warsaw | d25c1b7 | 1999-11-28 17:11:06 +0000 | [diff] [blame] | 162 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 163 | Note, all extension names are mapped to lower case in the |
| 164 | dictionary. |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 165 | |
Barry Warsaw | d25c1b7 | 1999-11-28 17:11:06 +0000 | [diff] [blame] | 166 | See each method's docstrings for details. In general, there is a |
| 167 | method of the same name to perform each SMTP command. There is also a |
| 168 | method called 'sendmail' that will do an entire mail transaction. |
| 169 | """ |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 170 | debuglevel = 0 |
| 171 | file = None |
| 172 | helo_resp = None |
| 173 | ehlo_resp = None |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 174 | does_esmtp = 0 |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 175 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 176 | def __init__(self, host = '', port = 0): |
| 177 | """Initialize a new instance. |
| 178 | |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 179 | If specified, `host' is the name of the remote host to which to |
| 180 | connect. If specified, `port' specifies the port to which to connect. |
Barry Warsaw | d25c1b7 | 1999-11-28 17:11:06 +0000 | [diff] [blame] | 181 | By default, smtplib.SMTP_PORT is used. An SMTPConnectError is raised |
| 182 | if the specified `host' doesn't respond correctly. |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 183 | |
| 184 | """ |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 185 | self.esmtp_features = {} |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 186 | if host: |
| 187 | (code, msg) = self.connect(host, port) |
| 188 | if code != 220: |
| 189 | raise SMTPConnectError(code, msg) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 191 | def set_debuglevel(self, debuglevel): |
| 192 | """Set the debug output level. |
| 193 | |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 194 | A non-false value results in debug messages for connection and for all |
| 195 | messages sent to and received from the server. |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 196 | |
| 197 | """ |
| 198 | self.debuglevel = debuglevel |
| 199 | |
| 200 | def connect(self, host='localhost', port = 0): |
| 201 | """Connect to a host on a given port. |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 202 | |
Barry Warsaw | 4c4bec8 | 1998-12-22 03:02:20 +0000 | [diff] [blame] | 203 | If the hostname ends with a colon (`:') followed by a number, and |
| 204 | there is no port specified, that suffix will be stripped off and the |
| 205 | number interpreted as the port number to use. |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 206 | |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 207 | Note: This method is automatically invoked by __init__, if a host is |
| 208 | specified during instantiation. |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 209 | |
| 210 | """ |
| 211 | if not port: |
Eric S. Raymond | c013f30 | 2001-02-09 05:40:38 +0000 | [diff] [blame] | 212 | i = host.find(':') |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 213 | if i >= 0: |
| 214 | host, port = host[:i], host[i+1:] |
Eric S. Raymond | c013f30 | 2001-02-09 05:40:38 +0000 | [diff] [blame] | 215 | try: port = int(port) |
Eric S. Raymond | 8d87603 | 2001-02-09 10:14:53 +0000 | [diff] [blame] | 216 | except ValueError: |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 217 | raise socket.error, "nonnumeric port" |
| 218 | if not port: port = SMTP_PORT |
| 219 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 220 | if self.debuglevel > 0: print 'connect:', (host, port) |
Barry Warsaw | 17bfef5 | 2000-11-08 22:19:47 +0000 | [diff] [blame] | 221 | try: |
| 222 | self.sock.connect((host, port)) |
| 223 | except socket.error: |
| 224 | self.close() |
| 225 | raise |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 226 | (code,msg)=self.getreply() |
| 227 | if self.debuglevel >0 : print "connect:", msg |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 228 | return (code,msg) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 230 | def send(self, str): |
| 231 | """Send `str' to the server.""" |
| 232 | if self.debuglevel > 0: print 'send:', `str` |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 233 | if self.sock: |
Guido van Rossum | 2880f6e | 1998-08-10 20:07:00 +0000 | [diff] [blame] | 234 | try: |
Barry Warsaw | 5bf94a0 | 2000-09-01 06:40:07 +0000 | [diff] [blame] | 235 | sendptr = 0 |
| 236 | while sendptr < len(str): |
| 237 | sendptr = sendptr + self.sock.send(str[sendptr:]) |
Guido van Rossum | 2880f6e | 1998-08-10 20:07:00 +0000 | [diff] [blame] | 238 | except socket.error: |
Guido van Rossum | 40233ea | 1999-01-15 03:23:55 +0000 | [diff] [blame] | 239 | raise SMTPServerDisconnected('Server not connected') |
Guido van Rossum | fc40a83 | 1998-01-29 17:26:45 +0000 | [diff] [blame] | 240 | else: |
Guido van Rossum | 40233ea | 1999-01-15 03:23:55 +0000 | [diff] [blame] | 241 | raise SMTPServerDisconnected('please run connect() first') |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 243 | def putcmd(self, cmd, args=""): |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 244 | """Send a command to the server.""" |
Guido van Rossum | db23d3d | 1999-06-09 15:13:10 +0000 | [diff] [blame] | 245 | if args == "": |
| 246 | str = '%s%s' % (cmd, CRLF) |
| 247 | else: |
| 248 | str = '%s %s%s' % (cmd, args, CRLF) |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 249 | self.send(str) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 250 | |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 251 | def getreply(self): |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 252 | """Get a reply from the server. |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 253 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 254 | Returns a tuple consisting of: |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 255 | |
| 256 | - server response code (e.g. '250', or such, if all goes well) |
| 257 | Note: returns -1 if it can't read response code. |
| 258 | |
| 259 | - server response string corresponding to response code (multiline |
| 260 | responses are converted to a single, multiline string). |
Guido van Rossum | f123f84 | 1999-03-29 20:33:21 +0000 | [diff] [blame] | 261 | |
| 262 | Raises SMTPServerDisconnected if end-of-file is reached. |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 263 | """ |
| 264 | resp=[] |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 265 | if self.file is None: |
| 266 | self.file = self.sock.makefile('rb') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 267 | while 1: |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 268 | line = self.file.readline() |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 269 | if line == '': |
| 270 | self.close() |
| 271 | raise SMTPServerDisconnected("Connection unexpectedly closed") |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 272 | if self.debuglevel > 0: print 'reply:', `line` |
Eric S. Raymond | c013f30 | 2001-02-09 05:40:38 +0000 | [diff] [blame] | 273 | resp.append(line[4:].strip()) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 274 | code=line[:3] |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 275 | # Check that the error code is syntactically correct. |
| 276 | # Don't attempt to read a continuation line if it is broken. |
| 277 | try: |
Eric S. Raymond | c013f30 | 2001-02-09 05:40:38 +0000 | [diff] [blame] | 278 | errcode = int(code) |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 279 | except ValueError: |
| 280 | errcode = -1 |
| 281 | break |
Guido van Rossum | f123f84 | 1999-03-29 20:33:21 +0000 | [diff] [blame] | 282 | # Check if multiline response. |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 283 | if line[3:4]!="-": |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 284 | break |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 285 | |
Eric S. Raymond | c013f30 | 2001-02-09 05:40:38 +0000 | [diff] [blame] | 286 | errmsg = "\n".join(resp) |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 287 | if self.debuglevel > 0: |
Guido van Rossum | fc40a83 | 1998-01-29 17:26:45 +0000 | [diff] [blame] | 288 | print 'reply: retcode (%s); Msg: %s' % (errcode,errmsg) |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 289 | return errcode, errmsg |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 290 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 291 | def docmd(self, cmd, args=""): |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 292 | """Send a command, and return its response code.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 293 | self.putcmd(cmd,args) |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 294 | return self.getreply() |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 295 | |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 296 | # std smtp commands |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 297 | def helo(self, name=''): |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 298 | """SMTP 'helo' command. |
| 299 | Hostname to send for this command defaults to the FQDN of the local |
| 300 | host. |
| 301 | """ |
Thomas Wouters | caa658d | 2000-08-15 19:30:36 +0000 | [diff] [blame] | 302 | if name: |
| 303 | self.putcmd("helo", name) |
| 304 | else: |
Fred Drake | 0ebc1c6 | 2000-08-16 14:26:22 +0000 | [diff] [blame] | 305 | self.putcmd("helo", socket.getfqdn()) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 306 | (code,msg)=self.getreply() |
| 307 | self.helo_resp=msg |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 308 | return (code,msg) |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 309 | |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 310 | def ehlo(self, name=''): |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 311 | """ SMTP 'ehlo' command. |
| 312 | Hostname to send for this command defaults to the FQDN of the local |
| 313 | host. |
| 314 | """ |
Thomas Wouters | caa658d | 2000-08-15 19:30:36 +0000 | [diff] [blame] | 315 | if name: |
| 316 | self.putcmd("ehlo", name) |
| 317 | else: |
Fred Drake | 0ebc1c6 | 2000-08-16 14:26:22 +0000 | [diff] [blame] | 318 | self.putcmd("ehlo", socket.getfqdn()) |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 319 | (code,msg)=self.getreply() |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 320 | # According to RFC1869 some (badly written) |
| 321 | # MTA's will disconnect on an ehlo. Toss an exception if |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 322 | # that happens -ddm |
| 323 | if code == -1 and len(msg) == 0: |
Guido van Rossum | 40233ea | 1999-01-15 03:23:55 +0000 | [diff] [blame] | 324 | raise SMTPServerDisconnected("Server not connected") |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 325 | self.ehlo_resp=msg |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 326 | if code != 250: |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 327 | return (code,msg) |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 328 | self.does_esmtp=1 |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 329 | #parse the ehlo response -ddm |
Eric S. Raymond | c013f30 | 2001-02-09 05:40:38 +0000 | [diff] [blame] | 330 | resp=self.ehlo_resp.split('\n') |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 331 | del resp[0] |
Guido van Rossum | 2880f6e | 1998-08-10 20:07:00 +0000 | [diff] [blame] | 332 | for each in resp: |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 333 | m=re.match(r'(?P<feature>[A-Za-z0-9][A-Za-z0-9\-]*)',each) |
| 334 | if m: |
Eric S. Raymond | c013f30 | 2001-02-09 05:40:38 +0000 | [diff] [blame] | 335 | feature=m.group("feature").lower() |
| 336 | params=m.string[m.end("feature"):].strip() |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 337 | self.esmtp_features[feature]=params |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 338 | return (code,msg) |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 340 | def has_extn(self, opt): |
| 341 | """Does the server support a given SMTP service extension?""" |
Eric S. Raymond | c013f30 | 2001-02-09 05:40:38 +0000 | [diff] [blame] | 342 | return self.esmtp_features.has_key(opt.lower()) |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 343 | |
Guido van Rossum | 18586f4 | 1998-04-03 17:03:13 +0000 | [diff] [blame] | 344 | def help(self, args=''): |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 345 | """SMTP 'help' command. |
| 346 | Returns help text from server.""" |
Guido van Rossum | 18586f4 | 1998-04-03 17:03:13 +0000 | [diff] [blame] | 347 | self.putcmd("help", args) |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 348 | return self.getreply() |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 349 | |
| 350 | def rset(self): |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 351 | """SMTP 'rset' command -- resets session.""" |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 352 | return self.docmd("rset") |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 353 | |
| 354 | def noop(self): |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 355 | """SMTP 'noop' command -- doesn't do anything :>""" |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 356 | return self.docmd("noop") |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 357 | |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 358 | def mail(self,sender,options=[]): |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 359 | """SMTP 'mail' command -- begins mail xfer session.""" |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 360 | optionlist = '' |
| 361 | if options and self.does_esmtp: |
Eric S. Raymond | c013f30 | 2001-02-09 05:40:38 +0000 | [diff] [blame] | 362 | optionlist = ' ' + ' '.join(options) |
Guido van Rossum | db23d3d | 1999-06-09 15:13:10 +0000 | [diff] [blame] | 363 | self.putcmd("mail", "FROM:%s%s" % (quoteaddr(sender) ,optionlist)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 364 | return self.getreply() |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 365 | |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 366 | def rcpt(self,recip,options=[]): |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 367 | """SMTP 'rcpt' command -- indicates 1 recipient for this mail.""" |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 368 | optionlist = '' |
| 369 | if options and self.does_esmtp: |
Eric S. Raymond | c013f30 | 2001-02-09 05:40:38 +0000 | [diff] [blame] | 370 | optionlist = ' ' + ' '.join(options) |
Guido van Rossum | 348fd06 | 1999-01-14 04:18:46 +0000 | [diff] [blame] | 371 | self.putcmd("rcpt","TO:%s%s" % (quoteaddr(recip),optionlist)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 372 | return self.getreply() |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 373 | |
| 374 | def data(self,msg): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 375 | """SMTP 'DATA' command -- sends message data to server. |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 376 | |
Barry Warsaw | 4c4bec8 | 1998-12-22 03:02:20 +0000 | [diff] [blame] | 377 | Automatically quotes lines beginning with a period per rfc821. |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 378 | Raises SMTPDataError if there is an unexpected reply to the |
| 379 | DATA command; the return value from this method is the final |
| 380 | response code received when the all data is sent. |
Barry Warsaw | 4c4bec8 | 1998-12-22 03:02:20 +0000 | [diff] [blame] | 381 | """ |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 382 | self.putcmd("data") |
| 383 | (code,repl)=self.getreply() |
| 384 | if self.debuglevel >0 : print "data:", (code,repl) |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 385 | if code != 354: |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 386 | raise SMTPDataError(code,repl) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 387 | else: |
Guido van Rossum | 20c9228 | 1999-04-21 16:52:20 +0000 | [diff] [blame] | 388 | q = quotedata(msg) |
| 389 | if q[-2:] != CRLF: |
| 390 | q = q + CRLF |
| 391 | q = q + "." + CRLF |
| 392 | self.send(q) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 393 | (code,msg)=self.getreply() |
| 394 | if self.debuglevel >0 : print "data:", (code,msg) |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 395 | return (code,msg) |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 396 | |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 397 | def verify(self, address): |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 398 | """SMTP 'verify' command -- checks for address validity.""" |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 399 | self.putcmd("vrfy", quoteaddr(address)) |
| 400 | return self.getreply() |
Barry Warsaw | 4c4bec8 | 1998-12-22 03:02:20 +0000 | [diff] [blame] | 401 | # a.k.a. |
| 402 | vrfy=verify |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 403 | |
| 404 | def expn(self, address): |
Barry Warsaw | a7d9bdf | 1998-12-22 03:24:27 +0000 | [diff] [blame] | 405 | """SMTP 'verify' command -- checks for address validity.""" |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 406 | self.putcmd("expn", quoteaddr(address)) |
| 407 | return self.getreply() |
| 408 | |
Barry Warsaw | 4c4bec8 | 1998-12-22 03:02:20 +0000 | [diff] [blame] | 409 | # some useful methods |
Jeremy Hylton | 31bb8ce | 1998-08-13 19:57:46 +0000 | [diff] [blame] | 410 | def sendmail(self, from_addr, to_addrs, msg, mail_options=[], |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 411 | rcpt_options=[]): |
| 412 | """This command performs an entire mail transaction. |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 413 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 414 | The arguments are: |
Barry Warsaw | 4c4bec8 | 1998-12-22 03:02:20 +0000 | [diff] [blame] | 415 | - from_addr : The address sending this mail. |
| 416 | - to_addrs : A list of addresses to send this mail to. A bare |
| 417 | string will be treated as a list with 1 address. |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 418 | - msg : The message to send. |
Barry Warsaw | 4c4bec8 | 1998-12-22 03:02:20 +0000 | [diff] [blame] | 419 | - mail_options : List of ESMTP options (such as 8bitmime) for the |
| 420 | mail command. |
| 421 | - rcpt_options : List of ESMTP options (such as DSN commands) for |
| 422 | all the rcpt commands. |
| 423 | |
| 424 | If there has been no previous EHLO or HELO command this session, this |
| 425 | method tries ESMTP EHLO first. If the server does ESMTP, message size |
| 426 | and each of the specified options will be passed to it. If EHLO |
| 427 | fails, HELO will be tried and ESMTP options suppressed. |
| 428 | |
| 429 | This method will return normally if the mail is accepted for at least |
Barry Warsaw | d25c1b7 | 1999-11-28 17:11:06 +0000 | [diff] [blame] | 430 | one recipient. It returns a dictionary, with one entry for each |
| 431 | recipient that was refused. Each entry contains a tuple of the SMTP |
| 432 | error code and the accompanying error message sent by the server. |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 433 | |
| 434 | This method may raise the following exceptions: |
| 435 | |
| 436 | SMTPHeloError The server didn't reply properly to |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 437 | the helo greeting. |
Barry Warsaw | d25c1b7 | 1999-11-28 17:11:06 +0000 | [diff] [blame] | 438 | SMTPRecipientsRefused The server rejected ALL recipients |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 439 | (no mail was sent). |
| 440 | SMTPSenderRefused The server didn't accept the from_addr. |
| 441 | SMTPDataError The server replied with an unexpected |
| 442 | error code (other than a refusal of |
| 443 | a recipient). |
| 444 | |
| 445 | Note: the connection will be open even after an exception is raised. |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 446 | |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 447 | Example: |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 448 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 449 | >>> import smtplib |
| 450 | >>> s=smtplib.SMTP("localhost") |
Guido van Rossum | fc40a83 | 1998-01-29 17:26:45 +0000 | [diff] [blame] | 451 | >>> tolist=["one@one.org","two@two.org","three@three.org","four@four.org"] |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 452 | >>> msg = ''' |
| 453 | ... From: Me@my.org |
| 454 | ... Subject: testin'... |
| 455 | ... |
| 456 | ... This is a test ''' |
| 457 | >>> s.sendmail("me@my.org",tolist,msg) |
| 458 | { "three@three.org" : ( 550 ,"User unknown" ) } |
| 459 | >>> s.quit() |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 460 | |
Barry Warsaw | 4c4bec8 | 1998-12-22 03:02:20 +0000 | [diff] [blame] | 461 | In the above example, the message was accepted for delivery to three |
| 462 | of the four addresses, and one was rejected, with the error code |
Barry Warsaw | d25c1b7 | 1999-11-28 17:11:06 +0000 | [diff] [blame] | 463 | 550. If all addresses are accepted, then the method will return an |
Barry Warsaw | 4c4bec8 | 1998-12-22 03:02:20 +0000 | [diff] [blame] | 464 | empty dictionary. |
| 465 | |
| 466 | """ |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 467 | if self.helo_resp is None and self.ehlo_resp is None: |
| 468 | if not (200 <= self.ehlo()[0] <= 299): |
| 469 | (code,resp) = self.helo() |
| 470 | if not (200 <= code <= 299): |
| 471 | raise SMTPHeloError(code, resp) |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 472 | esmtp_opts = [] |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 473 | if self.does_esmtp: |
| 474 | # Hmmm? what's this? -ddm |
| 475 | # self.esmtp_features['7bit']="" |
| 476 | if self.has_extn('size'): |
| 477 | esmtp_opts.append("size=" + `len(msg)`) |
| 478 | for option in mail_options: |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 479 | esmtp_opts.append(option) |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 480 | |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 481 | (code,resp) = self.mail(from_addr, esmtp_opts) |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 482 | if code != 250: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 483 | self.rset() |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 484 | raise SMTPSenderRefused(code, resp, from_addr) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 485 | senderrs={} |
Jeremy Hylton | 31bb8ce | 1998-08-13 19:57:46 +0000 | [diff] [blame] | 486 | if type(to_addrs) == types.StringType: |
| 487 | to_addrs = [to_addrs] |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 488 | for each in to_addrs: |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 489 | (code,resp)=self.rcpt(each, rcpt_options) |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 490 | if (code != 250) and (code != 251): |
Guido van Rossum | fc40a83 | 1998-01-29 17:26:45 +0000 | [diff] [blame] | 491 | senderrs[each]=(code,resp) |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 492 | if len(senderrs)==len(to_addrs): |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 493 | # the server refused all our recipients |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 494 | self.rset() |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 495 | raise SMTPRecipientsRefused(senderrs) |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 496 | (code,resp) = self.data(msg) |
| 497 | if code != 250: |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 498 | self.rset() |
Guido van Rossum | 296e143 | 1999-04-07 15:03:39 +0000 | [diff] [blame] | 499 | raise SMTPDataError(code, resp) |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 500 | #if we got here then somebody got our mail |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 501 | return senderrs |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 502 | |
| 503 | |
| 504 | def close(self): |
| 505 | """Close the connection to the SMTP server.""" |
| 506 | if self.file: |
| 507 | self.file.close() |
| 508 | self.file = None |
| 509 | if self.sock: |
| 510 | self.sock.close() |
| 511 | self.sock = None |
| 512 | |
| 513 | |
| 514 | def quit(self): |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 515 | """Terminate the SMTP session.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 516 | self.docmd("quit") |
| 517 | self.close() |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 518 | |
Barry Warsaw | 4c4bec8 | 1998-12-22 03:02:20 +0000 | [diff] [blame] | 519 | |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 520 | # Test the sendmail method, which tests most of the others. |
| 521 | # Note: This always sends to localhost. |
| 522 | if __name__ == '__main__': |
| 523 | import sys, rfc822 |
| 524 | |
| 525 | def prompt(prompt): |
| 526 | sys.stdout.write(prompt + ": ") |
Eric S. Raymond | c013f30 | 2001-02-09 05:40:38 +0000 | [diff] [blame] | 527 | return sys.stdin.readline().strip() |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 528 | |
| 529 | fromaddr = prompt("From") |
Eric S. Raymond | 38151ed | 2001-02-09 07:40:17 +0000 | [diff] [blame] | 530 | toaddrs = prompt("To").split(',') |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 531 | print "Enter message, end with ^D:" |
| 532 | msg = '' |
| 533 | while 1: |
| 534 | line = sys.stdin.readline() |
| 535 | if not line: |
| 536 | break |
| 537 | msg = msg + line |
| 538 | print "Message length is " + `len(msg)` |
| 539 | |
| 540 | server = SMTP('localhost') |
| 541 | server.set_debuglevel(1) |
| 542 | server.sendmail(fromaddr, toaddrs, msg) |
| 543 | server.quit() |