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.

136 lines
5.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Tecnativa - Antonio Espinosa
  3. # Copyright 2016 Tecnativa - Carlos Dauden
  4. # Copyright 2017 Tecnativa - Pedro M. Baeza
  5. # Copyright 2017 Tecnativa - David Vidal
  6. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  7. import requests
  8. import json
  9. from openerp import _, api, models
  10. from openerp.exceptions import UserError
  11. class ResPartner(models.Model):
  12. _inherit = 'res.partner'
  13. @api.multi
  14. def email_bounced_set(self, tracking_emails, reason, event=None):
  15. res = super(ResPartner, self).email_bounced_set(
  16. tracking_emails, reason, event=event)
  17. self._email_bounced_set(reason, event)
  18. return res
  19. @api.multi
  20. def _email_bounced_set(self, reason, event):
  21. for partner in self:
  22. if not partner.email:
  23. continue
  24. body = _('Email has been bounced: %s\n'
  25. 'Reason: %s\n'
  26. 'Event: %s') % (partner.email, reason,
  27. event and event.get('Message-Id',
  28. _('unknown')))
  29. partner.message_post(body=body)
  30. @api.multi
  31. def check_email_validity(self):
  32. """
  33. Checks mailbox validity with Mailgun's API
  34. API documentation:
  35. https://documentation.mailgun.com/en/latest/api-email-validation.html
  36. """
  37. api_key, api_url, domain, validation_key = self.env[
  38. 'mail.tracking.email']._mailgun_values()
  39. if not validation_key:
  40. raise UserError(_('You need to configure mailgun.validation_key'
  41. ' in order to be able to check mails validity'))
  42. for partner in self:
  43. res = requests.get(
  44. "%s/address/validate" % api_url,
  45. auth=("api", validation_key), params={
  46. "address": partner.email,
  47. "mailbox_verification": True,
  48. })
  49. if not res or res.status_code != 200:
  50. raise UserError(_(
  51. 'Error %s trying to '
  52. 'check mail' % res.status_code or 'of connection'))
  53. content = json.loads(res.content, res.apparent_encoding)
  54. if 'mailbox_verification' not in content:
  55. raise UserError(
  56. _("Mailgun Error. Mailbox verification value wasn't"
  57. " returned"))
  58. # Not a valid address: API sets 'is_valid' as False
  59. # and 'mailbox_verification' as None
  60. if not content['is_valid']:
  61. partner.email_bounced = True
  62. raise UserError(
  63. _('%s is not a valid email address. Please check it '
  64. 'in order to avoid sending issues') % (partner.email))
  65. # If the mailbox is not valid API returns 'mailbox_verification'
  66. # as a string with value 'false'
  67. if content['mailbox_verification'] == 'false':
  68. partner.email_bounced = True
  69. raise UserError(
  70. _('%s failed the mailbox verification. Please check it '
  71. 'in order to avoid sending issues') % (partner.email))
  72. # If Mailgun can't complete the validation request the API returns
  73. # 'mailbox_verification' as a string set to 'unknown'
  74. if content['mailbox_verification'] == 'unknown':
  75. raise UserError(
  76. _("%s couldn't be verified. Either the request couln't be "
  77. "completed or the mailbox provider doesn't support "
  78. "email verification") % (partner.email))
  79. @api.multi
  80. def check_email_bounced(self):
  81. """
  82. Checks if the partner's email is in Mailgun's bounces list
  83. API documentation:
  84. https://documentation.mailgun.com/en/latest/api-suppressions.html
  85. """
  86. api_key, api_url, domain, validation_key = self.env[
  87. 'mail.tracking.email']._mailgun_values()
  88. for partner in self:
  89. res = requests.get(
  90. '%s/%s/bounces/%s' % (api_url, domain, partner.email),
  91. auth=("api", api_key))
  92. if res.status_code == 200 and not partner.email_bounced:
  93. partner.email_bounced = True
  94. elif res.status_code == 404 and partner.email_bounced:
  95. partner.email_bounced = False
  96. @api.multi
  97. def force_set_bounced(self):
  98. """
  99. Forces partner's email into Mailgun's bounces list
  100. API documentation:
  101. https://documentation.mailgun.com/en/latest/api-suppressions.html
  102. """
  103. api_key, api_url, domain, validation_key = self.env[
  104. 'mail.tracking.email']._mailgun_values()
  105. for partner in self:
  106. res = requests.post(
  107. '%s/%s/bounces' % (api_url, domain),
  108. auth=("api", api_key),
  109. data={'address': partner.email})
  110. partner.email_bounced = (
  111. res.status_code == 200 and not partner.email_bounced)
  112. @api.multi
  113. def force_unset_bounced(self):
  114. """
  115. Forces partner's email deletion from Mailgun's bounces list
  116. API documentation:
  117. https://documentation.mailgun.com/en/latest/api-suppressions.html
  118. """
  119. api_key, api_url, domain, validation_key = self.env[
  120. 'mail.tracking.email']._mailgun_values()
  121. for partner in self:
  122. res = requests.delete(
  123. '%s/%s/bounces/%s' % (api_url, domain, partner.email),
  124. auth=("api", api_key))
  125. if res.status_code in (200, 404) and partner.email_bounced:
  126. partner.email_bounced = False