Jump to content

[Solved] Error 500 When I Try To Send A Mail


tosites

Recommended Posts

I'm trying to send a contact form by mail using a python script but I'm getting a server error 500.

Can you help me please saying what is wrong and how to configure correctly?

When I execute the script at my own computer it works ok with the Gmail but when I execute at server nor Gmail or my own mail server works to send the email.

PS: With Gmail it is necessary to use server.starttls().

Thanks for your help.

 

I'm posting the script below:

 

 

 

#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
# Import modules for email handling
import smtplib
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
name = form.getvalue('name')
email = form.getvalue('email')
subject = form.getvalue('subject')
message = form.getvalue('message')
# Send email
receivers = ['email@gmail.com']
text = "From: me\n" + "To: Contact <email@gmail.com>\n" + "Subject: " + subject + "\nFrom: " + name + " <" + email + ">\n" + message + "\n"
try:
server = smtplib.SMTP(localhost) #'smtp.gmail.com', 587)
#server.starttls()
server.login("contact@server.heliohost.org", "password")
server.sendmail("contact@server.heliohost.org", receivers, text) #"email@gmail.com", receivers, text)
server.quit()
except:
pass

 

 

Link to comment
Share on other sites

Here is the error your script is encountering (censoring the email address):

# ./send.py
Traceback (most recent call last):
  File "./send.py", line 21, in <module>
    text = "From: me\n" + "To: Contact <email@gmail.com>\n" + "Subject: " + subject + "\nFrom: " + name + " <" + email + ">\n" + message + "\n"
TypeError: cannot concatenate 'str' and 'NoneType' objects
Which, upon closer inspection seems to be because those variable aren't being passed in from the form if it's run on the command line...

 

Adding the lines

name = "Krydos"
email = "email@gmail.com"
subject = "Test email"
message = "Does this work?"
makes it execute without an error though since those variables now have values. Did you get the email?
Link to comment
Share on other sites

I can send the email when I run the script at my own computer using the Gmail account, but not when I run at server.

Of course, I could not test with the email server from the helionet.

I have the same site running over Apache at my Mac and the email is sent but the uploaded version give an error 500.

Of course, the email accounts used at this example is not the real ones at my script.

Thanks for your attention.

Link to comment
Share on other sites

The value of variables came from a form at my site. That is not being my problem but an error 500 (internal error) on server.

 

this is the result:

 

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head>

<title>500 Internal Server Error</title>

</head><body>

<h1>Internal Server Error</h1>

<p>The server encountered an internal error or

misconfiguration and was unable to complete

your request.</p>

<p>Please contact the server administrator at

webmaster@server to inform them of the time this error occurred,

and the actions you performed just before this error.</p>

<p>More information about this error may be available

in the server error log.</p>

<p>Additionally, a 500 Internal Server Error

error was encountered while trying to use an ErrorDocument to handle the request.</p>

</body></html>

 

Link to comment
Share on other sites

Adding the lines

name = "Krydos"
email = "email@gmail.com"
subject = "Test email"
message = "Does this work?"
makes it execute without an error though since those variables now have values. Did you get the email?

 

Link to comment
Share on other sites

Have you tried using the local smtp server instead of gmail? Something like

#!/usr/bin/python

# Import modules for CGI handling
import cgi, cgitb

# set header for cgi
print "Content-Type: text/html\n\n"

# Import smtplib for the actual sending function
import smtplib

sender = 'admin@domain.heliohost.org'
receiver = 'email@gmail.com'

message = """From: Krydos <{f}>
To: Krydos <{t}>
Subject: SMTP e-mail test

This is a test e-mail message.
""".format(f=sender, t=receiver)

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receiver, message)
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"
Link to comment
Share on other sites

Using the script below:

 

#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
# Import modules for email handling
import smtplib
# set header for cgi
print "Content-Type: text/html\n\n"
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
name = form.getvalue('name')
email = form.getvalue('email')
subject = form.getvalue('subject')
message = form.getvalue('message')
# Send email
receivers = ['email@gmail.com']
text = "From: me\n" + "To: Contact <email@gmail.com>\n" + "Subject: " + subject + "\nFrom: " + name + " <" + email + ">\n" + message + "\n"
try:
server = smtplib.SMTP("localhost")#'smtp.gmail.com', 587)
server.sendmail("contact@server.heliohost.org", "email@gmail.com", text)
#server.starttls()
#server.login("email@gmail.com", "password")
#server.sendmail("email@gmail.com", receivers, text)
server.quit()
#print "Successfully sent email"
except:
#print "Error: unable to send email"
pass

And I am still getting error 500 :(

 

I tested and I am passing the right values for variables from form.

If I execute the script as a regular program at my machine passing the values copied from alert dialog box the message is sent.

I don't know why it is not working inside Apache.

Link to comment
Share on other sites

What I'm suggesting is break your script into parts to test each function individually rather than just trying the whole thing at once. It's a common troubleshooting technique. For instance, if sending email from localhost works, then you could try sending email from gmail. Then if that works you can try having the script just display the data on the screen, and if that works maybe put the whole thing together to send the email, etc. Breaking it into smaller parts allows you to find which part isn't working rather than just seeing that the whole script doesn't work and you have no idea why.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...