This question has been flagged
2 Replies
12135 Views

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







Avatar
Discard
Best Answer

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

Avatar
Discard
Best Answer

in v12 better to use 

template_id.send() instead of template_obj.send(template_id) in order to instantly send the e-mail
Avatar
Discard