第一步: 配置SMTP地址:設置 - 技術 - 電子郵件-發件服務器
點擊“測試連接”,出現下面提示即表示配置成功
第二步: 創建郵件內容模板,記得在__manifest__.py中添加引用。簡單的模塊內容如下:
<?xml version='1.0' encoding='UTF-8' ?> <odoo> <data noupdate="0"> <record id="send_msg_template" model="mail.template"> <field name="name">郵件提醒</field> <field name="email_from">xxx@xxx.com</field> <field name="subject">標題</field> <field name="model_id" ref="xxx.model_model_id"/> <field name="email_to">xxx@xxx.com</field> <field name="body_html" type="html"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"/> <title>內容標題</title> <style> span.oe_mail_footer_access { display:block; text-align:center; color:grey; } </style> </head> <body> <div style="border-radius: 2px; max-width: 1200px; height: auto;margin-left: auto;margin-right: auto;background-color:#f9f9f9;"> <div style="height:auto;text-align: center;font-size : 30px;color: #8A89BA;"> <strong>郵件內容</strong> </div> </div> </body> </html> </field> </record> </data> </odoo>
第三步:觸發,在重新create方法、onchange事件或其它方法中均可觸發。下例是在新建員工重寫create方法中觸發。
@api.model
def create(self, values):
new_employee = super(employee, self).create(values)
try:
mail_template = self.env.ref('xxx.send_msg_template')
mail_template.subject = u'xxx提醒 - %s' % new_employee.employeeName
mail_template.send_mail(self.id, force_send=True)
finally:
aa = ""
return new_employee
另外需要在 __manifest__.py 中引用mail類,如:
# any module necessary for this one to work correctly 'depends': ['base','base_setup','base_import','mail'],