diff --git a/mail_tracking/README.rst b/mail_tracking/README.rst index 5148fa216..427577299 100644 --- a/mail_tracking/README.rst +++ b/mail_tracking/README.rst @@ -43,6 +43,15 @@ you need to add ``mail_tracking`` addon to wide load addons list (by default, only ``web`` addon), setting ``--load`` option. For example, ``--load=web,mail_tracking`` +Configuration +============= + +As there can be scenarios where sending a tracking img in the email body is +not desired, there is a global system parameter +"mail_tracking.tracking_img_disabled" that can be set to True to remove +the tracking img from all outgoing emails. Note that the **Opened** status +will not be available in this case. + Usage ===== diff --git a/mail_tracking/models/ir_mail_server.py b/mail_tracking/models/ir_mail_server.py index 7d4ce7552..7e6f7ae3a 100644 --- a/mail_tracking/models/ir_mail_server.py +++ b/mail_tracking/models/ir_mail_server.py @@ -23,6 +23,21 @@ class IrMailServer(models.Model): match = re.search(r']*data-odoo-tracking-email=["\']([0-9]*)["\']', body) return str(match.group(1)) if match and match.group(1) else False + def _tracking_img_disabled(self, tracking_email_id): + # while tracking_email_id is not needed in this implementation, it can + # be useful for other addons extending this function to make a more + # fine-grained decision + return ( + self.env["ir.config_parameter"] + .sudo() + .get_param("mail_tracking.tracking_img_disabled", False) + ) + + def _tracking_img_remove(self, body): + return re.sub( + r']*data-odoo-tracking-email=["\'][0-9]*["\'][^>]*>', "", body + ) + def build_email( self, email_from, @@ -41,9 +56,28 @@ class IrMailServer(models.Model): body_alternative=None, subtype_alternative="plain", ): + # Unfortunately we currently have to extract the mail.tracking.email + # record id from the tracking image in the body here as the core + # mail module does not allow headers to be inserted in the + # MailMail._send_prepare_values function. + # Things to consider before refactoring this: + # - There are third party modules completely replacing the + # MailMail._send function, so even when a future version + # of the core mail module supports adding headers there, we might + # want to wait a little until this feature has trickled through. + # - While it would be possible to find the mail.tracking.email + # record via the message_id and the email_to criteria, this + # would rely on the message having no duplicate recipient + # (e.g. different contacts having the same email address) and + # no other module inheriting the _send_prepare_values function + # modifying the email_to parameter. tracking_email_id = self._tracking_email_id_body_get(body) if tracking_email_id: headers = self._tracking_headers_add(tracking_email_id, headers) + # Only after the X-Odoo-MailTracking-ID header is set we can remove + # the tracking image in case it's to be disabled + if self._tracking_img_disabled(tracking_email_id): + body = self._tracking_img_remove(body) msg = super(IrMailServer, self).build_email( email_from=email_from, email_to=email_to, diff --git a/mail_tracking/models/mail_mail.py b/mail_tracking/models/mail_mail.py index 5df9eedfe..a286a055a 100644 --- a/mail_tracking/models/mail_mail.py +++ b/mail_tracking/models/mail_mail.py @@ -30,7 +30,11 @@ class MailMail(models.Model): def _send_prepare_values(self, partner=None): """Creates the mail.tracking.email record and adds the image tracking - to the email""" + to the email. Please note that because we can't add mail headers in this + function, the added tracking image will later (IrMailServer.build_email) + also be used to extract the mail.tracking.email record id and to set the + X-Odoo-MailTracking-ID header there. + """ email = super()._send_prepare_values(partner=partner) vals = self._tracking_email_prepare(partner, email) tracking_email = self.env["mail.tracking.email"].sudo().create(vals) diff --git a/mail_tracking/readme/CONFIGURE.rst b/mail_tracking/readme/CONFIGURE.rst new file mode 100644 index 000000000..ab2f79def --- /dev/null +++ b/mail_tracking/readme/CONFIGURE.rst @@ -0,0 +1,5 @@ +As there can be scenarios where sending a tracking img in the email body is +not desired, there is a global system parameter +"mail_tracking.tracking_img_disabled" that can be set to True to remove +the tracking img from all outgoing emails. Note that the **Opened** status +will not be available in this case. diff --git a/mail_tracking/static/description/index.html b/mail_tracking/static/description/index.html index 92681148a..be0a5fc43 100644 --- a/mail_tracking/static/description/index.html +++ b/mail_tracking/static/description/index.html @@ -3,7 +3,7 @@ - + Email tracking