How to receive email with Django using InboundMailHandler ?

282 views
Skip to first unread message

Minie Takalova

unread,
Oct 12, 2017, 6:14:45 AM10/12/17
to Google App Engine

Hello

Can somebody direct me to solution how to receiving and handle emails inside Django app on App engine standard?

First place where I start was:
https://cloud.google.com/appengine/docs/standard/python/mail/receiving-mail-with-mail-api

All is working fine for webapp2 but I would
like to handle it with Django view, cause rest of application is in Django.

When I use in app.yaml

inbound_services:
- mail

- url: /_ah/mail/.+
  script: myapp.views.handle_email
  login: admin

the handle_email function get two arguments. First is type Dict with this content

'wsgi.input': <cStringIO.StringI object at 0x7f0524277938>, 
'PATH_INFO': '/_ah/mail/tes...@myproject.localhost',
'wsgi.multiprocess': True,
'USER_EMAIL': '',
'wsgi.errors': <google.appengine.api.logservice.logservice._LogsStreamBuffer object at 0x7f0525c73090>,
 'REQUEST_ID_HASH': 'B4788AA9',
 'wsgi.run_once': False,
 'TZ': 'UTC',
 'APPLICATION_ID': 'dev~myproject',
 'HTTP_CONTENT_LENGTH': '676',
 'USER_IS_ADMIN': '0',
 'HTTP_CONTENT_TYPE': 'message/rfc822',
 'HTTPS': 'off',
 'wsgi.url_scheme': 'http',
 'SERVER_PROTOCOL': 'HTTP/1.1',
 'DEFAULT_VERSION_HOSTNAME': 'localhost:8080',
 'AUTH_DOMAIN': 'gmail.com',
 'HTTP_X_APPENGINE_COUNTRY': 'ZZ',
 'QUERY_STRING': '',
 'PATH_TRANSLATED': '/mywork/googlecloud/myproject/myapp/views.py',
 'INSTANCE_ID': '46aadbcae55fddddb13fdedbsfdsfdsdsadsa',
 'REMOTE_ADDR': '0.1.0.20',
 'USER_ID': '',
 'REQUEST_METHOD': 'POST',
 'USER_ORGANIZATION': '',
 'SERVER_PORT': '8080',
 'CONTENT_LENGTH': '676',
 'REQUEST_LOG_ID': '9fe3f972f494b37acc49aa11c7e5abe4bcbada8d',
 'SCRIPT_NAME': '',
 'HTTP_HOST': 'localhost:8080',
 'CONTENT_TYPE': 'message/rfc822',
 'APPENGINE_RUNTIME': 'python27',
 'SERVER_NAME': 'localhost',
 'wsgi.multithread': True,
 'wsgi.version': (1, 0),
 'CURRENT_VERSION_ID': '47.190328127403515824',
 'CURRENT_MODULE_ID': 'default',
 'USER_NICKNAME': '',
 'SERVER_SOFTWARE': 'Development/2.0',
 'DATACENTER': 'us1'

the second is type instancemethod with

'<bound method WsgiRequest._StartResponse of <google.appengine.runtime.wsgi.WsgiRequest object at 0x7f0525c732d0>>'


It is possible to use InboundMailHandler with this data?
How to use
InboundMailHandler.mapping() ?

Final goal for me is to read data from attachement.

Nice day for all of you.

Yannick (Cloud Platform Support)

unread,
Oct 12, 2017, 4:58:07 PM10/12/17
to Google App Engine
Hello Minie,

If I understand properly you already have a InboundMailHandler mail handler in your Django application, but you would like to use a view instead? Is there a specific reason why you want to do this if your current system already works?

There is no documentation available for this specific use case but all you should really need to do is have the Django url matcher send mail requests to your handler.


On Thursday, October 12, 2017 at 6:14:45 AM UTC-4, Minie Takalova wrote:

Hello

Can somebody direct me to solution how to receiving and handle emails inside Django app on App engine standard?

First place where I start was:
https://cloud.google.com/appengine/docs/standard/python/mail/receiving-mail-with-mail-api

All is working fine for webapp2 but I would
like to handle it with Django view, cause rest of application is in Django.

When I use in app.yaml

inbound_services:
- mail

- url: /_ah/mail/.+
  script: myapp.views.handle_email
  login: admin

the handle_email function get two arguments. First is type Dict with this content

'wsgi.input': <cStringIO.StringI object at 0x7f0524277938>, 
'PATH_INFO': '/_ah/mail/tester@myproject.localhost', 

Mintaka

unread,
Oct 12, 2017, 5:28:14 PM10/12/17
to google-a...@googlegroups.com
Hello Yannick,

The reason to have handle for in Django view code, instead of webapp2
is my solicitude about cleanness and readability of code.

Now I have code in separate file. Somethink like:

class LogSenderHandler(InboundMailHandler):
def receive(self, mail_message):
for filename, content in mail_message.attachments:
content = content.decode()
""" call function in django view """

app = webapp2.WSGIApplication([LogSenderHandler.mapping()], debug=True)

It is like to have another, very different application and walk around
standard Django logging, debugging, urls, .... A little
schizophrenic.

If exist way how to do it without webapp2 I rather do it that way, but
if it does not exist, I will have to live with it.

Thank you for your interest.

2017-10-12 22:58 GMT+02:00 'Yannick (Cloud Platform Support)' via
Google App Engine <google-a...@googlegroups.com>:
>> 'PATH_INFO': '/_ah/mail/tes...@myproject.localhost',
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Google App Engine" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/google-appengine/mqHgNaAxIL0/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> google-appengi...@googlegroups.com.
> To post to this group, send email to google-a...@googlegroups.com.
> Visit this group at https://groups.google.com/group/google-appengine.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/google-appengine/c5308623-9ba0-4ffc-9acc-c36bdd122f97%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Yannick (Cloud Platform Support)

unread,
Oct 16, 2017, 11:49:17 AM10/16/17
to Google App Engine
I believe it should be possible but do not have any code samples I can give you. Have you tried posting your question on Stack Overflow?


On Thursday, October 12, 2017 at 5:28:14 PM UTC-4, Minie Takalova wrote:
Hello Yannick,

The reason to have handle for in Django view code, instead of webapp2
is my solicitude about cleanness and readability of code.

Now I have code in separate file. Somethink like:

class LogSenderHandler(InboundMailHandler):
    def receive(self, mail_message):
        for filename, content in mail_message.attachments:
            content = content.decode()
            """ call function in django view """

app = webapp2.WSGIApplication([LogSenderHandler.mapping()], debug=True)

It is like to have another, very different application and walk around
standard Django logging, debugging, urls, ....  A little
schizophrenic.

If exist way how to do it without webapp2 I rather do it that way, but
if it does not exist, I will have to live with it.

Thank you for your interest.

2017-10-12 22:58 GMT+02:00 'Yannick (Cloud Platform Support)' via
Google App Engine <google-appengine@googlegroups.com>:
>> 'PATH_INFO': '/_ah/mail/tester@myproject.localhost',
Reply all
Reply to author
Forward
0 new messages