Skip to Content
Menú
This question has been flagged
1 Respondre
3048 Vistes

Hello,

I want to send an attachment PDF with email using scheduling action my function work well, but the report was messed

here is my function:

for record in invoices:

recipient_lists =[]
if record.partner_id:
recipient_lists.append(record\.partner_id\.id\)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ template\ =\ self\.env\.ref\('account\.email_template_edi_invoice',\ False\)

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ mail_template\ =\ self\.env\['mail\.template'\]\.browse\(template\.id\)

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ values\ =\ mail_template\.generate_email\(record\.id,\ fields=None\)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ values\['recipient_ids'\]\ =\ \[\(6,0,\[recipient_lists\]\)\]

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ values\['res_id'\]\ =\ record\.id
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ attachment_ids\ =\ values\.pop\('attachment_ids',\ \[\]\)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ attachments\ =\ values\.pop\('attachments',\ \[\]\)


\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ mail_create\ =\ self\.env\['mail\.mail'\]\.create\(values\)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Attachment\ =\ self\.env\['ir\.attachment'\]

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ for\ attachment\ in\ attachments:
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ attachment_data\ =\ \{
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 'name':\ attachment\[0\],
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 'datas_fname':\ attachment\[0\],
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 'datas':\ attachment\[1\],
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 'type':\ 'binary',
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 'res_model':\ 'mail\.message',
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 'res_id':\ mail_create.mail_message_id.id,
}
attachment_ids.append(Attachment.create(attachment_data).id)

values['attachment_ids'] = [(6, 0, attachment_ids)]
mail_create.write({'email_from':record.company_id.subscription_email,'attachment_ids': [(6, 0, attachment_ids)]})

if mail_create:
mail_create.send()
record.the_main_sent = True
Avatar
Descartar
Best Answer

Hi 

Please try like following

if attachment in pdf format 

 ir_actions_report = self.env['ir.actions.report.xml']
report = ir_actions_report.search([('report_name', '=', 'Name of the report')])
if report:
result, format = ir_actions_report.render_report(data,report.report_name,)
attachment_data = {
'name': file_name,
'datas'
: base64.b64encode(result),
'res_model'
: 'model.model',
'type'
: 'binary',
}
att_id = self.env['ir.attachment'].create(attachment_data)
if att_id:

values['attachment_ids'] = [(6, 0, [att_id.id])]
values = {
'email_to': partner.email,
'body_html': '<pre>%s</pre>' % (_(WELCOME_EMAIL_BODY) % email_data),
'subject': 'Mail Subject',
'auto_delete': False,
'message_type': 'email',
'email_cc': 'email' or ''
}


msg_id = self.env['mail.mail'].create(values)
ans = msg_id.send()


Hope this helps


Regards

Avatar
Descartar
Related Posts Respostes Vistes Activitat
2
de jul. 25
5893
2
d’oct. 25
8493
2
de nov. 24
29348
2
de maig 24
8138
3
de març 24
7554