This question has been flagged
7 Replies
18873 Views

Hi!

I would like to force a special e-mail address as sender for every e-mail that will be sent from Odoo.

For instance, notifications emails for waiting for approval timesheets are sent from the user's mail. Because of our mail server limitations, those mails aren't sent (they appear in red in mails list in Odoo admin panel). How to force sending all those mails from one single e-mail address?

I tried editing "Notification Email" template ("From" field), but this does not work.


Thank you for your help.

Avatar
Discard
Best Answer

Hi there!


I know this is an old post, but since there are still people interested in this, I had the same issue recently.

Odoo is in general prepared to send e-mails using only the "author's" address as sender (From), and then use a "catchall" reply-to address in order to route back the answers (when someone replies to its notifications).

The fact that you apparently can't configure Odoo to use a fixed sender for all outbound e-mails is kind of unreasonable, to say least. Because e-mail servers like Microsoft Exchange will, by default, only allow users to send through their own accounts. Not to mention that a fixed sender will be a much more simple solution and it would be nice to have a choice in this matter. But, as a community user I'm so happy that Odoo is available open source that this won't bother me much.


So as of now you have basically two options:


  1.  Configure you mail server to allow relay from your Odoo server. On Exchange Server, for example, this can be done by adding a custom Receive Connector with "Anonymous / Externally secured" authentication. You can find out more here: https://technet.microsoft.com/en-us/library/mt668454%28v=exchg.160%29.aspx?f=255&MSPPError=-2147217396  and  here: https://practical365.com/exchange-server/how-to-configure-a-relay-connector-for-exchange-server-2010/

  2. Modify the mail_mail.py and the ir_mail_server.py modules in Odoo in order to hardcode a fixed e-mail address that will be used for all outbound e-mail messages, which was the most effective solution for me.


So, for this second option, here is how to do it on a Odoo 10 installation:


A little background


If you try to send replies from within your Odoo records (Leads, Purchase orders, etc.) and your mail server doesn't allow relay, you will see messages like these in your logs (/var/log/odoo  - file: odoo-server):

2017-11-23 11:54:56,597 3848 odoo.addons.base.ir.ir_mail_server: Mail delivery failed via SMTP server 'YOURMAILSERVER'.
SMTPDataError: 550
5.7.1 Client does not have permissions to send as this sender
2017-11-23 11:54:56,598 3848 ERROR odoo.addons.mail.models.mail_mail: failed sending mail (id: 16) due to Mail Delivery Failed
Mail delivery failed via SMTP server 'YOURMAILSERVER'.
SMTPDataError: 550
5.7.1 Client does not have permissions to send as this sender
Traceback (most recent call last):
  File "/odoo/odoo-server/addons/mail/models/mail_mail.py", line 278, in send
    res = IrMailServer.send_email(msg, mail_server_id=mail.mail_server_id.id)
  File "/odoo/odoo-server/odoo/addons/base/ir/ir_mail_server.py", line 464, in send_email
    raise MailDeliveryException(_("Mail Delivery Failed"), msg)


The changes


Alter the file /odoo/odoo-server/addons/mail/models/mail_mail.py:


Inside the function "send" (look for "def send(" ), around line 278, you will find this block:


                # build an RFC2822 email.message.Message object and send it without queuing 
                res = None
                for email in email_list:
                    msg = IrMailServer.build_email(
                        email_from=mail.email_from,
                        email_to=email.get('email_to'),
                        subject=mail.subject,
                        [...some other stuff...]
                        headers=headers)
                    try:
                        res = IrMailServer.send_email(msg, mail_server_id=mail.mail_server_id.id)


Change the line email_from=mail.email_from . Replace "mail.email_from" by an e-mail address of your choice, like 'contact@mycompany.com'. You can even use a more fancy formatting here, to include a sender name, like 'My Sales Department <sales@mycompany.com>'.

It should look like this: email_from='My Company <contact@mycompany.com>'

Don't forget the single quotes.


Then, alter the file /odoo/odoo-server/odoo/addons/base/ir/ir_mail_server.py


Inside the function "send_email" (look for "def send_email"), around line 396 you will find:


        smtp_from = from_rfc2822[-1]
        email_to = message['To']
        email_cc = message['Cc']
        email_bcc = message['Bcc']


Change smtp_from = from_rfc2822[-1] . Replace "from_rfc2822[-1]" by your address. Here you have to use just the e-mail address, like 'contact@mycompany.com'  . Don't include < >, spaces nor any non-ASCII characters.

It should look like: smtp_from = 'contact@mycompany.com'


That's it. After saving these files, restart Odoo's service (sudo service odoo-server restart) and try again.

This was tested on an Odoo 10 installation, on a Ubuntu Server 16.04 (Xenial).  I have installed it using this great script from Yenthe: https://www.odoo.yenthevg.com/installing-odoo-10-ubuntu-16-04


Some final considerations:

  • While editing these python files, don't mess up with the indentation. Python has specific text indentation requirements. 
    More on this: https://stackoverflow.com/questions/1016814/what-to-do-with-unexpected-indent-in-python

  • Don't forget to set the "Alias Domain" and to configure the "catchall" address. Please take a look at these instructions: https://www.odoo.com/documentation/user/11.0/discuss/email_servers.html
    If you don't set the Alias Domain, it's likely that Odoo will populate the "reply-to" field with the author's e-mail, and this will lead to an even bigger mess.

  • If you choose the first option (configuring relay on your mail server), don't forget that you can't send e-mails using sender domains that you don't own. I mean, you should only allow your internal users to use Odoo with your own e-mail accounts. Don't let anyone use addresses like "mypersonal@gmail.com", because this will cause your mail server to try sending messages as it owned the "gmail.com" domain, whenever a user like this reply on a record, for example.


I hope this helps somebody.

Cheers,


Victor Lopes.

 

Avatar
Discard
Best Answer

So, you could also make an automated action (Technical - Automated Actions) similar to this:

Just change the model to what you need:


Avatar
Discard
Author Best Answer

Has anyone some more information about this issue?

Avatar
Discard
Best Answer

Hi,

I have the same issue, any solution?

Thanks!

Avatar
Discard
Best Answer

In addition to Victor Lopes comment.

Odoo v12

One additional line to change in the mail_message.py file

In line 31 in the def _get_default_from 

change the line return formataddr((self.env.user.name, self.env.user.email))
to return formataddr((self.env.user.name, 'Insert email address'))

Avatar
Discard
Best Answer

When you don't have manager rights on the email server, what you could do is to inherit Odoo's send_email() method in order to force all the email parameters to be the same as the email login user.

I was having this message

SMTPDataError: 550
5.7.1 Client does not have permissions to send as this sender

This is the code for Odoo 11

class IrMailServer(models.Model):
_inherit = "ir.mail_server"

@api.model
def send_email(self, message, mail_server_id=None, smtp_server=None, smtp_port=None,
smtp_user=None, smtp_password=None, smtp_encryption=None, smtp_debug=False,
smtp_session=None):

""" Force 'Return-Path' and 'Form' email parameters with smtp_user data,
Message error:
SMTPDataError: 550
5.7.1 Client does not have permissions to send as this sender
"""

# Get SMTP Server Details from Mail Server
mail_server = None
if mail_server_id:
mail_server = self.sudo().browse(mail_server_id)
elif not smtp_server:
mail_server_ids = self.sudo().search([], order='sequence', limit=1)
if mail_server_ids:
mail_server = mail_server_ids[0]

# if mail_serveris set, take smtp_user
if mail_server:
smtp_user = mail_server.smtp_user
if '@' not in smtp_user and mail_server.smtp_host:
# If smtp_user doesn't fit an email pattern, take domain from smtp_host
smtp_user += '@' + mail_server.smtp_host.partition('.')[2]

# Change email parameters
if 'Return-Path' in message:
message.replace_header('Return-Path', smtp_user)
else:
message['Return-Path'] = smtp_user

if 'From' in message:
message.replace_header('From', smtp_user)
else:
message['From'] = smtp_user

# Super to send_email()
res = super(IrMailServer, self).send_email(message, mail_server_id, smtp_server, smtp_port,
smtp_user, smtp_password, smtp_encryption, smtp_debug, smtp_session)
return res

It worked for me. I hope it helps.


Avatar
Discard
Best Answer

@Victor Lopes

Amazing work ! I've been stuck with this problem from days. It resolved it in minutes with your Option 2 ! Still work on Odoo V12

Tank you !

Avatar
Discard