Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
14122 Lượt xem

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







Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

in v12 better to use 

template_id.send() instead of template_obj.send(template_id) in order to instantly send the e-mail
Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 1 25
8416
1
thg 6 17
8828
1
thg 7 25
620
5
thg 5 25
18033
0
thg 4 25
1025