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'))