如何在Odoo 15中添加二进制文件到电子邮件附件



大家好!我有一个二进制字段,其中包含我用etree.Element

生成的xml文件batchfile = fields.Binary("XML Batch File", readonly=True)

所以我想附加这个二进制文件,以便我通过电子邮件发送它,但我怎么能做到这一点?

def send_claim_batch(self):
self.ensure_one()
ir_model_data = self.env['ir.model.data']
try:
template_id = ir_model_data._xmlid_lookup('iemr_medical_aid.iemr_claim_batch_email')[2]
except ValueError:
template_id = False

try:
compose_form_id = ir_model_data._xmlid_lookup('mail.email_compose_message_wizard_form')[2]
except ValueError:
compose_form_id = False

template_id = self.env.ref('event.event_registration_mail_template_badge')
compose_form = self.env.ref('mail.email_compose_message_wizard_form')

ctx = {
default_model: 'iemr.claim.batch',
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,
force_email: True,
}
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,
}

我不能测试它,但我认为它是这样的。

ctx = {
default_model: 'iemr.claim.batch',
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,
force_email: True,
}
if self.batchfile:
attachment = self.env['ir.attachment'].create({
'name': "%s.xml" % self.id,
'datas': self.batchfile,
'type': 'binary',
'res_model': self._name,
'res_id': self.id,
})
ctx['default_attachment_ids'] = [attachment.id]
return {
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
#...

最新更新