From 2d19c4c6b88ec75c3994af3a13ce467bb9d8e9f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Tue, 10 Apr 2018 14:00:14 +0200 Subject: [PATCH] [FIX] mail: raise a missing email error instead of crash when logging without email Currently when posting a message if the author has no email address an user error is raised. Indeed as posting a message goes through the process of creating notifications and sending emails an email address is required on the user. In saas 11.2 a _message_log shortcut has been added to log notes. This method should have the same behavior when author has no email. Currently there is a crash when trying to encode its email. After this commit an exceptions is raised like classic post. This commit is related to task ID 1823990 . --- addons/mail/models/mail_message.py | 2 +- addons/mail/models/mail_thread.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py index 3c17f207ab3..89e4237c8e1 100644 --- a/addons/mail/models/mail_message.py +++ b/addons/mail/models/mail_message.py @@ -28,7 +28,7 @@ class Message(models.Model): def _get_default_from(self): if self.env.user.email: return formataddr((self.env.user.name, self.env.user.email)) - raise UserError(_("Unable to send email, please configure the sender's email address.")) + raise UserError(_("Unable to post message, please configure the sender's email address.")) @api.model def _get_default_author(self): diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 73b2388329b..66dfcd84f8e 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -1998,16 +1998,16 @@ class MailThread(models.AbstractModel): kw_author = kwargs.pop('author_id', False) if kw_author: author = self.env['res.partner'].sudo().browse(kw_author) - email_from = formataddr((author.name, author.email)) else: author = self.env.user.partner_id - email_from = formataddr((author.name, author.email)) + if not author.email: + raise exceptions.UserError(_("Unable to log message, please configure the sender's email address.")) message_values = { 'subject': subject, 'body': body, 'author_id': author.id, - 'email_from': email_from, + 'email_from': formataddr((author.name, author.email)), 'message_type': message_type, 'model': kwargs.get('model', self._name), 'res_id': self.ids[0] if self.ids else False,