You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.3 KiB

  1. # -*- coding: utf-8 -*-
  2. import logging
  3. import requests
  4. from openerp import api, models
  5. try:
  6. import simplejson as json
  7. except ImportError:
  8. import json
  9. _logger = logging.getLogger(__name__)
  10. class MailThread(models.AbstractModel):
  11. _inherit = "mail.thread"
  12. @api.model
  13. def mailgun_fetch_message(self, message_url):
  14. api_key = self.env["ir.config_parameter"].sudo().get_param("mailgun.apikey")
  15. res = requests.get(
  16. message_url,
  17. headers={"Accept": "message/rfc2822"},
  18. auth=("api", api_key),
  19. verify=False,
  20. )
  21. self.message_process(False, res.json().get("body-mime"))
  22. class IrConfigParameter(models.Model):
  23. _inherit = ["ir.config_parameter"]
  24. @api.model
  25. def mailgun_verify(self):
  26. verified = self.get_param("mailgun.verified")
  27. if verified:
  28. return
  29. api_key = self.get_param("mailgun.apikey")
  30. mail_domain = self.get_param("mail.catchall.domain")
  31. if api_key and mail_domain:
  32. url = "https://api.mailgun.net/v3/domains/%s/verify" % mail_domain
  33. res = requests.put(url, auth=("api", api_key))
  34. if (
  35. res.status_code == 200
  36. and json.loads(res.text)["domain"]["state"] == "active"
  37. ):
  38. self.set_param("mailgun.verified", "1")