From 1aba5335a4053ab18e16f18f26d51d44cb24c28a Mon Sep 17 00:00:00 2001 From: Ildar Nasyrov Date: Sun, 24 Apr 2016 15:21:21 +0500 Subject: [PATCH] [ADD] mail_mailgun: new module --- mail_mailgun/README.rst | 30 +++++++++++++++ mail_mailgun/__init__.py | 2 + mail_mailgun/__openerp__.py | 12 ++++++ mail_mailgun/controllers/__init__.py | 2 + mail_mailgun/controllers/main.py | 57 ++++++++++++++++++++++++++++ mail_mailgun/doc/index.rst | 26 +++++++++++++ 6 files changed, 129 insertions(+) create mode 100644 mail_mailgun/README.rst create mode 100644 mail_mailgun/__init__.py create mode 100644 mail_mailgun/__openerp__.py create mode 100644 mail_mailgun/controllers/__init__.py create mode 100644 mail_mailgun/controllers/main.py create mode 100644 mail_mailgun/doc/index.rst diff --git a/mail_mailgun/README.rst b/mail_mailgun/README.rst new file mode 100644 index 0000000..3e74761 --- /dev/null +++ b/mail_mailgun/README.rst @@ -0,0 +1,30 @@ +============== + Mail Mailgun +============== + +With this module you can receive incoming messages from mailgun. +There is no IMAP or POP3 servers on mailgun that is to be used with odoo. +That is why we need this module. It fetches messages from mailgun using their API +and stores them in odoo. + +Credits +======= + +Contributors +============ +* Ildar Nasyrov + +Sponsors +======== +* `IT-Projects LLC `_ + +Further information +=================== + +HTML Description: https://apps.odoo.com/apps/modules/VERSION/TECHNICAL_NAME/ + +Usage instructions: ``_ + +Changelog: ``_ + +Tested on Odoo 9.0 ODOO_COMMIT_SHA_TO_BE_UPDATED diff --git a/mail_mailgun/__init__.py b/mail_mailgun/__init__.py new file mode 100644 index 0000000..192867d --- /dev/null +++ b/mail_mailgun/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +import controllers diff --git a/mail_mailgun/__openerp__.py b/mail_mailgun/__openerp__.py new file mode 100644 index 0000000..f0b5872 --- /dev/null +++ b/mail_mailgun/__openerp__.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +{ + 'name': "Mail Mailgun", + 'author': "IT-Projects LLC, Ildar Nasyrov", + 'license': 'LGPL-3', + 'website': "https://twitter.com/nasyrov_ildar", + 'category': 'Discuss', + 'version': '1.0.0', + 'depends': ['mail'], + 'data': [ + ], +} diff --git a/mail_mailgun/controllers/__init__.py b/mail_mailgun/controllers/__init__.py new file mode 100644 index 0000000..a84d81a --- /dev/null +++ b/mail_mailgun/controllers/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +import main diff --git a/mail_mailgun/controllers/main.py b/mail_mailgun/controllers/main.py new file mode 100644 index 0000000..1e46977 --- /dev/null +++ b/mail_mailgun/controllers/main.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +from openerp.addons.web import http +from openerp.addons.web.http import request +import werkzeug +import email +import requests +import simplejson + + +class MailMailgun(http.Controller): + + def get_logs(self): + return requests.get( + "https://api.mailgun.net/v3/iledarn.ru/events", + auth=("api", "key-8345fd06eeb8b27a3b1f9a1e025519ad"), + params={"begin" : "Fri, 3 May 2013 09:00:00 -0000", + "ascending" : "yes", + "limit" : 25, + "pretty" : "yes", + "recipient" : "admin@iledarn.ru" + }) + + @http.route('/mail_mailgun', auth='public', csrf=False) + def incoming_mail(self, **kw): + print '\n\n\n', 'in incoming_mail ', 'kw ', kw, '\n\n\n\n' + res = self.get_logs() + logs = res.text + logs_obj = simplejson.loads(logs) + items = logs_obj.get('items') + print '\n\n\n', 'logs ', logs, '\n\n\n' + print '\n\n\n', 'logs_obj ', logs_obj, '\n\n\n' + counter = 1 + for item in items: + mes = 'Item ' + str(counter) + counter = counter + 1 + if 'store' in item['routes'][0]['actions'][0]: + print '\n', mes, '\n' + print item + print '\n' + + @http.route('/mail_mailgun_mime', auth='public', csrf=False) + def incoming_mail_mime(self, **kw): + print '\n\n\n', 'in incoming_mail_mime ', 'kw ', kw, '\n\n\n\n' + body_mime = kw.get('body-mime') + mail_thread = request.env['mail.thread'].sudo() + # mail_thread_obj = request.env['res.partner'].sudo() + # msg_dict = mail_thread_obj.message_parse(body_mime) + # print '\n\n\n', 'msg_dict ', msg_dict, '\n\n\n' + # msg_id = msg_dict.get('message_id') + # print '\n\n\n', 'msg_id ', msg_id, '\n\n\n' + # mail_thread_obj.message_new(msg_dict) + res_id = mail_thread.message_process(model=False, message=body_mime) + print '\n\n\n', 'res_id ', res_id, '\n\n\n' + + + + diff --git a/mail_mailgun/doc/index.rst b/mail_mailgun/doc/index.rst new file mode 100644 index 0000000..60e0a27 --- /dev/null +++ b/mail_mailgun/doc/index.rst @@ -0,0 +1,26 @@ +============== + Mail mailgun +============== + +Usage +===== + +First we should register on mailgun.com. +After registgration there will be one domain available. E.g. sandbox5543f1479be64e5aac193406b4cdccf8.mailgun.org. +From this point we could already use API calls to mailgun API. There is API key on the +https://mailgun.com/app/domains/sandbox5543f1479be64e5aac193406b4cdccf8.mailgun.org page. +We can do all operations using mailgun API if we know our API key. More about it +on https://documentation.mailgun.com/. + +There is 'Add New Domain' on the https://mailgun.com/app/domains to add new domains. +All domains that we add manually should be verified. +After adding your domain you will see the https://mailgun.com/app/domains//verify page +with instructions on how to verify and use new domain. + +To receive incoming messages we specify so called routes on mailgun. +You can see more about the routes here https://documentation.mailgun.com/user_manual.html#receiving-forwarding-and-storing-messages + +Instruction how to check that module works. What shall user do and what would user get, e.g. +* Open menu ... +* Click ... +