Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
e-Commerce
Accounting
Inventory
Project management
Take the tour
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!
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
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.
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
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,
}
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign up
Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.
Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.