smtplib in the past and it is pretty easy. But I thought, gee it
would be easier if I could just call it as a function, passing the
from, to, subject, and message text. So I wrote it up as a function
and it sort of works, but I get a weird error. When it runs it
inserts a "\t" tab character before each item during the send portion
(which I can see when I turn on debug). The end result is that I
don't get any body or subject in my emails. It works fine when I copy
the inside of the function and run it directly. It isn't a
dealbreaker, I can certainly just call it direct, but from a learning
Python perspective I'm wondering if anyone knows what exactly is
happening. I'm more interested in the why this is happening than a
solution (though that would be great too). Oh and if you could
explain it to me, with no CS background, that would be even better.
I am working on Windows Vista with Python 2.5.2 (activestate).
Thanks --Joshua
Snip of script (more or less a copy/paste from effbot):
fromaddress = 'a*********@myd omain.com'
tolist = ['i*@mydomain.co m','j******@myd omain.com']
msgsubj = "Hello!"
messagebody = "This message was sent with Python's smtplib."
def send_mail(froma ddress,tolist,m sgsubj,messageb ody):
import smtplib
SERVER = "mymailserver.m ydomain.com"
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (fromaddress, ", ".join(tolist), msgsubj, messagebody)
print message
server = smtplib.SMTP(SE RVER)
server.set_debu glevel(1)
server.sendmail (fromaddress, tolist, message)
server.quit()
send_mail(froma ddress, tolist, msgsubj, messagebody)
Output when called from function:
send: 'ehlo twaus-mycomputer.mydo main.local\r\n'
reply: '250-mymailserver.my domain.com Hello [10.10.10.119]\r\n'
reply: '250-TURN\r\n'
reply: '250-SIZE\r\n'
reply: '250-ETRN\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-DSN\r\n'
reply: '250-ENHANCEDSTATUSC ODES\r\n'
reply: '250-8bitmime\r\n'
reply: '250-BINARYMIME\r\n'
reply: '250-CHUNKING\r\n'
reply: '250-VRFY\r\n'
reply: '250-X-EXPS GSSAPI NTLM LOGIN\r\n'
reply: '250-X-EXPS=LOGIN\r\n'
reply: '250-AUTH GSSAPI NTLM LOGIN\r\n'
reply: '250-AUTH=LOGIN\r\n'
reply: '250-X-LINK2STATE\r\n'
reply: '250-XEXCH50\r\n'
reply: '250 OK\r\n'
reply: retcode (250); Msg: mymailserver.my domain.com Hello
[10.10.10.119]
TURN
SIZE
ETRN
PIPELINING
DSN
ENHANCEDSTATUSC ODES
8bitmime
BINARYMIME
CHUNKING
VRFY
X-EXPS GSSAPI NTLM LOGIN
X-EXPS=LOGIN
AUTH GSSAPI NTLM LOGIN
AUTH=LOGIN
X-LINK2STATE
XEXCH50
OK
send: 'mail FROM:<au******* *@mydomain.coms ize=159\r\n'
reply: '250 2.1.0 au********@mydo main.com....Sen der OK\r\n'
reply: retcode (250); Msg: 2.1.0 au********@mydo main.com....Sen der OK
send: 'rcpt TO:<it@mydomain .com>\r\n'
reply: '250 2.1.5 it@mydomain.com \r\n'
reply: retcode (250); Msg: 2.1.5 it@mydomain.com
send: 'rcpt TO:<jh*****@myd omain.com>\r\n'
reply: '250 2.1.5 jh*****@mydomai n.com \r\n'
reply: retcode (250); Msg: 2.1.5 jh*****@mydomai n.com
send: 'data\r\n'
reply: '354 Start mail input; end with <CRLF>.<CRLF>\r \n'
reply: retcode (354); Msg: Start mail input; end with <CRLF>.<CRLF>
data: (354, 'Start mail input; end with <CRLF>.<CRLF> ')
send: "\tFrom: au********@mydo main.com\r\n\tTo: it@mydomain.com, j
hu****@mydomain .com\r\n\tSubject: Hello!\r\n\tThi s message was sent
with
Python's smtplib.\r\n\t\ r\n.\r\n"
reply: '250 2.6.0
<my************ *************@m ymailserver.myd omain.com>
Queued mail for delivery\r\n'
reply: retcode (250); Msg: 2.6.0
<mymailservergz 1Lz7c0000fb75@m ymailserver.
mydomain.comQue ued mail for delivery
data: (250, '2.6.0
<my************ *************@m ymailserver.myd omain.com
Queued mail for delivery')send: 'quit\r\n'
reply: '221 2.0.0 mymailserver.my domain.com Service closing
transmission
channel\r\n'
reply: retcode (221); Msg: 2.0.0 mymailserver.my domain.com Service
closin
g transmission channel
From: au********@mydo main.com
To: it@mydomain.com, jh*****@mydomai n.com
Subject: Hello!
This message was sent with Python's smtplib.
Output if you just run the internal part of the function directly:
send: 'ehlo twaus-mycomputer.mydo main.local\r\n'
reply: '250-mymailserver.my domain.com Hello [10.10.10.119]\r\n'
reply: '250-TURN\r\n'
reply: '250-SIZE\r\n'
reply: '250-ETRN\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-DSN\r\n'
reply: '250-ENHANCEDSTATUSC ODES\r\n'
reply: '250-8bitmime\r\n'
reply: '250-BINARYMIME\r\n'
reply: '250-CHUNKING\r\n'
reply: '250-VRFY\r\n'
reply: '250-X-EXPS GSSAPI NTLM LOGIN\r\n'
reply: '250-X-EXPS=LOGIN\r\n'
reply: '250-AUTH GSSAPI NTLM LOGIN\r\n'
reply: '250-AUTH=LOGIN\r\n'
reply: '250-X-LINK2STATE\r\n'
reply: '250-XEXCH50\r\n'
reply: '250 OK\r\n'
reply: retcode (250); Msg: mymailserver.my domain.com Hello
[10.10.10.119]
TURN
SIZE
ETRN
PIPELINING
DSN
ENHANCEDSTATUSC ODES
8bitmime
BINARYMIME
CHUNKING
VRFY
X-EXPS GSSAPI NTLM LOGIN
X-EXPS=LOGIN
AUTH GSSAPI NTLM LOGIN
AUTH=LOGIN
X-LINK2STATE
XEXCH50
OK
send: 'mail FROM:<au******* *@mydomain.coms ize=154\r\n'
reply: '250 2.1.0 au********@mydo main.com....Sen der OK\r\n'
reply: retcode (250); Msg: 2.1.0 au********@mydo main.com....Sen der OK
send: 'rcpt TO:<it@mydomain .com>\r\n'
reply: '250 2.1.5 it@mydomain.com \r\n'
reply: retcode (250); Msg: 2.1.5 it@mydomain.com
send: 'rcpt TO:<jh*****@myd omain.com>\r\n'
reply: '250 2.1.5 jh*****@mydomai n.com \r\n'
reply: retcode (250); Msg: 2.1.5 jh*****@mydomai n.com
send: 'data\r\n'
reply: '354 Start mail input; end with <CRLF>.<CRLF>\r \n'
reply: retcode (354); Msg: Start mail input; end with <CRLF>.<CRLF>
data: (354, 'Start mail input; end with <CRLF>.<CRLF> ')
send: "From: au********@mydo main.com\r\nTo: it@mydomain.com, jhunt
er@mydomain.com\r\nSubject: Hello!\r\nThis message was sent with
Python's
smtplib.\r\n.\r \n"
reply: '250 2.6.0
<my************ *************@m ymailserver.myd omain.com>
Queued mail for delivery\r\n'
reply: retcode (250); Msg: 2.6.0
<mymailservercD oXlFg0000fb76@m ymailserver.
mydomain.comQue ued mail for delivery
data: (250, '2.6.0
<my************ *************@m ymailserver.myd omain.com
Queued mail for delivery')send: 'quit\r\n'
reply: '221 2.0.0 mymailserver.my domain.com Service closing
transmission
channel\r\n'
reply: retcode (221); Msg: 2.0.0 mymailserver.my domain.com Service
closin
g transmission channel