top of page

Odoo - Attach Excel in the Email Template

Writer's picture: Tek Siong, HockTek Siong, Hock

Updated: Jan 10, 2022

This Odoo technical development blog is my way to contribute back to the Odoo Community, for all the selfless and great sharing by the community members.


If you want to send the excel as an attachment in the email template, use the 'render_xlsx' method (similar to render_pdf) to create the excel. See the following example.


@api.multi

def action_send_booking_confirmation_si(self):

self.ensure_one()

ir_model_data = self.env['ir.model.data']

try:

template_id = \

ir_model_data.get_object_reference('sci_goexcel_freight', 'email_template_edi_booking_confirmation')[1]

except ValueError:

template_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

excel_template_id = self.env.ref('sci_goexcel_freight.action_si_report_xlsx').render_xlsx(self.ids,[])

data_record = base64.b64encode(excel_template_id[0])

file_name = 'SI ' + self.booking_no + '.xlsx'

ir_values = {

'name': "Shipping Instruction",

'type': 'binary',

'datas': data_record,

'datas_fname': file_name,

'store_fname': 'Shipping Instruction',

'mimetype': 'application/xlsx',

}

excel_attachment = self.env['ir.attachment'].create(ir_values)

template = self.env['mail.template'].browse(template_id)

template.attachment_ids = False

template.attachment_ids = [(4, excel_attachment.id)]

ctx = {

'default_model': 'freight.booking',

'default_res_id': self.ids[0],

'default_use_template': bool(template_id),

'default_template_id': template_id,

'default_composition_mode': 'comment',

'mark_so_as_sent': True,

'custom_layout': "mail.mail_notification_light",

# 'proforma': self.env.context.get('proforma', False),

'force_email': True

}

base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')

ctx['action_url'] = "{}/web?db={}".format(base_url, self.env.cr.dbname)

return {

'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,

}


663 views0 comments

Recent Posts

See All

Comments


Subscribe Form

  • facebook
  • linkedin

©2019 by Excelroot Technology Sdn Bhd.

bottom of page