Where O Where Could My VARIABLE Be?

Steve Holden sholden at holdenweb.com
Fri May 4 17:32:04 CEST 2001


[posted and mailed]

"Ben Ocean" <zope at thewebsons.com> wrote in message
news:mailman.988987448.30084.python-list at python.org...
> Hi;
> Why does this send me a blank email when the file has information??
>  >>>
> #!/usr/bin/python
>
> import cgi, os
> import string
> import smtplib
>
> print "Content-type: text/html\n\n"
> file = open("outfile.txt", "r")
> line = file.readline()
> file.close()
> server = smtplib.SMTP("localhost")
> server.sendmail("beno at thewebsons.com", "beno at thewebsons.com", line)
> server.quit()
> <<<
> ...when *this* will send me an email with the word *line*...
>  >>>
> #!/usr/bin/python
>
> import cgi, os
> import string
> import smtplib
>
> print "Content-type: text/html\n\n"
> file = open("outfile.txt", "r")
> line = file.readline()
> file.close()
> server = smtplib.SMTP("localhost")
> server.sendmail("beno at thewebsons.com", "beno at thewebsons.com", "line")  #
> note the only difference is here
> server.quit()
> <<<
> ...and *this* will print the entire contents of the file to the screen
(and
> send me a blank email)???
>  >>>
> #!/usr/bin/python
>
> import cgi, os
> import string
> import smtplib
>
> print "Content-type: text/html\n\n"
> file = open("outfile.txt", "r")
> line = file.readline()
> file.close()
> print line #### note this is the only additional line
> server = smtplib.SMTP("localhost")
> server.sendmail("beno at thewebsons.com", "beno at thewebsons.com", line)
> server.quit()
> <<<
> PLEASE point out what small, stupid error I'm not seeing!
> TIA,
> BenO

Ben:

Without seeing the contents of the file, it's difficult to say. Perhaps the
first line of the file is blank?

However, in general you should make sure that the message passed to sendmail
is formatted as an email message, with headers followed by a blank line
followed by the message text. The simplest way to do this would be to put a
"From:" and a "To:" header (if you are getting these, your mail server is
being *extremely* kind to you, and you should not rely on it).

Try this (untested):

#!/usr/bin/python

import cgi, os
import string
import smtplib

print "Content-type: text/html\n\n"
file = open("outfile.txt", "r")
message = file.read() # Note: reads whole file
message = """From: beno at thewebsons.com
To: beno at thewebsons.com

""" + message    # Adds a minimal set of headers
file.close()
server = smtplib.SMTP("localhost")
server.sendmail("beno at thewebsons.com", "beno at thewebsons.com", message)
server.quit()
print """
<HTML>
<HEAD></HEAD>
<BODY><H1>Mail Sent</H1></BODY>
</HTML>"""

I added the bit at the bottom because the first week in May is
be-kind-to-browsers week. You can drop the last print statement if this page
isn't being triggered by a browser. Or for any other reason you feel like.

Let me know if this works.

regards
 Steve





More information about the Python-list mailing list