Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | """SMTP/ESMTP client class. |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 3 | |
| 4 | Author: The Dragon De Monsyne <dragondm@integral.org> |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 5 | ESMTP support, test code and doc fixes added by |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 6 | Eric S. Raymond <esr@thyrsus.com> |
Guido van Rossum | 69a79bc | 1998-07-13 15:18:49 +0000 | [diff] [blame] | 7 | Better RFC 821 compliance (MAIL and RCPT, and CRLF in data) |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 8 | by Carey Evans <c.evans@clear.net.nz>, for picky mail servers. |
| 9 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 10 | (This was modified from the Python 1.5 library HTTP lib.) |
| 11 | |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 12 | This should follow RFC 821 (SMTP) and RFC 1869 (ESMTP). |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 14 | Notes: |
| 15 | |
| 16 | Please remember, when doing ESMTP, that the names of the SMTP service |
| 17 | extensions are NOT the same thing as the option keyords for the RCPT |
| 18 | and MAIL commands! |
| 19 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 20 | Example: |
| 21 | |
| 22 | >>> import smtplib |
| 23 | >>> s=smtplib.SMTP("localhost") |
| 24 | >>> print s.help() |
| 25 | This is Sendmail version 8.8.4 |
| 26 | Topics: |
| 27 | HELO EHLO MAIL RCPT DATA |
| 28 | RSET NOOP QUIT HELP VRFY |
| 29 | EXPN VERB ETRN DSN |
| 30 | For more info use "HELP <topic>". |
| 31 | To report bugs in the implementation send email to |
| 32 | sendmail-bugs@sendmail.org. |
| 33 | For local information send email to Postmaster at your site. |
| 34 | End of HELP info |
| 35 | >>> s.putcmd("vrfy","someone@here") |
| 36 | >>> s.getreply() |
| 37 | (250, "Somebody OverHere <somebody@here.my.org>") |
| 38 | >>> s.quit() |
| 39 | |
| 40 | """ |
| 41 | |
| 42 | import socket |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 43 | import string, re |
| 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 | |
| 47 | SMTP_PORT = 25 |
| 48 | CRLF="\r\n" |
| 49 | |
| 50 | # used for exceptions |
Guido van Rossum | fc40a83 | 1998-01-29 17:26:45 +0000 | [diff] [blame] | 51 | SMTPServerDisconnected="Server not connected" |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 52 | SMTPSenderRefused="Sender address refused" |
| 53 | SMTPRecipientsRefused="All Recipients refused" |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 54 | SMTPDataError="Error transmitting message data" |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 69a79bc | 1998-07-13 15:18:49 +0000 | [diff] [blame] | 56 | def quoteaddr(addr): |
| 57 | """Quote a subset of the email addresses defined by RFC 821. |
| 58 | |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 59 | Should be able to handle anything rfc822.parseaddr can handle.""" |
Guido van Rossum | 69a79bc | 1998-07-13 15:18:49 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 61 | m=None |
Guido van Rossum | 69a79bc | 1998-07-13 15:18:49 +0000 | [diff] [blame] | 62 | try: |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 63 | m=rfc822.parseaddr(addr)[1] |
| 64 | except AttributeError: |
| 65 | pass |
| 66 | if not m: |
| 67 | #something weird here.. punt -ddm |
| 68 | return addr |
| 69 | else: |
| 70 | return "<%s>" % m |
Guido van Rossum | 69a79bc | 1998-07-13 15:18:49 +0000 | [diff] [blame] | 71 | |
| 72 | def quotedata(data): |
| 73 | """Quote data for email. |
| 74 | |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 75 | Double leading '.', and change Unix newline '\n', or Mac '\r' into |
Guido van Rossum | 69a79bc | 1998-07-13 15:18:49 +0000 | [diff] [blame] | 76 | Internet CRLF end-of-line.""" |
| 77 | return re.sub(r'(?m)^\.', '..', |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 78 | re.sub(r'(?:\r\n|\n|\r(?!\n))', CRLF, data)) |
Guido van Rossum | 69a79bc | 1998-07-13 15:18:49 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 80 | class SMTP: |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 81 | """This class manages a connection to an SMTP or ESMTP server. |
| 82 | SMTP Objects: |
| 83 | SMTP objects have the following attributes: |
| 84 | helo_resp |
| 85 | This is the message given by the server in responce to the |
| 86 | most recent HELO command. |
| 87 | |
| 88 | ehlo_resp |
| 89 | This is the message given by the server in responce to the |
| 90 | most recent EHLO command. This is usually multiline. |
| 91 | |
| 92 | does_esmtp |
| 93 | This is a True value _after you do an EHLO command_, if the |
| 94 | server supports ESMTP. |
| 95 | |
| 96 | esmtp_features |
| 97 | This is a dictionary, which, if the server supports ESMTP, |
| 98 | will _after you do an EHLO command_, contain the names of the |
| 99 | SMTP service extentions this server supports, and their |
| 100 | parameters (if any). |
| 101 | Note, all extention names are mapped to lower case in the |
| 102 | dictionary. |
| 103 | |
| 104 | For method docs, see each method's docstrings. In general, there is |
| 105 | a method of the same name to preform each SMTP comand, and there |
| 106 | is a method called 'sendmail' that will do an entiere mail |
| 107 | transaction.""" |
| 108 | |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 109 | debuglevel = 0 |
| 110 | file = None |
| 111 | helo_resp = None |
| 112 | ehlo_resp = None |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 113 | does_esmtp = 0 |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 115 | def __init__(self, host = '', port = 0): |
| 116 | """Initialize a new instance. |
| 117 | |
| 118 | If specified, `host' is the name of the remote host to which |
| 119 | to connect. If specified, `port' specifies the port to which |
| 120 | to connect. By default, smtplib.SMTP_PORT is used. |
| 121 | |
| 122 | """ |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 123 | self.esmtp_features = {} |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 124 | if host: self.connect(host, port) |
| 125 | |
| 126 | def set_debuglevel(self, debuglevel): |
| 127 | """Set the debug output level. |
| 128 | |
| 129 | A non-false value results in debug messages for connection and |
| 130 | for all messages sent to and received from the server. |
| 131 | |
| 132 | """ |
| 133 | self.debuglevel = debuglevel |
| 134 | |
| 135 | def connect(self, host='localhost', port = 0): |
| 136 | """Connect to a host on a given port. |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 137 | |
| 138 | If the hostname ends with a colon (`:') followed by a number, |
Guido van Rossum | 2880f6e | 1998-08-10 20:07:00 +0000 | [diff] [blame] | 139 | and there is no port specified, that suffix will be stripped |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 140 | off and the number interpreted as the port number to use. |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 141 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 142 | Note: This method is automatically invoked by __init__, |
| 143 | if a host is specified during instantiation. |
| 144 | |
| 145 | """ |
| 146 | if not port: |
| 147 | i = string.find(host, ':') |
| 148 | if i >= 0: |
| 149 | host, port = host[:i], host[i+1:] |
| 150 | try: port = string.atoi(port) |
| 151 | except string.atoi_error: |
| 152 | raise socket.error, "nonnumeric port" |
| 153 | if not port: port = SMTP_PORT |
| 154 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 155 | if self.debuglevel > 0: print 'connect:', (host, port) |
| 156 | self.sock.connect(host, port) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 157 | (code,msg)=self.getreply() |
| 158 | if self.debuglevel >0 : print "connect:", msg |
| 159 | return msg |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 160 | |
| 161 | def send(self, str): |
| 162 | """Send `str' to the server.""" |
| 163 | if self.debuglevel > 0: print 'send:', `str` |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 164 | if self.sock: |
Guido van Rossum | 2880f6e | 1998-08-10 20:07:00 +0000 | [diff] [blame] | 165 | try: |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 166 | self.sock.send(str) |
Guido van Rossum | 2880f6e | 1998-08-10 20:07:00 +0000 | [diff] [blame] | 167 | except socket.error: |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 168 | raise SMTPServerDisconnected |
Guido van Rossum | fc40a83 | 1998-01-29 17:26:45 +0000 | [diff] [blame] | 169 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 170 | raise SMTPServerDisconnected |
Guido van Rossum | fc40a83 | 1998-01-29 17:26:45 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 172 | def putcmd(self, cmd, args=""): |
| 173 | """Send a command to the server. |
| 174 | """ |
| 175 | str = '%s %s%s' % (cmd, args, CRLF) |
| 176 | self.send(str) |
| 177 | |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 178 | def getreply(self): |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 179 | """Get a reply from the server. |
| 180 | |
| 181 | Returns a tuple consisting of: |
| 182 | - server response code (e.g. '250', or such, if all goes well) |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 183 | Note: returns -1 if it can't read response code. |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 184 | - server response string corresponding to response code |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 185 | (note : multiline responses converted to a single, |
Guido van Rossum | fc40a83 | 1998-01-29 17:26:45 +0000 | [diff] [blame] | 186 | multiline string) |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 187 | """ |
| 188 | resp=[] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 189 | self.file = self.sock.makefile('rb') |
| 190 | while 1: |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 191 | line = self.file.readline() |
| 192 | if self.debuglevel > 0: print 'reply:', `line` |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 193 | resp.append(string.strip(line[4:])) |
| 194 | code=line[:3] |
| 195 | #check if multiline resp |
| 196 | if line[3:4]!="-": |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 197 | break |
| 198 | try: |
| 199 | errcode = string.atoi(code) |
| 200 | except(ValueError): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 201 | errcode = -1 |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 202 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 203 | errmsg = string.join(resp,"\n") |
| 204 | if self.debuglevel > 0: |
Guido van Rossum | fc40a83 | 1998-01-29 17:26:45 +0000 | [diff] [blame] | 205 | print 'reply: retcode (%s); Msg: %s' % (errcode,errmsg) |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 206 | return errcode, errmsg |
| 207 | |
| 208 | def docmd(self, cmd, args=""): |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 209 | """ Send a command, and return its response code """ |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 210 | |
| 211 | self.putcmd(cmd,args) |
| 212 | (code,msg)=self.getreply() |
| 213 | return code |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 214 | # std smtp commands |
| 215 | |
| 216 | def helo(self, name=''): |
| 217 | """ SMTP 'helo' command. Hostname to send for this command |
| 218 | defaults to the FQDN of the local host """ |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 219 | name=string.strip(name) |
| 220 | if len(name)==0: |
| 221 | name=socket.gethostbyaddr(socket.gethostname())[0] |
| 222 | self.putcmd("helo",name) |
| 223 | (code,msg)=self.getreply() |
| 224 | self.helo_resp=msg |
| 225 | return code |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 227 | def ehlo(self, name=''): |
| 228 | """ SMTP 'ehlo' command. Hostname to send for this command |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 229 | defaults to the FQDN of the local host. """ |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 230 | name=string.strip(name) |
| 231 | if len(name)==0: |
| 232 | name=socket.gethostbyaddr(socket.gethostname())[0] |
| 233 | self.putcmd("ehlo",name) |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 234 | (code,msg)=self.getreply() |
| 235 | # According to RFC1869 some (badly written) |
| 236 | # MTA's will disconnect on an ehlo. Toss an exception if |
| 237 | # that happens -ddm |
| 238 | if code == -1 and len(msg) == 0: |
| 239 | raise SMTPServerDisconnected |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 240 | self.ehlo_resp=msg |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 241 | if code<>250: |
| 242 | return code |
| 243 | self.does_esmtp=1 |
| 244 | #parse the ehlo responce -ddm |
Guido van Rossum | 2880f6e | 1998-08-10 20:07:00 +0000 | [diff] [blame] | 245 | resp=string.split(self.ehlo_resp,'\n') |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 246 | del resp[0] |
Guido van Rossum | 2880f6e | 1998-08-10 20:07:00 +0000 | [diff] [blame] | 247 | for each in resp: |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 248 | m=re.match(r'(?P<feature>[A-Za-z0-9][A-Za-z0-9\-]*)',each) |
| 249 | if m: |
| 250 | feature=string.lower(m.group("feature")) |
| 251 | params=string.strip(m.string[m.end("feature"):]) |
| 252 | self.esmtp_features[feature]=params |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 253 | return code |
| 254 | |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 255 | def has_extn(self, opt): |
| 256 | """Does the server support a given SMTP service extension?""" |
| 257 | return self.esmtp_features.has_key(string.lower(opt)) |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | 18586f4 | 1998-04-03 17:03:13 +0000 | [diff] [blame] | 259 | def help(self, args=''): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 260 | """ SMTP 'help' command. Returns help text from server """ |
Guido van Rossum | 18586f4 | 1998-04-03 17:03:13 +0000 | [diff] [blame] | 261 | self.putcmd("help", args) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 262 | (code,msg)=self.getreply() |
| 263 | return msg |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 264 | |
| 265 | def rset(self): |
| 266 | """ SMTP 'rset' command. Resets session. """ |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 267 | code=self.docmd("rset") |
| 268 | return code |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 269 | |
| 270 | def noop(self): |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 271 | """ SMTP 'noop' command. Doesn't do anything :> """ |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 272 | code=self.docmd("noop") |
| 273 | return code |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 275 | def mail(self,sender,options=[]): |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 276 | """ SMTP 'mail' command. Begins mail xfer session. """ |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 277 | optionlist = '' |
| 278 | if options and self.does_esmtp: |
| 279 | optionlist = string.join(options, ' ') |
| 280 | self.putcmd("mail", "FROM:%s %s" % (quoteaddr(sender) ,optionlist)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 281 | return self.getreply() |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 282 | |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 283 | def rcpt(self,recip,options=[]): |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 284 | """ SMTP 'rcpt' command. Indicates 1 recipient for this mail. """ |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 285 | optionlist = '' |
| 286 | if options and self.does_esmtp: |
| 287 | optionlist = string.join(options, ' ') |
| 288 | self.putcmd("rcpt","TO:%s %s" % (quoteaddr(recip),optionlist)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 289 | return self.getreply() |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 290 | |
| 291 | def data(self,msg): |
| 292 | """ SMTP 'DATA' command. Sends message data to server. |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 293 | Automatically quotes lines beginning with a period per rfc821. """ |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 294 | self.putcmd("data") |
| 295 | (code,repl)=self.getreply() |
| 296 | if self.debuglevel >0 : print "data:", (code,repl) |
| 297 | if code <> 354: |
| 298 | return -1 |
| 299 | else: |
Guido van Rossum | 69a79bc | 1998-07-13 15:18:49 +0000 | [diff] [blame] | 300 | self.send(quotedata(msg)) |
| 301 | self.send("%s.%s" % (CRLF, CRLF)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 302 | (code,msg)=self.getreply() |
| 303 | if self.debuglevel >0 : print "data:", (code,msg) |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 304 | return code |
| 305 | |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 306 | def vrfy(self, address): |
| 307 | return self.verify(address) |
| 308 | |
| 309 | def verify(self, address): |
| 310 | """ SMTP 'verify' command. Checks for address validity. """ |
| 311 | self.putcmd("vrfy", quoteaddr(address)) |
| 312 | return self.getreply() |
| 313 | |
| 314 | def expn(self, address): |
| 315 | """ SMTP 'verify' command. Checks for address validity. """ |
| 316 | self.putcmd("expn", quoteaddr(address)) |
| 317 | return self.getreply() |
| 318 | |
| 319 | |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 320 | #some useful methods |
Jeremy Hylton | 31bb8ce | 1998-08-13 19:57:46 +0000 | [diff] [blame] | 321 | def sendmail(self, from_addr, to_addrs, msg, mail_options=[], |
| 322 | rcpt_options=[]): |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 323 | """ This command performs an entire mail transaction. |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 324 | The arguments are: |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 325 | - from_addr : The address sending this mail. |
| 326 | - to_addrs : a list of addresses to send this mail to |
Jeremy Hylton | 31bb8ce | 1998-08-13 19:57:46 +0000 | [diff] [blame] | 327 | (a string will be treated as a list with 1 address) |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 328 | - msg : the message to send. |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 329 | - mail_options : list of ESMTP options (such as 8bitmime) |
| 330 | for the mail command |
| 331 | - rcpt_options : List of ESMTP options (such as DSN commands) |
| 332 | for all the rcpt commands |
Guido van Rossum | 2880f6e | 1998-08-10 20:07:00 +0000 | [diff] [blame] | 333 | If there has been no previous EHLO or HELO command this session, |
| 334 | this method tries ESMTP EHLO first. If the server does ESMTP, message |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 335 | size and each of the specified options will be passed to it. |
| 336 | If EHLO fails, HELO will be tried and ESMTP options suppressed. |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 337 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 338 | This method will return normally if the mail is accepted for at least |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 339 | one recipient. Otherwise it will throw an exception (either |
| 340 | SMTPSenderRefused, SMTPRecipientsRefused, or SMTPDataError) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 341 | That is, if this method does not throw an exception, then someone |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 342 | should get your mail. If this method does not throw an exception, |
| 343 | it returns a dictionary, with one entry for each recipient that was |
Guido van Rossum | fc40a83 | 1998-01-29 17:26:45 +0000 | [diff] [blame] | 344 | refused. |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 345 | |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 346 | Example: |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 347 | |
| 348 | >>> import smtplib |
| 349 | >>> s=smtplib.SMTP("localhost") |
Guido van Rossum | fc40a83 | 1998-01-29 17:26:45 +0000 | [diff] [blame] | 350 | >>> 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] | 351 | >>> msg = ''' |
| 352 | ... From: Me@my.org |
| 353 | ... Subject: testin'... |
| 354 | ... |
| 355 | ... This is a test ''' |
| 356 | >>> s.sendmail("me@my.org",tolist,msg) |
| 357 | { "three@three.org" : ( 550 ,"User unknown" ) } |
| 358 | >>> s.quit() |
| 359 | |
Guido van Rossum | fc40a83 | 1998-01-29 17:26:45 +0000 | [diff] [blame] | 360 | In the above example, the message was accepted for delivery to |
| 361 | three of the four addresses, and one was rejected, with the error |
| 362 | code 550. If all addresses are accepted, then the method |
| 363 | will return an empty dictionary. |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 364 | """ |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 365 | if not self.helo_resp and not self.ehlo_resp: |
| 366 | if self.ehlo() >= 400: |
| 367 | self.helo() |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 368 | esmtp_opts = [] |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 369 | if self.does_esmtp: |
| 370 | # Hmmm? what's this? -ddm |
| 371 | # self.esmtp_features['7bit']="" |
| 372 | if self.has_extn('size'): |
| 373 | esmtp_opts.append("size=" + `len(msg)`) |
| 374 | for option in mail_options: |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 375 | esmtp_opts.append(option) |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 376 | |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 377 | (code,resp) = self.mail(from_addr, esmtp_opts) |
| 378 | if code <> 250: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 379 | self.rset() |
| 380 | raise SMTPSenderRefused |
| 381 | senderrs={} |
Jeremy Hylton | 31bb8ce | 1998-08-13 19:57:46 +0000 | [diff] [blame] | 382 | if type(to_addrs) == types.StringType: |
| 383 | to_addrs = [to_addrs] |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 384 | for each in to_addrs: |
Guido van Rossum | fcfb632 | 1998-08-04 15:29:54 +0000 | [diff] [blame] | 385 | (code,resp)=self.rcpt(each, rcpt_options) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 386 | if (code <> 250) and (code <> 251): |
Guido van Rossum | fc40a83 | 1998-01-29 17:26:45 +0000 | [diff] [blame] | 387 | senderrs[each]=(code,resp) |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 388 | if len(senderrs)==len(to_addrs): |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 389 | # the server refused all our recipients |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 390 | self.rset() |
| 391 | raise SMTPRecipientsRefused |
| 392 | code=self.data(msg) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 393 | if code <>250 : |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 394 | self.rset() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 395 | raise SMTPDataError |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 396 | #if we got here then somebody got our mail |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 397 | return senderrs |
Guido van Rossum | bbe323e | 1998-01-29 17:24:40 +0000 | [diff] [blame] | 398 | |
| 399 | |
| 400 | def close(self): |
| 401 | """Close the connection to the SMTP server.""" |
| 402 | if self.file: |
| 403 | self.file.close() |
| 404 | self.file = None |
| 405 | if self.sock: |
| 406 | self.sock.close() |
| 407 | self.sock = None |
| 408 | |
| 409 | |
| 410 | def quit(self): |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 411 | """Terminate the SMTP session.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 412 | self.docmd("quit") |
| 413 | self.close() |
Guido van Rossum | 95e6f70 | 1998-06-25 02:15:50 +0000 | [diff] [blame] | 414 | |
| 415 | # Test the sendmail method, which tests most of the others. |
| 416 | # Note: This always sends to localhost. |
| 417 | if __name__ == '__main__': |
| 418 | import sys, rfc822 |
| 419 | |
| 420 | def prompt(prompt): |
| 421 | sys.stdout.write(prompt + ": ") |
| 422 | return string.strip(sys.stdin.readline()) |
| 423 | |
| 424 | fromaddr = prompt("From") |
| 425 | toaddrs = string.splitfields(prompt("To"), ',') |
| 426 | print "Enter message, end with ^D:" |
| 427 | msg = '' |
| 428 | while 1: |
| 429 | line = sys.stdin.readline() |
| 430 | if not line: |
| 431 | break |
| 432 | msg = msg + line |
| 433 | print "Message length is " + `len(msg)` |
| 434 | |
| 435 | server = SMTP('localhost') |
| 436 | server.set_debuglevel(1) |
| 437 | server.sendmail(fromaddr, toaddrs, msg) |
| 438 | server.quit() |