Reputation: 335
I am hosting a website on Google Apps Engine and am trying to use Python's mail API to take POST data and send an email.
Here is my script:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import mail
class SendEmail(webapp.RequestHandler):
def post(self):
name = self.request.get('name')
# self.response.out.write(name)
email = self.request.get('email')
tempSubject = self.request.get('subject')
msg = self.request.get('message')
if name is None:
self.response.out.write("Error: You did not enter a name.")
elif email is None:
self.response.out.write("Error: You did not enter an email.")
elif tempSubject is None:
self.response.out.write("Error: You did not enter a subject.")
elif msg is None:
self.response.out.write("Error: You did not enter a message.")
else:
_subject = "Msg from: " + name + "Re: " + tempSubject
message = mail.EmailMessage(sender = "[email protected]", to = "[email protected]", subject = _subject, body = msg, reply_to = email)
message.send()
def runApp():
application = webapp.WSGIApplication([('/email', SendEmail)], debug=True)
run_wsgi_app(application)
if __name__ == '__main__':
runApp()
And here is the traceback from the log on the server:
<type 'exceptions.NameError'>: name 'name' is not defined
Traceback (most recent call last):
File "/base/data/home/apps/s~alex-young/1.365202894602706277/email.py", line 5, in <module>
class SendEmail(webapp.RequestHandler):
File "/base/data/home/apps/s~alex-young/1.365202894602706277/email.py", line 14, in SendEmail
if name is None:
I ran the script locally with no errors, but once I try to run it on the server it keeps insisting the name
variable I declared doesn't exist. Any idea why this happens?
Also, if I comment out that line, it says email
doesn't exist, and so forth
Upvotes: 1
Views: 104
Reputation: 335
As it turns out, sometimes I used spaces to indent and other times I used tabs. Python didn't like that. Here is the final code:
import cgi
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import mail
class SendEmail(webapp.RequestHandler):
def post(self):
name = self.request.get('name', '')
email = self.request.get('email', '')
tempSubject = self.request.get('subject', '')
msg = self.request.get('message', '')
if name is None:
self.response.out.write("Error: You did not enter a name.")
elif email is None:
self.response.out.write("Error: You did not enter an email.")
elif tempSubject is None:
self.response.out.write("Error: You did not enter a subject.")
elif msg is None:
self.response.out.write("Error: You did not enter a message.")
else:
_subject = "Message from: " + name + ", Re: " + tempSubject
msg += "\n\nI can be reached at "
msg += email
message = mail.EmailMessage(sender = "[email protected]", to = "[email protected]")
message.subject = _subject
message.body = msg
message.send()
self.redirect('/')
def runApp():
application = webapp.WSGIApplication([('/email', SendEmail)], debug=True)
run_wsgi_app(application)
if __name__ == '__main__':
runApp()
Upvotes: 1