I am reading the tutorial about Receiving Mail. I updated the app.yaml file as instructed:
application: hello-1-world
version: 1
runtime: python
api_version: 1
handlers:
- url: /favicon.ico
static_files: static/images/favicon.ico
upload: static/images/favicon.ico
- url: /_ah/mail/.+
script: handle_incoming_email.py
login: admin
- url: /.*
script: hw.py
inbound_services:
- mail
And created a handle_incoming_email.py
import cgi
import os
import logging
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.api import mail
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
class ReceiveEmail(InboundMailHandler):
def receive(self,message):
logging.info("Received email from %s" % message.sender)
plaintext = message.bodies(content_type='text/plain')
for text in plaintext:
txtmsg = ""
txtmsg = text[1].decode()
logging.info("Body is %s" % txtmsg)
self.response.out.write(txtmsg)
application = webapp.WSGIApplication([
ReceiveEmail.mapping()
], debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
I also have hw.py
that I used to practice sending email. That one works.
Now I go to http://localhost:8081/_ah/admin/inboundmail
and send an email to [email protected]
Can anyone explain to me how I process this email? How do I access the content of the email? I have the code
self.response.out.write(txtmsg)
in handle_incoming_email.py
but that does not print anything.
I would greatly appreciate if someone clarify how receiving email works.
For instance, in this question
class MailHandler (InboundMailHandler):
def receive(self, message):
sender = message.sender
user_account = db.GqlQuery("SELECT * FROM Task WHERE user = :1", sender).fetch(5)
as far as I understand sender
is the email of the sender. So, in my case, how do I access the sender email address.
Also, why do I need to have a separate script to handle incoming mail? Why can't I put the ReceiveEmail
handler in my hw.py
script? If I do that, where do I put the line
application = webapp.WSGIApplication([
ReceiveEmail.mapping()
], debug=True)
I would be grateful if you can help me with these questions.
(I asked the same question in GAE group but there were no answers.)
Is [email protected] a valid google user? GAE can receive/send mails only from the google user of your application. Your code seems correct.
"Also, why do I need to have a separate script to handle incoming mail? Why can't I put the ReceiveEmail handler in my hw.py" -> the main script is to handle url request, I think is much clearer in this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With