Skip to Content
Menu
This question has been flagged

I want to have a send by mail action to a model attaching the binary field to the mail.

  • It should be able to send to a custom mail (not only from the users list)

  • it should delete the attachment after send.

So far i have:

@api.multi
    def action_send_mail(self):
        self.ensure_one()
        attachment = {
            'name': ("%s" %self.filename),
            'datas': self.get_file_data(),
            'datas_fname': self.filename,
            'res_model': 'my.model',
            'type': 'binary'
        }
        id = self.env['ir.attachment'].create(attachment)
        email_template = self.env.ref('my.email_template_example')
        email_template.attachment_ids =  False
        email_template.attachment_ids =  [(4,id.id)]

        ir_model_data = self.env['ir.model.data']
        try:
            template_id = ir_model_data.get_object_reference('my', 'email_template_example')[1]
        except ValueError:
            template_id = False
        try:
            compose_form_id = ir_model_data.get_object_reference('my', 'email_compose_message_wizard_form')[1]
        except ValueError:
            compose_form_id = False
        ctx = dict()
        ctx.update({
            'default_model': 'my.model',
            'default_res_id': self.ids[0],
            'default_use_template': bool(template_id),
            'default_template_id': template_id,
            'default_composition_mode': 'comment',
            'attachment_ids':  [(4,id.id)],
        })
        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,
        }


And I have created mail template like this:

</record>
        <!--Email template -->
        <record id="email_template_my_module" model="mail.template">
            <field name="name">Example - Send by Email</field>
            <field name="email_from"></field>
            <field name="subject"></field>
            <field name="email_to"></field>
            <field name="model_id" ref="model_my_module"/>
            <field name="auto_delete" eval="True"/>
            <field name="attachment_ids"></field>
            <field name="body_html"><![CDATA[
<p>
I'm sending you the file as attachment
</p>
]]></field>
 </record>
  • The mail get send only if I select a recipient from the existing users. I cannot make it send to custom mail, I've been trying to override 'mail.compose.message' and 'mail.message' but no success.

  • Where can I execute code for deleting the attachment after send?

Avatar
Discard
Related Posts Replies Views Activity
1
Aug 24
274
0
Dec 19
1980
0
Nov 21
1990
0
Nov 21
1670
1
Aug 20
10676