Search code examples
pythonemailxsltodooemail-attachments

Generate a .xsl file from an encodeString


I have an encoded string (excel_file) and I want to generate a xsl file from this string.

Is it possible to generate a .xsl file adding a code only in my .py file without saving this file in local, because I want to attache this file to a mail.

att_id = self.env['ir.attachment'].create({

                    'name': 'My name',
                    'type': 'binary',
                    'datas':excel_file,
                    'datas_fname': 'Myname.xsl',
                    'res_model': 'print.invoice.cron',
                    'res_id': self.id,
                    'mimetype': 'text/csv'

                    })


                tools.email_send(email_from='[email protected]',
                            email_to=['[email protected]'],
                            subject='Some subject',
                            body=att_id)

I'm reciving the id of attachement,but not the file.And there is no attache attribute for email_send()


Solution

  • So here's my suggestion:

    att_id = self.env['ir.attachment'].create({
    
                        'name': 'My name',
                        'type': 'binary',
                        'datas':excel_file,
                        'datas_fname': 'Myname.xsl',
                        'res_model': 'print.invoice.cron',
                        'res_id': self.id,
                        'mimetype': 'text/csv'
    
                        })
    template = self.env.ref(
                            'module_name.email_template', False)
    ctx = dict(
                                default_model=self._name,
                                default_res_id=self.id,
                                default_use_template=bool(template),
                                default_template_id=template.id,
                                default_composition_mode='comment',
                                default_email_to=email,
                                default_lang=self.env.user.partner_id.lang,
                                default_attachment_ids=[(6, 0, att_id.ids)],
                            )
    self.env['mail.template'].browse(template.id).with_context(
                                ctx).send_mail(self.id, force_send=True)
    

    Code for template creation:

        <?xml version="1.0" encoding="utf-8"?>
        <odoo>
            <data noupdate="1">
                <record id="email_template" model="mail.template">
                    <field name="name">Template Name</field>
                    <field name="model_id" ref="module_name.model_your_model"/>
                    <field name="email_to">${ctx.get('default_email_to')}</field>
                    <field name="subject">Subject</field>
                    <field name="body_html" type="html">
        <div style="margin: 0px; padding: 0px;">
            <p style="margin: 0px; padding: 0px; font-size: 13px;">
    Email body
    </p>
            Thanks
        </div>
                    </field>
                    <field name="lang">${ctx.get('default_lang')}</field>
                    <field name="user_signature" eval="True"/>
                    <field name="auto_delete" eval="True"/>
                </record>
    
            </data>
        </odoo>
    

    Please check and let me know if it works.