Skip to Content
Menu
This question has been flagged
4 Replies
8788 Views

In my model I have

file = fields.Binary(string='File', store=False, required=True)

How can I send it by mail? Y would like to be able to chose any custom email to send to.

Avatar
Discard
Best Answer

Try this code,

def send_mail_func(self)

    email_template = self.env.ref('module1.test_email_template')

     attachment = {

               'name': str(self.file_name),

               'datas': self.binary_field_name

          datas_fname': self.file_name,

               'res_model': 'model_name',

               'type': 'binary'

           }
         
             id = self.env['ir.attachment'].create(attachment)

                email_template.attachment_ids = [(4, ir_id.id)]

               email_template.send_mail(self.id, raise_exception=False, force_send=True)


In this code,  details with binary field is created in ir_attachment. Then  [(4, ir_id.id)] is used to link with existing record of email_template.  Finally the mail will be send with binary field.
Avatar
Discard
Author

Hey thanks, I am on the right way. I need to see the compose mail form though so I can manually put the mail_to: and the body of the message. Right now the mails stay as not sent so I have to go to settings->mail and resend them with the correct mail_to:

Author

Also I see that the attachments are not being deleted after the mail is sent. The template has <field name="auto_delete" eval="True"/>

Hi Magen,

After sending a mail, use 'email_template.attachment_ids = [(3, ir_id.id)]' to unlink the attachment.

Author

Where can I execute the code after sending the mail since I use the 'email_compose_message_wizard_form' instead of sending it directly with 'send_mail'

Also in my question I mention that in the 'compose_message_wizard' I want to specify custom email to send to not a user. I still can't figure out this one.

Author Best Answer

Ok i made it work like that [EDIT: it does not attach correctly the file]

def action_send_mail(self):
        self.ensure_one()
        attachment = {
            'name': ("%s" %self.filename),
            'datas': self.get_file_data(),
            'datas_fname': self.filename,
            'res_model': 'music_audiofile',
            'type': 'binary'
        }
        id = self.env['ir.attachment'].create(attachment)
        ir_model_data = self.env['ir.model.data']
        try:
            template_id = ir_model_data.get_object_reference('music', 'email_template_music_audiofile')[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
        ctx = dict()
        ctx.update({
            'default_model': 'music.audiofile',
            '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,
            '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,
        }.

 So what is wrong with this approach. And I cannot figure out how to enter manually the Mail To: instead of choosing from the list of users as s.Do I have to override the email_compose_message_wizard_form or something like that?

Avatar
Discard
Related Posts Replies Views Activity
5
Jan 23
14438
1
Aug 24
273
1
Mar 15
5873
0
Nov 21
1989
2
Jul 20
25897