diff --git a/addons/mail/models/mail_mail.py b/addons/mail/models/mail_mail.py index d8abbd1f06f..23e154e83c9 100644 --- a/addons/mail/models/mail_mail.py +++ b/addons/mail/models/mail_mail.py @@ -6,6 +6,7 @@ import datetime import logging import psycopg2 import threading +import re from collections import defaultdict from email.utils import formataddr @@ -252,6 +253,7 @@ class MailMail(models.Model): @api.multi def _send(self, auto_commit=False, raise_exception=False, smtp_session=None): IrMailServer = self.env['ir.mail_server'] + IrAttachment = self.env['ir.attachment'] for mail_id in self.ids: success_pids = [] failure_type = 'NONE' @@ -263,11 +265,18 @@ class MailMail(models.Model): if mail.state != 'exception' and mail.auto_delete: mail.sudo().unlink() continue + + # remove attachments if user send the link with the access_token + body = mail.body_html or '' + attachments = mail.attachment_ids + for link in re.findall(r'/web/(?:content|image)/([0-9]+)', body): + attachments = attachments - IrAttachment.browse(int(link)) + # load attachment binary data with a separate read(), as prefetching all # `datas` (binary field) could bloat the browse cache, triggerring # soft/hard mem limits with temporary data. attachments = [(a['datas_fname'], base64.b64decode(a['datas']), a['mimetype']) - for a in mail.attachment_ids.sudo().read(['datas_fname', 'datas', 'mimetype'])] + for a in attachments.sudo().read(['datas_fname', 'datas', 'mimetype'])] # specific behavior to customize the send email for notified partners email_list = []