SMTPlib inside function, extra tab

Tino Wildenhain tino at wildenhain.de
Tue Oct 7 09:43:57 EDT 2008


Hunter wrote:
> 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)
   ^^^^^
The tabs are exactly here.

best is to use the mail package to generate mime compliant
emails and use simple templates - which could in the
easiest form just module level constants like:

stdform="""
Hello %(greeting)s,

this automated email is about %(subject)s ... """

and so on and then you use it with

stdform % dict(greeting='Mr Ed',subject='writing emails')

...

HTH
Tino
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3241 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20081007/e4d57192/attachment-0001.bin>


More information about the Python-list mailing list