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.

135 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 odoo import _, api, models
  10. from odoo.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.id or _('unknown'))
  28. partner.message_post(body=body)
  29. @api.multi
  30. def check_email_validity(self):
  31. """
  32. Checks mailbox validity with Mailgun's API
  33. API documentation:
  34. https://documentation.mailgun.com/en/latest/api-email-validation.html
  35. """
  36. api_key, api_url, domain, validation_key = self.env[
  37. 'mail.tracking.email']._mailgun_values()
  38. if not validation_key:
  39. raise UserError(_('You need to configure mailgun.validation_key'
  40. ' in order to be able to check mails validity'))
  41. for partner in self:
  42. res = requests.get(
  43. "%s/address/validate" % api_url,
  44. auth=("api", validation_key), params={
  45. "address": partner.email,
  46. "mailbox_verification": True,
  47. })
  48. if not res or res.status_code != 200:
  49. raise UserError(_(
  50. 'Error %s trying to '
  51. 'check mail' % res.status_code or 'of connection'))
  52. content = json.loads(res.content, res.apparent_encoding)
  53. if 'mailbox_verification' not in content:
  54. raise UserError(
  55. _("Mailgun Error. Mailbox verification value wasn't"
  56. " returned"))
  57. # Not a valid address: API sets 'is_valid' as False
  58. # and 'mailbox_verification' as None
  59. if not content['is_valid']:
  60. partner.email_bounced = True
  61. raise UserError(
  62. _('%s is not a valid email address. Please check it '
  63. 'in order to avoid sending issues') % (partner.email))
  64. # If the mailbox is not valid API returns 'mailbox_verification'
  65. # as a string with value 'false'
  66. if content['mailbox_verification'] == 'false':
  67. partner.email_bounced = True
  68. raise UserError(
  69. _('%s failed the mailbox verification. Please check it '
  70. 'in order to avoid sending issues') % (partner.email))
  71. # If Mailgun can't complete the validation request the API returns
  72. # 'mailbox_verification' as a string set to 'unknown'
  73. if content['mailbox_verification'] == 'unknown':
  74. raise UserError(
  75. _("%s couldn't be verified. Either the request couln't be "
  76. "completed or the mailbox provider doesn't support "
  77. "email verification") % (partner.email))
  78. @api.multi
  79. def check_email_bounced(self):
  80. """
  81. Checks if the partner's email is in Mailgun's bounces list
  82. API documentation:
  83. https://documentation.mailgun.com/en/latest/api-suppressions.html
  84. """
  85. api_key, api_url, domain, validation_key = self.env[
  86. 'mail.tracking.email']._mailgun_values()
  87. for partner in self:
  88. res = requests.get(
  89. '%s/%s/bounces/%s' % (api_url, domain, partner.email),
  90. auth=("api", api_key))
  91. if res.status_code == 200 and not partner.email_bounced:
  92. partner.email_bounced = True
  93. elif res.status_code == 404 and partner.email_bounced:
  94. partner.email_bounced = False
  95. @api.multi
  96. def force_set_bounced(self):
  97. """
  98. Forces partner's email into Mailgun's bounces list
  99. API documentation:
  100. https://documentation.mailgun.com/en/latest/api-suppressions.html
  101. """
  102. api_key, api_url, domain, validation_key = self.env[
  103. 'mail.tracking.email']._mailgun_values()
  104. for partner in self:
  105. res = requests.post(
  106. '%s/%s/bounces' % (api_url, domain),
  107. auth=("api", api_key),
  108. data={'address': partner.email})
  109. partner.email_bounced = (
  110. res.status_code == 200 and not partner.email_bounced)
  111. @api.multi
  112. def force_unset_bounced(self):
  113. """
  114. Forces partner's email deletion from Mailgun's bounces list
  115. API documentation:
  116. https://documentation.mailgun.com/en/latest/api-suppressions.html
  117. """
  118. api_key, api_url, domain, validation_key = self.env[
  119. 'mail.tracking.email']._mailgun_values()
  120. for partner in self:
  121. res = requests.delete(
  122. '%s/%s/bounces/%s' % (api_url, domain, partner.email),
  123. auth=("api", api_key))
  124. if res.status_code in (200, 404) and partner.email_bounced:
  125. partner.email_bounced = False