views:

159

answers:

3
import logging, email
from google.appengine.ext import webapp 
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler 
from google.appengine.ext.webapp.util import run_wsgi_app


class LogSenderHandler(InboundMailHandler):
    def receive(self, message):
        _subject = message.subject
        _sender=message.sender
        bodies = message.bodies('text/plain')
        allBodies = ""
        #for body in bodies:
        #  allBodies = allBodies + "\n---------------------------\n" + body[1].decode()
        #m= mail.EmailMessage(sender="[email protected] ",subject="reply to "+_subject)
        #m.to = _sender
        #m.body =allBodies
        #m.send()
        message = mail.EmailMessage(sender="[email protected]",
                                        subject="Your account has been approved")
        message.to = _sender
        message.body = """
        Dear Albert:

        Your example.com account has been approved.  You can now visit
        http://www.example.com/ and sign in using your Google Account to
        access new features.

        Please let us know if you have any questions.

        The example.com Team
        """

        message.send()



application = webapp.WSGIApplication([LogSenderHandler.mapping()], debug=True)

app.yaml:

application: zjm1126

version: 1-2
runtime: python
api_version: 1

inbound_services:
- mail

handlers:
- url: /media
  static_dir: media

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

- url: /
  script: a.py

- url: /sign
  script: a.py

- url: .*
  script: django_bootstrap.py

i use my email:[email protected] send some words to [email protected]

but i can't get a Receiving Email, why ???

thanks

and this is the log:

alt text

+1  A: 

It looks like you're trying to make code from mail send\receive tutorial to work. I used that tutorial also to check how mail service works and didn't have problems with it. What I could suggest doing is:

  1. decouple mail sending and receiving scripts as it seem like you're going to cycle it;

  2. I guess you already have the sending code somewhere else, but just in case, something has to send an email to [email protected] to trigger the LogSenderHandler handler;

  3. You can check and debug your code locally by using zjm1126 development console. Try sending an email from here: http://localhost:8080/_ah/admin/inboundmail and put a breakpoint into the LogSenderHandler.receive method to see if it gets hit and what's going on after that;

  4. In your yaml I see other handlers but webapp.WSGIApplication has only LogSenderHandler mappings. It might be the reason why those other scripts are not getting executed;

other than that your code and yaml look fine and should work

hope this helps, regards

serge_gubenko
A: 

Everything looks fine - your handler is returning a 200 OK. If you're not receiving the email it's sending, try logging the values you're using so you can check that everything's valid and what you expect it to be.

Nick Johnson
A: 

I had the same problem after following the google tutorial as well. Thanks to this tute I discovered a rather important bit of code that slipped my mind and isn't in the google tutorial.

def main():
    run_wsgi_app(application)
if __name__ == "__main__":
    main()

Hope that helps.

Poj