跳至内容
菜单
此问题已终结
2 回复
13544 查看

Hi

I'm searching for a simple example in order to send an email via Python (example in a cronjob).

I searched and found dozens of pages but none with a valid solution


This is my code:

_cr = openerp.registry(self.config['db_name']).cursor()

env = openerp.api.Environment(_cr, openerp.SUPERUSER_ID, {})

mail_pool = env['mail.mail']
         
values={}
values.update({'subject': 'Your subject'})
values.update({'email_to': 'email_to@gmail.com'})
values.update({'body_html': 'body' })
values.update({'body': 'body' })
msg_id = mail_pool.create(values)
if msg_id:
   mail_pool.send([msg_id])

else:
    logger.error("Error")


It's not working because

IrMailServer = env['ir.mail_server']

in "core send" function is None (obviously I've already correctly set up an Outgoing Server)

Why???

Please, can you write a practical and working example?

Thank's

Pietro







形象
丢弃
最佳答案

Hi Setsu,

You can check the below code to send the mail from the python,

template_obj = self.env['mail.mail']
template_data = {
'subject': 'Due Invoice Notification : ' + current_date,
'body_html': message_body,
'email_from': sender,
'email_to': ", ".join(recipients)
}
template_id = template_obj.create(template_data)
template_obj.send(template_id)

Also give the mail as depends in manifest file

Thanks

形象
丢弃
最佳答案

in v12 better to use 

template_id.send() instead of template_obj.send(template_id) in order to instantly send the e-mail
形象
丢弃
相关帖文 回复 查看 活动
2
1月 25
7543
1
6月 17
8206
5
5月 25
16388
0
4月 25
364
1
4月 25
543