473,503 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SMTPlib inside function, extra tab

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 = 'a*********@mydomain.com'
tolist = ['i*@mydomain.com','j******@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:<au********@mydomain.comsize=159\r\n'
reply: '250 2.1.0 au********@mydomain.com....Sender OK\r\n'
reply: retcode (250); Msg: 2.1.0 au********@mydomain.com....Sender 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*****@mydomain.com>\r\n'
reply: '250 2.1.5 jh*****@mydomain.com \r\n'
reply: retcode (250); Msg: 2.1.5 jh*****@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: au********@mydomain.com\r\n\tTo: it@mydomain.com, j
hu****@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
<my*************************@mymailserver.mydomain .com>
Queued mail for delivery\r\n'
reply: retcode (250); Msg: 2.6.0
<mymailservergz1Lz7c0000fb75@mymailserver.
mydomain.comQueued mail for delivery
data: (250, '2.6.0
<my*************************@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: au********@mydomain.com
To: it@mydomain.com, jh*****@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:<au********@mydomain.comsize=154\r\n'
reply: '250 2.1.0 au********@mydomain.com....Sender OK\r\n'
reply: retcode (250); Msg: 2.1.0 au********@mydomain.com....Sender 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*****@mydomain.com>\r\n'
reply: '250 2.1.5 jh*****@mydomain.com \r\n'
reply: retcode (250); Msg: 2.1.5 jh*****@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: au********@mydomain.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*************************@mymailserver.mydomain .com>
Queued mail for delivery\r\n'
reply: retcode (250); Msg: 2.6.0
<mymailservercDoXlFg0000fb76@mymailserver.
mydomain.comQueued mail for delivery
data: (250, '2.6.0
<my*************************@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
Oct 7 '08 #1
1 2372
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
Oct 9 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
10947
by: Bill | last post by:
I am trying to have the capability to email attachments. Specifically I want to be able to email a specific attachment that I name that may be a PDF document, text doc, etc. I already have a...
1
3375
by: Jeff Sandys | last post by:
Can you include a subject with sendmail using smtplib? When I do this (names changed to protect the innocent): import smtplib toadr = "me@myisp.com" frmadr = "myhost@myhost.com" msg = "this...
0
3030
by: Tim Williams | last post by:
I have a working SMTP client that I need to add TLS capability to, I absolutely need the client to timeout within a specified time, but when I use the sock.timeout() line it freezes the reading...
3
4359
by: Van_Gogh | last post by:
Hi, I am learning how to use the smtplib module, but am having some very early problems, maybe because I don't understand it. So, am I correct that by following the example in the Python: >>>...
3
8196
by: Stuart D. Gathman | last post by:
I am doing SMTP callbacks in a Python milter (http://pymilter.sourceforge.net) using the smtplib module. For some spammer MXes, it takes days (!) before smtplib.sendmail will return. Since the...
7
2345
by: 3KWA | last post by:
Hi all, I tried to send a small mailing list using python today (2036 emails). For that purpose I looked in the doc for some code examples (I had never done it before). And I ended up writing...
0
1850
by: Roger | last post by:
I am having a problem sending email through smtp.gmail.com using smtplib. Everything works and the mail is sent and received, except quit. The following shows the problem (without bothering to...
3
1959
by: Kevin | last post by:
Hi everyone, I'm running Python 2.5.1 on an XP-Pro platform, with all the updates (SP2, etc) installed. I have a program (send_file.py) that sends a file to a service provider, using an ftp...
5
4664
by: zxo102 | last post by:
Hi, I am trying to use python module smtplib to send my email out on window xp (localhost). import smtplib server = smtplib.SMTP('localhost') but I got the error information as follows: ...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.