1. 程式人生 > >Odoo 郵件系統設定 Odoo 郵件傳送失敗原始碼分析

Odoo 郵件系統設定 Odoo 郵件傳送失敗原始碼分析

Odoo的郵件跟蹤功能是做的非常好的, 可以通過系統郵件,當客戶回覆郵件之後,系統會自動將郵件內容作為訊息與原有記錄進行對應。
很多初學者,傳送郵件時常常遇到如下錯誤。
郵件投遞失敗
通過SMTP傳送郵件失敗 'None'.
SMTPSenderRefused: 501
mail from address must be same as authorization user
[email protected]
為什麼傳送地址會變為"[email protected]"呢?
我們來分析原始碼,參見ir_mail_server.py中send_mail 方法:
smtp_from = message['Return-Path'
] or self._get_default_bounce_address() or message['From'] assert smtp_from, "The Return-Path or From header is required for any outbound email"
我們看到 smtp_from 的獲取值的先後順序,其中最後一個值message['From'],指的是當前使用者的郵箱。如果我們要用當前使用者的郵箱傳送郵件,是不太好的, 因為,我們就需要為每個使用者設定一個傳送郵件的賬號。
message['Return-Path'] 來源自mail_mail.py 中_send 方法,程式碼如下:

# headers
headers = {}
ICP = self.env['ir.config_parameter'].sudo()
bounce_alias = ICP.get_param("mail.bounce.alias")
catchall_domain = ICP.get_param("mail.catchall.domain")
if bounce_alias and catchall_domain:
    if mail.model and mail.res_id:
        headers['Return-Path'] = '%s+%d-%s-%[email protected]
%s'
% (bounce_alias, mail.id, mail.model, mail.res_id, catchall_domain) else: headers['Return-Path'] = '%s+%[email protected]%s' % (bounce_alias, mail.id, catchall_domain) if mail.headers: try: headers.update(safe_eval(mail.headers)) except Exception: pass
從上述程式碼可以看出,icp.config_paramerter,中存在 bounce_alias,catchall_domain ,就會生成 Return-Path 的值
self._get_default_bounce_address()來源自ir_mail_server.py 中_get_default_bounce_address方法:
@api.model
def _get_default_bounce_address(self):
    '''Compute the default bounce address.

    The default bounce address is used to set the envelop address if no
    envelop address is provided in the message.  It is formed by properly
    joining the parameters "mail.bounce.alias" and
    "mail.catchall.domain".

    If "mail.bounce.alias" is not set it defaults to "postmaster-odoo".

    If "mail.catchall.domain" is not set, return None.

    '''
    get_param = self.env['ir.config_parameter'].sudo().get_param
    postmaster = get_param('mail.bounce.alias', default='postmaster-odoo')
    domain = get_param('mail.catchall.domain')
    if postmaster and domain:
        return '%[email protected]%s' % (postmaster, domain)
從以上可以得知 _get_default_bounce_address 傳送郵件名來源於 mail.bounce.alias ,預設值是postmaster-odoo.
根據以上程式碼的值,不刪除mail.bounce.alias的值,會導致傳送地址錯誤,刪除mail.bounce.alias的值,導致預設傳送郵箱名是postmaster-odoo.
綜上所述,在不修改程式碼的情況,我們需要如何做才能正常收發郵件呢?1.設定自己公司的企業郵箱,我使用的是免費的騰訊企業郵箱。2.在自己企業內部郵箱中設定[email protected] ,[email protected](專門用於接收郵件) 等2個賬號。3.在Odoo 系統中設定SMTP 伺服器,使用者名稱必須為[email protected]4.在Odoo 系統中設定POP3伺服器,使用者名稱必須為[email protected].5.在Odoo系統中設定郵箱域名,對應xxx.com.6.為每個使用者設定一個郵箱,否則傳送郵件時,會報錯,要求你設定郵箱。 這個郵箱也可以使用者自己在使用者資料中設定更改。最後一步,也是最重要的,在系統引數中刪除 mail.bounce.alias 這條記錄就可以了,否則,前功盡棄。

歡迎大家討論,參考。轉載,請註明出處!

來自:https://www.odooqs.com/blog/1/post/odoo-5