BPm
BPm

Reputation: 2994

google appengine - how to configure to send and receive email

I've googled a lot on this and I dont know what I'm missing. I have a contact form so users can contact me:

contact.html

<form id="contact" action="" method="post" name="contact">
    <label for="subject">Subject:</label>
    <input id="id_subject" type="text" name="subject" maxlength="100">
    <label for="email">Email:</label>
    <input id="id_email" type="text" name="email">
    <label for="message">Message:</label>
    <textarea id="id_message" rows="10" cols="40" name="message"></textarea>
    <input type="submit" value="Submit">
</form>

app.yaml

inbound_services:
- mail    
- url: /_ah/mail/.+
  script: main.py
  login: admin

main.py

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.api import mail
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler 

class Contact(webapp.RequestHandler):
    def get(self):  
        self.response.out.write(template.render('contact.html', {}))
    def post(self):
        sender = self.request.get("email")
        subject = self.request.get("subject")
        body = self.request.get("message")          
        message = mail.EmailMessage(sender=sender, subject=subject)
        message.to = "[email protected]"
        message.body = body
        message.send()

class LogSenderHandler(InboundMailHandler):
    def receive(self, mail_message):
        logging.info("Received a message from: " + mail_message.sender)

def main():
    application = webapp.WSGIApplication([('/contact', Contact),  
                                           LogSenderHandler.mapping()],
                                           debug=True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()

The class LogSenderHandler doesn't work, the receive() requires an extra argument that i don't know where to get. So I don't comment it out when i deploy. It's a mess in my head. I don't know how to put it together.
When I tried to email to myself I get this on my dashboard logs:

Unauthorized sender
Traceback (most recent call last):
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 703, in __call__
    handler.post(*groups)
  File "/base/data/home/apps/s~projectname/1.354692671370598794/main.py", line 27, in post
    message.send()
  File "/base/python_runtime/python_lib/versions/1/google/appengine/api/mail.py", line 894, in send
    raise ERROR_MAP[e.application_error](e.error_detail)
InvalidSenderError: Unauthorized sender

Could you help please?

Upvotes: 3

Views: 2660

Answers (2)

Neil
Neil

Reputation: 625

You can't send through the email submitted on the form, since you aren't authorized to use that email. You either have to use an email you control or the user's email by connecting to their Google account. http://code.google.com/appengine/docs/python/mail/sendingmail.html This page says which email you can use to send emails from, just scroll down right after the first code block.

Upvotes: 1

pradeek
pradeek

Reputation: 22095

From the App Engine docs

The email address of the sender, the From address. The sender address must be one of the following types:

  • The address of a registered administrator for the application. You can add administrators to an application using the Administration Console.

  • The address of the user for the current request signed in with a Google Account. You can determine the current user's email address with the Users API. The user's account must be a Gmail account, or be on a domain managed by Google Apps.

  • Any valid email receiving address for the app (such as [email protected]).

So if your sender email id is not one of these, you will get this error.

Upvotes: 1

Related Questions