[MERGE] forward port of branch 8.0 up to e883193
[odoo/odoo.git] / addons / mail / mail_mail.py
index c741953..5de69fb 100644 (file)
@@ -276,6 +276,15 @@ class mail_mail(osv.Model):
                     except Exception:
                         pass
 
+                # Writing on the mail object may fail (e.g. lock on user) which
+                # would trigger a rollback *after* actually sending the email.
+                # To avoid sending twice the same email, provoke the failure earlier
+                mail.write({
+                    'state': 'exception', 
+                    'failure_reason': _('Error without exception. Probably due do sending an email without computed recipients.'),
+                })
+                mail_sent = False
+
                 # build an RFC2822 email.message.Message object and send it without queuing
                 res = None
                 for email in email_list:
@@ -299,11 +308,8 @@ class mail_mail(osv.Model):
                                                     context=context)
 
                 if res:
-                    mail.write({'state': 'sent', 'message_id': res})
+                    mail.write({'state': 'sent', 'message_id': res, 'failure_reason': False})
                     mail_sent = True
-                else:
-                    mail.write({'state': 'exception', 'failure_reason': _('Error without exception. Probably due do sending an email without computed recipients.')})
-                    mail_sent = False
 
                 # /!\ can't use mail.state here, as mail.refresh() will cause an error
                 # see revid:odo@openerp.com-20120622152536-42b2s28lvdv3odyr in 6.1