SMTPlib inside function, extra tab

H

Hunter

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 = '(e-mail address removed)'
tolist = ['(e-mail address removed)','(e-mail address removed)']
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:<[email protected]> size=159\r\n'
reply: '250 2.1.0 (e-mail address removed)....Sender OK\r\n'
reply: retcode (250); Msg: 2.1.0 (e-mail address removed)....Sender OK
send: 'rcpt TO:<[email protected]>\r\n'
reply: '250 2.1.5 (e-mail address removed) \r\n'
reply: retcode (250); Msg: 2.1.5 (e-mail address removed)
send: 'rcpt TO:<[email protected]>\r\n'
reply: '250 2.1.5 (e-mail address removed) \r\n'
reply: retcode (250); Msg: 2.1.5 (e-mail address removed)
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: (e-mail address removed)\r\n\tTo: (e-mail address removed), j
(e-mail address removed)\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
<[email protected]>
Queued mail for delivery\r\n'
reply: retcode (250); Msg: 2.6.0
<mymailservergz1Lz7c0000fb75@mymailserver.
mydomain.com> Queued mail for delivery
data: (250, '2.6.0
<[email protected]
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: (e-mail address removed)
To: (e-mail address removed), (e-mail address removed)
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:<[email protected]> size=154\r\n'
reply: '250 2.1.0 (e-mail address removed)....Sender OK\r\n'
reply: retcode (250); Msg: 2.1.0 (e-mail address removed)....Sender OK
send: 'rcpt TO:<[email protected]>\r\n'
reply: '250 2.1.5 (e-mail address removed) \r\n'
reply: retcode (250); Msg: 2.1.5 (e-mail address removed)
send: 'rcpt TO:<[email protected]>\r\n'
reply: '250 2.1.5 (e-mail address removed) \r\n'
reply: retcode (250); Msg: 2.1.5 (e-mail address removed)
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: (e-mail address removed)\r\nTo: (e-mail address removed), jhunt
(e-mail address removed)\r\nSubject: Hello!\r\nThis message was sent with
Python's
smtplib.\r\n.\r\n"
reply: '250 2.6.0
<[email protected]>
Queued mail for delivery\r\n'
reply: retcode (250); Msg: 2.6.0
<mymailservercDoXlFg0000fb76@mymailserver.
mydomain.com> Queued mail for delivery
data: (250, '2.6.0
<[email protected]
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
 
H

Hunter

Thank you Tino. I appreciate the help.

Duh! Anything inside """ """ is preformatted text. I have tabs
inside my preformatted text (without even thinking because it looks
more normal because of the indent). I removed them and voila!

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()

--Joshua
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top