Why Does This Variable Vanish?

Tim Roberts timr at probo.com
Mon May 7 01:24:48 EDT 2001


Ben Ocean <zope at thewebsons.com> wrote:
>>Here's my code:
> >>>
>    import smtplib
>    file = open("outfile.txt", "r")
>    line = file.readline()
>    server = smtplib.SMTP("localhost")
>    server.sendmail("beno at thewebsons.com", "beno at thewebsons.com", line)
>    server.quit()
><<<
>The variable *line* in the 2nd to last line of code doesn't print anything. 
>Now, if I put a *print line* command after *line = file.readline()* it'll 
>print to screen. And if I define *line = "whatever"*  and put it after the 
>*line = file.readline()* statement, in the exact_same_place as the *print 
>line* statement above, then I get an email with *whatever* in the body. So, 
>what in tarnation is going on?? Why doesn't variable *line* persist until 
>the 2nd to the last line of code? This makes no sense to me at all! Your 
>help is appreciated.

You need to read the smtplib code a bit closer.  The third parameter to
smtplib.sendmail has to include all the headers for the message.  You're
not including any.  Does the line you're reading from outfile.txt happen to
contain a colon?

Try:
  server.sendmail("...","...", "Subject: Test\n\n%s" % line )

--
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list