From 1aba5335a4053ab18e16f18f26d51d44cb24c28a Mon Sep 17 00:00:00 2001 From: Ildar Nasyrov Date: Sun, 24 Apr 2016 15:21:21 +0500 Subject: [PATCH 01/12] [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 ... + From d4072dce6e052ddb0fa80fc1f59608ab51aac103 Mon Sep 17 00:00:00 2001 From: Ildar Nasyrov Date: Sun, 24 Apr 2016 20:14:29 +0500 Subject: [PATCH 02/12] =?UTF-8?q?[DOC]=20mail=5Fmailgun:=20do=D1=81/index.?= =?UTF-8?q?rst?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mail_mailgun/doc/index.rst | 43 ++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/mail_mailgun/doc/index.rst b/mail_mailgun/doc/index.rst index 60e0a27..f937b3f 100644 --- a/mail_mailgun/doc/index.rst +++ b/mail_mailgun/doc/index.rst @@ -5,22 +5,29 @@ 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 ... +* register on http://mailgun.com +* On https://mailgun.com/app/domains click on sandbox...mailgun.org domain. Here you can see all information needed to configure odoo outgoing mail feature +* Copy API Key value into odoo + + * Open menu Settings/Parameters/System Parameters + * Edit mailgun.apikey parameter + * Put API Key from mailgun into Value field and save + +* From https://mailgun.com/cp/routes create new route + + * Priority: ``0`` + * Filter expression: ``catch_all()`` + * Actions: ``store(notify="http:///mailgun/notify")`` + +* In odoo remove 'localhost' Outgoing Mail Server and create 'mailgun'. Now you can send emails +* From odoo menu Settings/General Settings edit Alias Domain + + * Put your mailgun domain here. E.g. sandbox...mailgun.org + * Click 'Apply' button + +* Edit Messaging Alias for your user. Now you can receive emails that is sent to configured alias email address. + + * From menu Settings/Users/Users open you user and click 'Edit' + * On Preference tab put alias into Messaging Alias field and click 'Save' + From bef3130f4b25425592d50abe1e7f897e596c783c Mon Sep 17 00:00:00 2001 From: Ildar Nasyrov Date: Sun, 24 Apr 2016 20:27:58 +0500 Subject: [PATCH 03/12] [DOC] mail_mailgun: doc/index.rst formattings and new paragraph --- mail_mailgun/doc/index.rst | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/mail_mailgun/doc/index.rst b/mail_mailgun/doc/index.rst index f937b3f..398b548 100644 --- a/mail_mailgun/doc/index.rst +++ b/mail_mailgun/doc/index.rst @@ -9,25 +9,32 @@ Usage * On https://mailgun.com/app/domains click on sandbox...mailgun.org domain. Here you can see all information needed to configure odoo outgoing mail feature * Copy API Key value into odoo - * Open menu Settings/Parameters/System Parameters - * Edit mailgun.apikey parameter - * Put API Key from mailgun into Value field and save + * Open menu Settings/Parameters/System Parameters + * Edit mailgun.apikey parameter + * Put API Key from mailgun into Value field and save * From https://mailgun.com/cp/routes create new route - * Priority: ``0`` - * Filter expression: ``catch_all()`` - * Actions: ``store(notify="http:///mailgun/notify")`` + * Priority: ``0`` + * Filter expression: ``catch_all()`` + * Actions: ``store(notify="http:///mailgun/notify")`` * In odoo remove 'localhost' Outgoing Mail Server and create 'mailgun'. Now you can send emails * From odoo menu Settings/General Settings edit Alias Domain - * Put your mailgun domain here. E.g. sandbox...mailgun.org - * Click 'Apply' button + * Put your mailgun domain here. E.g. sandbox...mailgun.org + * Click 'Apply' button * Edit Messaging Alias for your user. Now you can receive emails that is sent to configured alias email address. - * From menu Settings/Users/Users open you user and click 'Edit' - * On Preference tab put alias into Messaging Alias field and click 'Save' + * From menu Settings/Users/Users open you user and click 'Edit' + * On Preference tab put alias into Messaging Alias field and click 'Save'. E.g. ``admin@sandbox...mailgun.org`` + +* Send email on ``admin@sandox...mailgun.org`` +* Open ``Discuss`` from odoo +* See your message there +* Stop odoo and send several emails again. On odoo starting you see all your messages in ``Discuss`` + + From 3d21182317de8ba1eba3307f6ad9f519974c81f9 Mon Sep 17 00:00:00 2001 From: Ivan Yelizariev Date: Tue, 3 May 2016 19:08:45 +0500 Subject: [PATCH 04/12] [DOC] update doc/index.rst --- mail_mailgun/doc/index.rst | 53 +++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/mail_mailgun/doc/index.rst b/mail_mailgun/doc/index.rst index 398b548..73c2bd0 100644 --- a/mail_mailgun/doc/index.rst +++ b/mail_mailgun/doc/index.rst @@ -6,12 +6,40 @@ Usage ===== * register on http://mailgun.com -* On https://mailgun.com/app/domains click on sandbox...mailgun.org domain. Here you can see all information needed to configure odoo outgoing mail feature -* Copy API Key value into odoo +* On https://mailgun.com/app/domains click on sandbox123...mailgun.org domain. Here you can see all information needed to configure odoo outgoing mail feature + * Copy API Key value into odoo + + * Open menu ``Settings / Parameters / System Parameters`` + * Create new parameter + + * key: mailgun.apikey parameter + * Value: API Key from mailgun (``key-...``) + * click Save + + * Copy smtp credentials into odoo + + * open ``Settings / Technical / Email / Outgoing Mail Servers`` + + * delete localhost + * create new server + + * Description: ``mailgun`` + * SMTP Server: ``smtp.mailgun.org`` + * Connection Security: ``SSL/TLS`` + * Username: e.g. ``postmaster@sandbox123....mailgun.org`` + * Password: ``...`` (copy ``Default Password`` from mailgun) + +* From odoo menu ``Settings / General Settings`` edit Alias Domain + + * Put your mailgun domain here. E.g. sandbox123...mailgun.org + * Click 'Apply' button + +* Open menu ``Settings / Users / Users`` - * Open menu Settings/Parameters/System Parameters - * Edit mailgun.apikey parameter - * Put API Key from mailgun into Value field and save + * choose your user and click ``[Edit]`` + * On Preference tab put alias into Messaging Alias field and click ``[Save]``. E.g. ``admin@sandbox...mailgun.org`` + +* Now you can receive emails that is sent to configured alias email address. * From https://mailgun.com/cp/routes create new route @@ -19,19 +47,8 @@ Usage * Filter expression: ``catch_all()`` * Actions: ``store(notify="http:///mailgun/notify")`` -* In odoo remove 'localhost' Outgoing Mail Server and create 'mailgun'. Now you can send emails -* From odoo menu Settings/General Settings edit Alias Domain - - * Put your mailgun domain here. E.g. sandbox...mailgun.org - * Click 'Apply' button - -* Edit Messaging Alias for your user. Now you can receive emails that is sent to configured alias email address. - - * From menu Settings/Users/Users open you user and click 'Edit' - * On Preference tab put alias into Messaging Alias field and click 'Save'. E.g. ``admin@sandbox...mailgun.org`` - -* Send email on ``admin@sandox...mailgun.org`` -* Open ``Discuss`` from odoo +* Via your favorite mail client (e.g. gmail.com) send email to ``admin@sandox...mailgun.org`` +* Open ``Discuss`` in odoo * See your message there * Stop odoo and send several emails again. On odoo starting you see all your messages in ``Discuss`` From e369e683cb41db30b3f2956151b1f7aafa202b6f Mon Sep 17 00:00:00 2001 From: Ivan Yelizariev Date: Wed, 4 May 2016 15:07:59 +0500 Subject: [PATCH 05/12] [DOC] tiny improvements --- mail_mailgun/README.rst | 6 +++--- mail_mailgun/doc/index.rst | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/mail_mailgun/README.rst b/mail_mailgun/README.rst index 3e74761..32c0535 100644 --- a/mail_mailgun/README.rst +++ b/mail_mailgun/README.rst @@ -11,17 +11,17 @@ Credits ======= Contributors -============ +------------ * Ildar Nasyrov Sponsors -======== +-------- * `IT-Projects LLC `_ Further information =================== -HTML Description: https://apps.odoo.com/apps/modules/VERSION/TECHNICAL_NAME/ +HTML Description: https://apps.odoo.com/apps/modules/9.0/mail_mailgun/ Usage instructions: ``_ diff --git a/mail_mailgun/doc/index.rst b/mail_mailgun/doc/index.rst index 73c2bd0..e7e3c49 100644 --- a/mail_mailgun/doc/index.rst +++ b/mail_mailgun/doc/index.rst @@ -12,7 +12,7 @@ Usage * Open menu ``Settings / Parameters / System Parameters`` * Create new parameter - * key: mailgun.apikey parameter + * key: ``mailgun.apikey`` * Value: API Key from mailgun (``key-...``) * click Save @@ -34,13 +34,11 @@ Usage * Put your mailgun domain here. E.g. sandbox123...mailgun.org * Click 'Apply' button -* Open menu ``Settings / Users / Users`` +* Set admin's email alias. Open menu ``Settings / Users / Users`` * choose your user and click ``[Edit]`` * On Preference tab put alias into Messaging Alias field and click ``[Save]``. E.g. ``admin@sandbox...mailgun.org`` -* Now you can receive emails that is sent to configured alias email address. - * From https://mailgun.com/cp/routes create new route * Priority: ``0`` @@ -50,6 +48,7 @@ Usage * Via your favorite mail client (e.g. gmail.com) send email to ``admin@sandox...mailgun.org`` * Open ``Discuss`` in odoo * See your message there +* Reply to the message and check it in your mail client (e.g. gmail.com) * Stop odoo and send several emails again. On odoo starting you see all your messages in ``Discuss`` From aa2bbc5efbf88bfda66c7c653460c29ce74b1358 Mon Sep 17 00:00:00 2001 From: Ivan Yelizariev Date: Wed, 4 May 2016 15:10:49 +0500 Subject: [PATCH 06/12] [REF] rename module to mailgun --- {mail_mailgun => mailgun}/README.rst | 0 {mail_mailgun => mailgun}/__init__.py | 0 {mail_mailgun => mailgun}/__openerp__.py | 0 {mail_mailgun => mailgun}/controllers/__init__.py | 0 {mail_mailgun => mailgun}/controllers/main.py | 0 {mail_mailgun => mailgun}/doc/index.rst | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename {mail_mailgun => mailgun}/README.rst (100%) rename {mail_mailgun => mailgun}/__init__.py (100%) rename {mail_mailgun => mailgun}/__openerp__.py (100%) rename {mail_mailgun => mailgun}/controllers/__init__.py (100%) rename {mail_mailgun => mailgun}/controllers/main.py (100%) rename {mail_mailgun => mailgun}/doc/index.rst (100%) diff --git a/mail_mailgun/README.rst b/mailgun/README.rst similarity index 100% rename from mail_mailgun/README.rst rename to mailgun/README.rst diff --git a/mail_mailgun/__init__.py b/mailgun/__init__.py similarity index 100% rename from mail_mailgun/__init__.py rename to mailgun/__init__.py diff --git a/mail_mailgun/__openerp__.py b/mailgun/__openerp__.py similarity index 100% rename from mail_mailgun/__openerp__.py rename to mailgun/__openerp__.py diff --git a/mail_mailgun/controllers/__init__.py b/mailgun/controllers/__init__.py similarity index 100% rename from mail_mailgun/controllers/__init__.py rename to mailgun/controllers/__init__.py diff --git a/mail_mailgun/controllers/main.py b/mailgun/controllers/main.py similarity index 100% rename from mail_mailgun/controllers/main.py rename to mailgun/controllers/main.py diff --git a/mail_mailgun/doc/index.rst b/mailgun/doc/index.rst similarity index 100% rename from mail_mailgun/doc/index.rst rename to mailgun/doc/index.rst From 3cd74e354be137f5383ad973c12f9c3ac9e9a775 Mon Sep 17 00:00:00 2001 From: Ivan Yelizariev Date: Wed, 4 May 2016 15:42:39 +0500 Subject: [PATCH 07/12] [DOC] updates to new modules name + improvements in docs --- mailgun/README.rst | 8 ++++---- mailgun/__openerp__.py | 2 +- mailgun/controllers/main.py | 4 ++-- mailgun/doc/index.rst | 29 +++++++++++++++-------------- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/mailgun/README.rst b/mailgun/README.rst index 32c0535..1ce53ae 100644 --- a/mailgun/README.rst +++ b/mailgun/README.rst @@ -1,6 +1,6 @@ -============== - Mail Mailgun -============== +========= + 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. @@ -21,7 +21,7 @@ Sponsors Further information =================== -HTML Description: https://apps.odoo.com/apps/modules/9.0/mail_mailgun/ +HTML Description: https://apps.odoo.com/apps/modules/9.0/mailgun/ Usage instructions: ``_ diff --git a/mailgun/__openerp__.py b/mailgun/__openerp__.py index f0b5872..50f9277 100644 --- a/mailgun/__openerp__.py +++ b/mailgun/__openerp__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- { - 'name': "Mail Mailgun", + 'name': "Mailgun", 'author': "IT-Projects LLC, Ildar Nasyrov", 'license': 'LGPL-3', 'website': "https://twitter.com/nasyrov_ildar", diff --git a/mailgun/controllers/main.py b/mailgun/controllers/main.py index 1e46977..90652bb 100644 --- a/mailgun/controllers/main.py +++ b/mailgun/controllers/main.py @@ -20,7 +20,7 @@ class MailMailgun(http.Controller): "recipient" : "admin@iledarn.ru" }) - @http.route('/mail_mailgun', auth='public', csrf=False) + @http.route('/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() @@ -38,7 +38,7 @@ class MailMailgun(http.Controller): print item print '\n' - @http.route('/mail_mailgun_mime', auth='public', csrf=False) + @http.route('/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') diff --git a/mailgun/doc/index.rst b/mailgun/doc/index.rst index e7e3c49..33ccd49 100644 --- a/mailgun/doc/index.rst +++ b/mailgun/doc/index.rst @@ -1,12 +1,13 @@ -============== - Mail mailgun -============== +========= + Mailgun +========= Usage ===== -* register on http://mailgun.com -* On https://mailgun.com/app/domains click on sandbox123...mailgun.org domain. Here you can see all information needed to configure odoo outgoing mail feature +* register or log in http://mailgun.com +* On https://mailgun.com/app/domains click on you domain, e.g. sandbox123...mailgun.org domain. Here you can see all information needed to configure odoo outgoing mail feature + * if you in sandbox domain, add Authorized Recepient * Copy API Key value into odoo * Open menu ``Settings / Parameters / System Parameters`` @@ -29,15 +30,10 @@ Usage * Username: e.g. ``postmaster@sandbox123....mailgun.org`` * Password: ``...`` (copy ``Default Password`` from mailgun) -* From odoo menu ``Settings / General Settings`` edit Alias Domain - - * Put your mailgun domain here. E.g. sandbox123...mailgun.org - * Click 'Apply' button - -* Set admin's email alias. Open menu ``Settings / Users / Users`` - - * choose your user and click ``[Edit]`` - * On Preference tab put alias into Messaging Alias field and click ``[Save]``. E.g. ``admin@sandbox...mailgun.org`` + * From odoo menu ``Settings / General Settings`` edit Alias Domain + + * Put your mailgun domain here. E.g. sandbox123...mailgun.org + * Click 'Apply' button * From https://mailgun.com/cp/routes create new route @@ -45,6 +41,11 @@ Usage * Filter expression: ``catch_all()`` * Actions: ``store(notify="http:///mailgun/notify")`` +* Set admin's email alias. Open menu ``Settings / Users / Users`` + + * choose your user and click ``[Edit]`` + * On Preference tab put alias into Messaging Alias field and click ``[Save]``. E.g. ``admin@sandbox...mailgun.org`` + * Via your favorite mail client (e.g. gmail.com) send email to ``admin@sandox...mailgun.org`` * Open ``Discuss`` in odoo * See your message there From 992a06744635515ce48d0c668265b74e1b605a11 Mon Sep 17 00:00:00 2001 From: Ivan Yelizariev Date: Wed, 4 May 2016 18:37:14 +0500 Subject: [PATCH 08/12] [ADD] mails via notification work --- mailgun/README.rst | 1 + mailgun/__init__.py | 1 + mailgun/controllers/main.py | 53 +++++-------------------------------- mailgun/models.py | 21 +++++++++++++++ 4 files changed, 29 insertions(+), 47 deletions(-) create mode 100644 mailgun/models.py diff --git a/mailgun/README.rst b/mailgun/README.rst index 1ce53ae..5be7a47 100644 --- a/mailgun/README.rst +++ b/mailgun/README.rst @@ -13,6 +13,7 @@ Credits Contributors ------------ * Ildar Nasyrov +* Ivan Yelizariev Sponsors -------- diff --git a/mailgun/__init__.py b/mailgun/__init__.py index 192867d..43dbbaa 100644 --- a/mailgun/__init__.py +++ b/mailgun/__init__.py @@ -1,2 +1,3 @@ # -*- coding: utf-8 -*- import controllers +import models diff --git a/mailgun/controllers/main.py b/mailgun/controllers/main.py index 90652bb..b03c8a6 100644 --- a/mailgun/controllers/main.py +++ b/mailgun/controllers/main.py @@ -6,52 +6,11 @@ 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('/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('/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' - - - - + @http.route('/mailgun/notify', auth='public', type='http', csrf=False) + def mailgun_notify(self, **kw): + # mailgun notification in json format + message_url = kw.get('message-url') + request.env['mail.thread'].sudo().mailgun_fetch_message(message_url) + return 'ok' diff --git a/mailgun/models.py b/mailgun/models.py new file mode 100644 index 0000000..5a246b4 --- /dev/null +++ b/mailgun/models.py @@ -0,0 +1,21 @@ +import requests +import time +import dateutil +import pytz + +from openerp import models, api + +from openerp.addons.mail.models.mail_message import decode +from openerp.addons.mail.models.mail_thread import mail_header_msgid_re + +import logging +_logger = logging.getLogger(__name__) + +class MailThread(models.AbstractModel): + _inherit = 'mail.thread' + + @api.model + def mailgun_fetch_message(self, message_url): + api_key = self.env['ir.config_parameter'].sudo().get_param('mailgun.apikey') + res = requests.get(message_url, headers={'Accept': 'message/rfc2822'}, auth=('api', api_key)) + self.message_process(False, res.json().get('body-mime')) From d8219d7e4ff604f0358899272c6f97241be59008 Mon Sep 17 00:00:00 2001 From: Ivan Yelizariev Date: Wed, 4 May 2016 19:42:50 +0500 Subject: [PATCH 09/12] [DOC] add todo about reloading mails after odoo starts to get undelivered mails --- mailgun/README.rst | 4 ++++ mailgun/doc/index.rst | 2 -- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mailgun/README.rst b/mailgun/README.rst index 5be7a47..a80bf09 100644 --- a/mailgun/README.rst +++ b/mailgun/README.rst @@ -7,6 +7,10 @@ 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. +TODO. + +* If emails are sent when odoo is stopped then Mailgun will retry (other than for delivery notification) during 8 hours at the following intervals before stop trying: 10 minutes, 10 minutes, 15 minutes, 30 minutes, 1 hour, 2 hour and 4 hours. This could be fixed by fetching undelivered messages after odoo starts. + Credits ======= diff --git a/mailgun/doc/index.rst b/mailgun/doc/index.rst index 33ccd49..a632863 100644 --- a/mailgun/doc/index.rst +++ b/mailgun/doc/index.rst @@ -50,8 +50,6 @@ Usage * Open ``Discuss`` in odoo * See your message there * Reply to the message and check it in your mail client (e.g. gmail.com) -* Stop odoo and send several emails again. On odoo starting you see all your messages in ``Discuss`` - From 859cbe07b49f9dfae36ec91a001a586999099340 Mon Sep 17 00:00:00 2001 From: Ivan Yelizariev Date: Wed, 4 May 2016 19:43:40 +0500 Subject: [PATCH 10/12] [DOC] fix format --- mailgun/README.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mailgun/README.rst b/mailgun/README.rst index a80bf09..4af26dc 100644 --- a/mailgun/README.rst +++ b/mailgun/README.rst @@ -7,7 +7,8 @@ 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. -TODO. +TODO +---- * If emails are sent when odoo is stopped then Mailgun will retry (other than for delivery notification) during 8 hours at the following intervals before stop trying: 10 minutes, 10 minutes, 15 minutes, 30 minutes, 1 hour, 2 hour and 4 hours. This could be fixed by fetching undelivered messages after odoo starts. From 1dddadfd39b95c667aceff04b2936838c1935f36 Mon Sep 17 00:00:00 2001 From: Ivan Yelizariev Date: Wed, 4 May 2016 19:48:28 +0500 Subject: [PATCH 11/12] [DOC] and revision --- mailgun/README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mailgun/README.rst b/mailgun/README.rst index 4af26dc..f1a4ae9 100644 --- a/mailgun/README.rst +++ b/mailgun/README.rst @@ -33,4 +33,4 @@ Usage instructions: ``_ Changelog: ``_ -Tested on Odoo 9.0 ODOO_COMMIT_SHA_TO_BE_UPDATED +Tested on Odoo 9.0 c8cd67c5d98b410cabe0a6efb3347a8a4de731d8 From 7a4930a913dbd7ce7362a03517fa94867b47dfa9 Mon Sep 17 00:00:00 2001 From: Ivan Yelizariev Date: Wed, 4 May 2016 19:56:00 +0500 Subject: [PATCH 12/12] [DOC] fix format --- mailgun/README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mailgun/README.rst b/mailgun/README.rst index f1a4ae9..4d45817 100644 --- a/mailgun/README.rst +++ b/mailgun/README.rst @@ -8,7 +8,7 @@ That is why we need this module. It fetches messages from mailgun using their AP and stores them in odoo. TODO ----- +==== * If emails are sent when odoo is stopped then Mailgun will retry (other than for delivery notification) during 8 hours at the following intervals before stop trying: 10 minutes, 10 minutes, 15 minutes, 30 minutes, 1 hour, 2 hour and 4 hours. This could be fixed by fetching undelivered messages after odoo starts.