Dear Python Tutors,

I am having problems trying to send an email (using the smtplib module ) to
send an email to myself over my browser using a python script, which is
being called by an html form that I have created. My python cgi script is
located in my cgi-bin directory, which I had made sure that in my httpd
configuration, I had added the python extensions so that it can be run as a
cgi script over my browser. If I run my python script directly on the
commandline, I am able to send an email to myself and receive it.
However, when I try to do the same and run it from my html form, it doesn't
work. I'm sure I'm missing something here, but just can't figure out what.
My suspicions could be that to send an email over the browser requires a
specific format, which I could not figure out.
Below is my cgi script and errors that I get when I run the cgi script.

Any help would be appreciated.

Many thanks,

my script:

import cgi
import string,sys
import smtplib
import cgitb
sys.stderr = sys.stdout

def main():
print "Content-type: text/html\n"
form = cgi.FieldStorage()
if form.has_key("firstname") and form["firstname"].value != "":
result = "<p>Name: " + form["firstname"].value +
"<p>Favorite Color(s): " + form["colors"].value + "<p>Favorite Drinks(s): "
+ str(form.getlist("drinks")) + "<p>Email: " + form["email"].value
return result
print "Please enter your firstname!"

TEXT = main()

FROM = "myemail at gmail.com"
TO = "myemail at gmail.com"
SUBJECT = "Second try"

# Prepare actual message

message = """\
From: %s
To: %s
Subject: %s

""" % (FROM,",".join(TO), SUBJECT, TEXT)

# Send the mail
server2 = smtplib.SMTP("localhost")
server2.sendmail(FROM, TO, message)


Error I get:

/usr/lib/python2.6/cgitb.py:173: DeprecationWarning: BaseException.message
has been deprecated as of Python 2.6 value pydoc.html.repr(getattr(evalue, name))

*<class 'socket.error'>*Python 2.6.6: /usr/bin/python
Fri Apr 20 15:13:06 2012

A problem occurred in a Python script. Here is the sequence of function
calls leading up to the error, in the order they occurred.
/var/www/cgi-bin/action.py in **() 37
38 # Send the mail
39 server2 = smtplib.SMTP("localhost")
40 server2.sendmail(FROM, TO, message)
41 server2.quit()
server2 *undefined*, *smtplib* = <module 'smtplib' from
'/usr/lib/python2.6/smtplib.pyc'>, smtplib.*SMTP* = <class smtplib.SMTP>
/usr/lib/python2.6/smtplib.py in *__init__*(self=<smtplib.SMTP instance>,
host='localhost', port=0, local_hostname=None, timeout=<object object>)
237 self.default_port = SMTP_PORT
238 if host:
239 (code, msg) = self.connect(host, port)
240 if code != 220:
241 raise SMTPConnectError(code, msg)
code *undefined*, msg *undefined*, *self* = <smtplib.SMTP instance>, self.*
connect* = <bound method SMTP.connect of <smtplib.SMTP instance>>, *host* 'localhost', *port* = 0 /usr/lib/python2.6/smtplib.py in
instance>, host='localhost', port%) 293
if not port: port = self.default_port
if self.debuglevel > 0: print>>stderr, 'connect:', (host, port)
295 self.sock = self._get_socket(host, port, self.timeout)
296 (code, msg) = self.getreply()
297 if self.debuglevel > 0: print>>stderr, "connect:", msg
*self* = <smtplib.SMTP instance>, self.sock *undefined*, self.*_get_socket* <bound method SMTP._get_socket of <smtplib.SMTP instance>>, *host* 'localhost', *port* = 25, self.*timeout* = <object object>
/usr/lib/python2.6/smtplib.py in *_get_socket*(self=<smtplib.SMTP
instance>, port='localhost', host%, timeout=<object object>) 271
# and just alter the socket connection bit.
if self.debuglevel > 0: print>>stderr, 'connect:', (host, port)
273 return socket.create_connection((port, host), timeout)
275 def connect(self, host='localhost', port = 0):
*global* *socket* = <module 'socket' from
'/usr/lib/python2.6/socket.pyc'>, socket.*create_connection* = <function
create_connection>, *port* = 'localhost', *host* = 25, *timeout* = <object
object> /usr/lib/python2.6/socket.py in
25), timeout=<object object>) 563 except error, msg:
564 if sock is not None:
565 sock.close()
567 raise error, msg
*global* *error* = <class 'socket.error'>, *msg* = error(13, 'Permission

*<class 'socket.error'>*: [Errno 13] Permission denied
args = (13, 'Permission denied')
errno = 13
filename = None
message = ''
strerror = 'Permission denied'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120420/fe330d51/attachment.html>

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
postedApr 20, '12 at 1:21p
activeApr 20, '12 at 1:21p

1 user in discussion

Kreta06: 1 post



site design / logo © 2022 Grokbase