zjm1126 zjm1126 - 27 days ago 2
Python Question

this is my Receiving Email code,but can't Receiving Email .. (google-app-engine)

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
bodies = message.bodies('text/plain')
allBodies = ""
#for body in bodies:
# allBodies = allBodies + "\n---------------------------\n" + body[1].decode()
#m= mail.EmailMessage(sender="zjm1126@gmail.com ",subject="reply to "+_subject)
#m.to = _sender
#m.body =allBodies
message = mail.EmailMessage(sender="zjm1126@gmail.com",
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


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


application: zjm1126

version: 1-2
runtime: python
api_version: 1

- mail

- 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:zjm1126@gmail.com send some words to ss@zjm1126.appspotmail.com

I can't get a Receiving Email, why?

Answer Source

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 ss@zjm1126.appspotmail.com 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