콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
2 답글
13845 화면

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
7981
1
6월 17
8538
5
5월 25
17201
0
4월 25
765
1
4월 25
999