SMTPlib inside function, extra tab

Hunter hunterji at gmail.com
Tue Oct 7 09:03:12 EDT 2008


I am writing a script that needs to send some emails.  And I've used
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 = 'automation at mydomain.com'
tolist = ['it at mydomain.com','jhunter at mydomain.com']
msgsubj = "Hello!"
messagebody = "This message was sent with Python's smtplib."


def send_mail(fromaddress,tolist,msgsubj,messagebody):
	import smtplib
	SERVER = "mymailserver.mydomain.com"
	message = """\
	From: %s
	To: %s
	Subject: %s
	%s
	""" % (fromaddress, ", ".join(tolist),msgsubj, messagebody)
	print message
	server = smtplib.SMTP(SERVER)
	server.set_debuglevel(1)
	server.sendmail(fromaddress, tolist, message)
	server.quit()

send_mail(fromaddress, tolist, msgsubj, messagebody)

Output when called from function:
send: 'ehlo twaus-mycomputer.mydomain.local\r\n'
reply: '250-mymailserver.mydomain.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-ENHANCEDSTATUSCODES\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.mydomain.com Hello
[10.10.10.119]

TURN
SIZE
ETRN
PIPELINING
DSN
ENHANCEDSTATUSCODES
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:<automation at mydomain.com> size=159\r\n'
reply: '250 2.1.0 automation at mydomain.com....Sender OK\r\n'
reply: retcode (250); Msg: 2.1.0 automation at mydomain.com....Sender OK
send: 'rcpt TO:<it at mydomain.com>\r\n'
reply: '250 2.1.5 it at mydomain.com \r\n'
reply: retcode (250); Msg: 2.1.5 it at mydomain.com
send: 'rcpt TO:<jhunter at mydomain.com>\r\n'
reply: '250 2.1.5 jhunter at mydomain.com \r\n'
reply: retcode (250); Msg: 2.1.5 jhunter at mydomain.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: automation at mydomain.com\r\n\tTo: it at mydomain.com, j
hunter at mydomain.com\r\n\tSubject: Hello!\r\n\tThis message was sent
with
Python's smtplib.\r\n\t\r\n.\r\n"
reply: '250 2.6.0
<mymailservergz1Lz7c0000fb75 at mymailserver.mydomain.com>
 Queued mail for delivery\r\n'
reply: retcode (250); Msg: 2.6.0
<mymailservergz1Lz7c0000fb75 at mymailserver.
mydomain.com> Queued mail for delivery
data: (250, '2.6.0
<mymailservergz1Lz7c0000fb75 at mymailserver.mydomain.com
> Queued mail for delivery')
send: 'quit\r\n'
reply: '221 2.0.0 mymailserver.mydomain.com Service closing
transmission
channel\r\n'
reply: retcode (221); Msg: 2.0.0 mymailserver.mydomain.com Service
closin
g transmission channel
From: automation at mydomain.com
To: it at mydomain.com, jhunter at mydomain.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.mydomain.local\r\n'
reply: '250-mymailserver.mydomain.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-ENHANCEDSTATUSCODES\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.mydomain.com Hello
[10.10.10.119]

TURN
SIZE
ETRN
PIPELINING
DSN
ENHANCEDSTATUSCODES
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:<automation at mydomain.com> size=154\r\n'
reply: '250 2.1.0 automation at mydomain.com....Sender OK\r\n'
reply: retcode (250); Msg: 2.1.0 automation at mydomain.com....Sender OK
send: 'rcpt TO:<it at mydomain.com>\r\n'
reply: '250 2.1.5 it at mydomain.com \r\n'
reply: retcode (250); Msg: 2.1.5 it at mydomain.com
send: 'rcpt TO:<jhunter at mydomain.com>\r\n'
reply: '250 2.1.5 jhunter at mydomain.com \r\n'
reply: retcode (250); Msg: 2.1.5 jhunter at mydomain.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: automation at mydomain.com\r\nTo: it at mydomain.com, jhunt
er at mydomain.com\r\nSubject: Hello!\r\nThis message was sent with
Python's
 smtplib.\r\n.\r\n"
reply: '250 2.6.0
<mymailservercDoXlFg0000fb76 at mymailserver.mydomain.com>
 Queued mail for delivery\r\n'
reply: retcode (250); Msg: 2.6.0
<mymailservercDoXlFg0000fb76 at mymailserver.
mydomain.com> Queued mail for delivery
data: (250, '2.6.0
<mymailservercDoXlFg0000fb76 at mymailserver.mydomain.com
> Queued mail for delivery')
send: 'quit\r\n'
reply: '221 2.0.0 mymailserver.mydomain.com Service closing
transmission
channel\r\n'
reply: retcode (221); Msg: 2.0.0 mymailserver.mydomain.com Service
closin
g transmission channel



More information about the Python-list mailing list