2019-05-24 12:42:58 +02:00
|
|
|
# Copyright 2017 LasLabs Inc.
|
|
|
|
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
|
|
|
|
|
|
|
|
from odoo import api, fields, models
|
|
|
|
|
|
|
|
|
|
|
|
class IrMailServer(models.Model):
|
|
|
|
|
2020-06-30 16:21:11 +02:00
|
|
|
_inherit = "ir.mail_server"
|
2019-05-24 12:42:58 +02:00
|
|
|
|
|
|
|
smtp_from = fields.Char(
|
2020-06-30 16:21:11 +02:00
|
|
|
string="Email From", help="Set this in order to email from a specific address."
|
2019-05-24 12:42:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
@api.model
|
2020-06-30 16:21:11 +02:00
|
|
|
def send_email(
|
|
|
|
self, message, mail_server_id=None, smtp_server=None, *args, **kwargs
|
|
|
|
):
|
2019-05-24 12:42:58 +02:00
|
|
|
|
|
|
|
# Replicate logic from core to get mail server
|
|
|
|
mail_server = None
|
|
|
|
if mail_server_id:
|
|
|
|
mail_server = self.sudo().browse(mail_server_id)
|
|
|
|
elif not smtp_server:
|
2020-06-30 16:21:11 +02:00
|
|
|
mail_server = self.sudo().search([], order="sequence", limit=1)
|
2019-05-24 12:42:58 +02:00
|
|
|
|
|
|
|
if mail_server and mail_server.smtp_from:
|
2020-06-30 16:21:11 +02:00
|
|
|
split_from = message["From"].rsplit(" <", 1)
|
2019-05-24 12:42:58 +02:00
|
|
|
if len(split_from) > 1:
|
2020-06-30 16:21:11 +02:00
|
|
|
email_from = "{} <{}>".format(split_from[0], mail_server.smtp_from)
|
2019-05-24 12:42:58 +02:00
|
|
|
else:
|
|
|
|
email_from = mail_server.smtp_from
|
|
|
|
|
2020-06-30 16:21:11 +02:00
|
|
|
message.replace_header("From", email_from)
|
2020-07-22 12:26:59 +02:00
|
|
|
bounce_alias = (
|
|
|
|
self.env["ir.config_parameter"].sudo().get_param("mail.bounce.alias")
|
2020-06-30 16:21:11 +02:00
|
|
|
)
|
2019-05-24 12:42:58 +02:00
|
|
|
if not bounce_alias:
|
|
|
|
# then, bounce handling is disabled and we want
|
|
|
|
# Return-Path = From
|
2020-06-30 16:21:11 +02:00
|
|
|
if "Return-Path" in message:
|
|
|
|
message.replace_header("Return-Path", email_from)
|
2019-05-24 12:42:58 +02:00
|
|
|
else:
|
2020-06-30 16:21:11 +02:00
|
|
|
message.add_header("Return-Path", email_from)
|
2019-05-24 12:42:58 +02:00
|
|
|
|
|
|
|
return super(IrMailServer, self).send_email(
|
|
|
|
message, mail_server_id, smtp_server, *args, **kwargs
|
|
|
|
)
|