Odoo Help

0

Force sender email for all platform mails

By
Atl Sup
on 11/17/15, 10:08 AM 1,267 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.

0
Victor Lopes
On 11/23/17, 9:15 AM

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.

 

0

Hi,

I have the same issue, any solution?

Thanks!

0
Atl Sup
On 12/8/15, 4:50 AM

Has anyone some more information about this issue?

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

Asked: 11/17/15, 10:08 AM
Seen: 1267 times
Last updated: 11/23/17, 1:03 PM