tosites Posted January 12, 2017 Posted January 12, 2017 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 handlingimport cgi, cgitb # Import modules for email handlingimport smtplib # Create instance of FieldStorageform = cgi.FieldStorage() # Get data from fieldsname = form.getvalue('name')email = form.getvalue('email')subject = form.getvalue('subject')message = form.getvalue('message') # Send emailreceivers = ['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
Krydos Posted January 12, 2017 Posted January 12, 2017 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 linesname = "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?
tosites Posted January 14, 2017 Author Posted January 14, 2017 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.
tosites Posted January 15, 2017 Author Posted January 15, 2017 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 ormisconfiguration and was unable to completeyour 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 availablein the server error log.</p><p>Additionally, a 500 Internal Server Errorerror was encountered while trying to use an ErrorDocument to handle the request.</p></body></html>
Krydos Posted January 15, 2017 Posted January 15, 2017 Adding the linesname = "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?
tosites Posted January 16, 2017 Author Posted January 16, 2017 No. The same error occur using this lines that I have when I read the values from my form.
Krydos Posted January 16, 2017 Posted January 16, 2017 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"
tosites Posted January 16, 2017 Author Posted January 16, 2017 Using the script below: #!/usr/bin/python # Import modules for CGI handlingimport cgi, cgitb # Import modules for email handlingimport smtplib # set header for cgiprint "Content-Type: text/html\n\n" # Create instance of FieldStorageform = cgi.FieldStorage() # Get data from fieldsname = form.getvalue('name')email = form.getvalue('email')subject = form.getvalue('subject')message = form.getvalue('message') # Send emailreceivers = ['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.
Krydos Posted January 16, 2017 Posted January 16, 2017 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.
tosites Posted January 16, 2017 Author Posted January 16, 2017 Worked with localhost. Thank you very much. I tested calling directly with: http://server.heliohost.org/cgi-bin/send.py?name=A&email=B&subject=C&message=D And it is sending the email. Thanks again.
Recommended Posts