This question has been flagged
5 Replies
13519 Views

Hello,

I'm developing a module to extend Invoice (Odoo 11). I have added a binary field into the Invoice model, and I want to be able to send this binary via mail.

Sending an e-mail directly and attaching a binary is simple, but I want to open the composer wizard with the binary as an attachment. Currently, the "Send by e-mail" button opens the composer with Odoo's default PDF report; I want to add another file in that view.

This is my current code to try to attach the files. This piece of code itself runs and doesn't generate any errors, but the composer wizard just ignores the added info.

@api.multi
def action_invoice_sent(self):
self.ensure_one()

result = super(AccountInvoice,self).action_invoice_sent()

pdf_attachment_id = self.env['ir.attachment'].create({
'name': ("%s" %self.pdf),
'datas': self.pdfname,
'datas_fname': self.pdf,
'res_model': 'mail.compose.message',
'res_id': 0,
'type': 'binary'
})

result['context'].update({
'attachment_ids': [(6,0,[pdf_attachment_id.id])]
})

return result

Thanks a lot!
Avatar
Discard
Author Best Answer

Thanks but that's not my question. I don't want to attach a PDF report, the default invoice's e-mail template already does that. I want to add *another* file in the e-mail

I already solved it by extending the Composer wizard to retrieve the added files in context, but thanks anyway.

Avatar
Discard
Best Answer

Hugo M.

Firstly you have to check that the attachment you are sending is in pdf format or not, if its not then convert it using get_pdf method.

here is the example of attached report,

@api.multi
def action_rfq_send(self):
    self.ensure_one()
    ir_model_data = self.env['ir.model.data']
    try:
        if self.env.context.get('send_rfq', False):
            temp_id = self.env.ref('purchase.email_template_edi_purchase')
        else:
            temp_id = self.env.ref(
                               'purchase.email_template_edi_purchase_done')
    except ValueError:
        temp_id = False
    try:
        compose_form_id = ir_model_data.get_object_reference('mail',
                         'email_compose_message_wizard_form')[1]
    except ValueError:
        compose_form_id = False

    attach_obj = self.env['ir.attachment']

    pdf_rfq_quote = self.env['report'].sudo().get_pdf([self.id],
                                             'report name')
    result_rfq_quote = base64.b64encode(pdf_rfq_quote)

    attachment_ids = []
    if result:
        attach_data = {
            'name': 'name.pdf',
            'datas': result_rfq_quote,
            'datas_fname': 'name.pdf',
            'res_model': 'ir.ui.view',
            }
        attach_id = attach_obj.create(attach_data)
        attachment_ids.append(attach_id.id)
    if attachment_ids:
        temp_id.write({'attachment_ids': [(6, 0, attachment_ids)]})

    ctx = dict(self.env.context or {})
        ctx.update({
            'default_model': 'purchase.order',
            'default_res_id': self.ids[0],
            'default_use_template': bool(temp_id.id),
            'default_template_id': temp_id.id,
            'default_composition_mode': 'comment',
        })
        return {
            'name': _('Compose Email'),
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'mail.compose.message',
            'views': [(compose_form_id, 'form')],
            'view_id': compose_form_id,
            'target': 'new',
            'context': ctx,
        }
       

Thank You

Avatar
Discard
Best Answer

it works with pdf file ?


Avatar
Discard
Best Answer
Thanks but that's not my question. I don't want to attach a PDF report, the default invoice's e-mail template already does that. I want to add *another* file in the e-mail
I already solved it by extending the Composer wizard to retrieve the added files in context, but thanks anyway.


I want to do the same, how to solve this?

Thank you.
Avatar
Discard
Best Answer

Hello, this is my implementation using odoo email wizard:

def send_mail(self):
self.ensure_one()
template = self.env.ref('my_module.email_template_payroll', False)
compose_form = self.env.ref('mail.email_compose_message_wizard_form', False)

# attach xml from binary field
if self.xml_data:
xml_name = '%s.xml' % self.name
attachment_ids = []
encoded_data = base64.b64encode(self.xml_data)
decoded_data = base64.b64decode(encoded_data)

if decoded_data:
attach_data = {
'name': xml_name,
'res_name': xml_name,
'datas': decoded_data,
'res_model': 'hr.payslip',
'res_id': self.id,
}
attach_id = self.env['ir.attachment'].create(attach_data)
attachment_ids.append(attach_id.id)

if attachment_ids:
template.write({'attachment_ids': [(6, 0, attachment_ids)]})

ctx = dict()
ctx.update({
'default_model': 'hr.payslip',
'default_res_id': self.id,
'default_use_template': bool(template),
'default_template_id': template.id,
'default_composition_mode': 'comment',
})


return {
'name': _('Compose Email'),
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'mail.compose.message',
'views': [(compose_form.id, 'form')],
'view_id': compose_form.id,
'target': 'new',
'context': ctx,
}

Be careful with the indentation of the code, it got lost when paste text.

Hope it helps


Avatar
Discard

xml_data a existing field ?

"xml_data" field is a binary stored field in the same model of your method "send_mail". not remember if its a native odoo field or a custom one.

it works with pdf file ?

I think should work too, pdf files are stored by odoo in binary fields as well.

i have this error

File "/home/ibs/Bureau/workspace/odoo13/odoo/api.py", line 390, in call_kw
result = _call_kw_multi(method, model, args, kwargs)
File "/home/ibs/Bureau/workspace/odoo13/odoo/api.py", line 377, in _call_kw_multi
result = method(recs, *args, **kwargs)
File "/home/ibs/Bureau/workspace/odoo13/test_erp/spc_intranet_sfr/models/hs_commande.py", line 278, in action_commande_send
encoded_data = base64.b64encode(self.attachment_access_adsl)
File "/usr/lib/python3.8/base64.py", line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'ir.attachment'

This is the function
def action_commande_send(self):
self.ensure_one()
template = self.env.ref('spc_intranet_sfr.email_template_hs_commande', False)
compose_form = self.env.ref('mail.email_compose_message_wizard_form', False)

if self.attachment_access_adsl:
pdf_name = '%s.pdf' % self.name
attachment_ids = []
encoded_data = base64.b64encode(self.attachment_access_adsl)
decoded_data = base64.b64decode(encoded_data)

if decoded_data:
attach_data = {
'name': pdf_name,
'res_name': pdf_name,
'datas': decoded_data,
'res_model': 'hs.commande',
'res_id': self.id,
}
attach_id = self.env['ir.attachment'].create(attach_data)
attachment_ids.append(attach_id.id)

if attachment_ids:
template.write({'attachment_ids': [(6, 0, attachment_ids)]})

ctx = dict()
ctx.update({
'default_model': 'hs.commande',
'default_res_id': self.id,
'default_use_template': bool(template),
'default_template_id': template.id,
'default_composition_mode': 'comment',
})
return {
'name': _('Compose Email'),
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'mail.compose.message',
'views': [(compose_form.id, 'form')],
'view_id': compose_form.id,
'target': 'new',
'context': ctx,
}

The error is in this line:
encoded_data = base64.b64encode(self.attachment_access_adsl)

I think you are passing the full model or class object, but you need to pass the binary field instead, for example:

encoded_data = base64.b64encode(self.attachment_access_adsl.bin_field_name)

bin_field_name ?!!!!!!

base64.b64encode(*)

This method expects binary data as a parameter, you are passing an instance object of the class "ir.attachment".

what you need to pass is the binary field of the class "ir.attachment". In odoo 15 the binary field name is "datas"