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