[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 and in saas
11.3 a message_notify has been added to notify peopel of a custom content.
Those methods 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 a manual forward-port of 3a978cc64e6b1d8aca7cfd5d4e319bcb302dd262 as well as an improvement
to fix the newly-introduced message_notify method.
This commit is contained in:
Thibault Delavallée
2018-04-10 14:06:00 +02:00
parent 4859b642d8
commit 95d5020ff7
2 changed files with 7 additions and 7 deletions
+1 -1
View File
@@ -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):
+6 -6
View File
@@ -1973,16 +1973,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 notify message, please configure the sender's email address."))
msg_values = {
'subject': subject,
'body': body,
'author_id': author.id,
'email_from': email_from,
'email_from': formataddr((author.name, author.email)),
'message_type': 'notification',
'partner_ids': partner_ids,
'model': False,
@@ -2007,16 +2007,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,