473,654 Members | 3,072 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*********@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
Oct 7 '08 #1
1 2384
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(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()

--Joshua
Oct 9 '08 #2

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

Similar topics

9
10953
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 working piece of code that emails jpg attachments but does not work with any other types of attachments. Could someone tell me how to modify this code to send other types of attachments like the one's stated above(especially PDF's)? Also how do I...
1
3388
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 is an important message." server = smtplib.SMTP('localhost') server.sendmail(frmadr, toadr, msg)
0
3038
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 of chars from SSLFakeFile used during TLS instead of timing out the client connection. I am working from my own hacked version of smtplib, but can demonstrate the problem using the standard module (Python 2.3.4). Can anyone suggest a...
3
4385
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: >>> import smtplib >>> server = smtplib.SMTP('localhost') >>> server.sendmail('soothsayer@example.org', 'jcaesar@example.org', """To: jcaesar@example.org
3
8206
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 spammer connects to us every few seconds, this quickly leads to a problem :-) I need to set a timelimit for the operation of smtplib.sendmail. It has to be thread based, because pymilter uses libmilter which is thread based. There are some...
7
2357
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 and running this little script: # list.txt is a text file container an email per line fp=open('list.txt','r') list=fp.readlines()
0
1864
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 login or do the sendmail): I have not had this problem with other mail servers (but only tried two). A solution for gmail seems to be to replace the server.quit() with server.close(). The difference between the two commands is quit() sends a...
3
1966
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 connection. The program works properly, and I've created an 'exe' of it, using py2exe. It was distrubuted to my user base a couple of weeks ago and seems to be working well. None of the users have Python installed on their machines, thus the need...
5
4680
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: Traceback (most recent call last):
0
8372
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7304
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1592
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.