Files
odoo_source/addons/mass_mailing/wizard/test_mailing.py
T
Olivier Dony ca989b6548 [FIX] mass_mailing: prevent duplicates and sending to opted-out
The mass-mailing App suffered from severe issues due to its inability
to detect and handle duplicates "at the email level", and the absence
of any global blacklist system, leading to lack of user trust.

Mailing-lists typically include multiple records with the same email,
and it is critical to avoid sending them the same email several times.
A related problem is the unsubscription of an email that is present
in other records (duplicates). Opting out the first email should
automatically blacklist it for other records as well.

Ideally we should have a global blacklist table in order to
share the unsubscription requests globally across models (Leads,
Partners, Mailing-list contacts). It would also allow importing
it from other blacklist systems. (TODO for master)

This commit introduces a partial solution, made of several
small changes:

- In mail.mail: double-check that an outgoing email has the
correct status (`outgoing`) before sending it. This allows
adding emails in the queue and cancelling them before they
actually get sent.
- In crm.lead: force predictable recipients for mass-mailing,
by always using the email of the lead rather than the email
of the linked partner when there is one. This simplifies the
computation of the blacklist and seen list. Other areas in
the codebase already assume as much.
- In mass.mailing:

 + Before sending out a mailing-list batch, compute the
   blacklist (all opted-out emails) and the seen_list
   (emails who previously received this mail) to make
   sure we only ever target valid recipients.
 + While delivering the mass-mailing, any email targeting
   an address that is in the blacklist or "seen list" is
   canceled before being sent. The corresponding statistics
   entry is considered "not delivered".
   Also updates the "seen list" continuously.
 + When a mass-mail belongs to a campaign with the
   "unique AB/B testing" flag, the "seen list" is
   common to the whole campaign, as an extra safety.
 + Auto-delete mass-mailing test messages sent with the
   test wizard, to avoid polluting the mail_mail table

Note: this fix uses a simple regex for efficiently extracting
the blacklist in pure SQL from different models, and doing so,
assumes that each record only holds a single email
(no comma-separated adresses). This should be sufficient for
most cases. The regex:

                   ([^ ,;<@]+@[^> ,;]+)
2017-03-27 18:47:59 +02:00

39 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models, tools
class TestMassMailing(models.TransientModel):
_name = 'mail.mass_mailing.test'
_description = 'Sample Mail Wizard'
email_to = fields.Char(string='Recipients', required=True,
help='Comma-separated list of email addresses.', default=lambda self: self.env['mail.message']._get_default_from())
mass_mailing_id = fields.Many2one('mail.mass_mailing', string='Mailing', required=True, ondelete='cascade')
@api.multi
def send_mail_test(self):
self.ensure_one()
mails = self.env['mail.mail']
mailing = self.mass_mailing_id
test_emails = tools.email_split(self.email_to)
for test_mail in test_emails:
# Convert links in absolute URLs before the application of the shortener
mailing.write({'body_html': self.env['mail.template']._replace_local_links(mailing.body_html)})
mail_values = {
'email_from': mailing.email_from,
'reply_to': mailing.reply_to,
'email_to': test_mail,
'subject': mailing.name,
'body_html': mailing.body_html,
'notification': True,
'mailing_id': mailing.id,
'attachment_ids': [(4, attachment.id) for attachment in mailing.attachment_ids],
'auto_delete': True,
}
mail = self.env['mail.mail'].create(mail_values)
mails |= mail
mails.send()
return True