Перейти к содержимому
Меню
Чтобы взаимодействовать с сообществом, необходимо зарегистрироваться.
Этот вопрос был отмечен
1 Ответить
200 Представления

Hi guys,

I was writing a small module on 18.0 EE essentially it goes to send an email with a custom receipt. The print function from the record creates the pdf correctly, but I can't create the ir.attachment in any way, I've tried several times creating the attachment first and adding it to the email with 6.0,receipt_attach_id but nothing. It always continues to give me an error when sending email on the lines of attachemnt id 259 user id 2 not found etc. If you could help me that would be perfect, thanks!


<record id="action_report_owner_receipt" model="ir.actions.report">
        <field name="name">Ricevuta Soggiorno</field>
        <field name="model">pms.reservation</field>
        <field name="report_type">qweb-pdf</field>
        <field name="report_name">property_management.report_owner_receipt</field>
        <field name="report_file">property_management.report_owner_receipt</field>
        <field name="paperformat_id" ref="paperformat_owner_receipt"/>
        <field name="binding_model_id" ref="model_ciao_reservation"/>
        <field name="binding_type">report</field>
        <field name="print_report_name">'Ricevuta - %s' % (object.ota_res_id or object.pms_res_id or '')</field>
    </record>
report_action = self.env.ref('property_management.action_report_owner_receipt')
pdf_content = report_action._render_qweb_pdf(self.id)[0]

mail_values = {
            'subject': subject,
            'email_from': from_email,
            'email_to': self.guest_partner_id.email,
            'body_html': body_html,
            'auto_delete': False,
            'attachment_ids': [
                (0, 0, {
                    'name': f'Ricevuta_{self.ota_res_id}.pdf',
                    'datas': base64.b64encode(pdf_content),
                    'mimetype': 'application/pdf',
                })
            ]
        }
mail = self.env['mail.mail'].sudo().create(mail_values)
mail.send_mail(force_send=True, raise_exception=True)

Аватар
Отменить
Лучший ответ

Hii

Split attachment creation and email creation:

report_action = self.env.ref('property_management.action_report_owner_receipt') pdf_content = report_action._render_qweb_pdf(self.id)[0] # 1. Create the attachment first attachment = self.env['ir.attachment'].sudo().create({ 'name': f'Ricevuta_{self.ota_res_id}.pdf', 'type': 'binary', 'datas': base64.b64encode(pdf_content), 'res_model': self._name, 'res_id': self.id, 'mimetype': 'application/pdf', }) # 2. Create the email and link the attachment mail_values = { 'subject': f"Ricevuta - {self.ota_res_id}", 'email_from': from_email, 'email_to': self.guest_partner_id.email, 'body_html': body_html, 'auto_delete': False, 'attachment_ids': [(6, 0, [attachment.id])], } mail = self.env['mail.mail'].sudo().create(mail_values) mail.send_mail(force_send=True, raise_exception=True)


i hope it is use full

Аватар
Отменить
Related Posts Ответы Просмотры Активность
0
мар. 22
1272
1
авг. 24
1538
0
июн. 22
3182
0
нояб. 21
2871
1
авг. 21
5594