Odoo Help


4 Answers

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.
7/6/17 10:52

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:

7/6/17 11:20

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

9/6/17 01:14

Hi Magen,

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

11/6/17 10:49

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.

7/6/17 15:36

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

def action_send_mail(self):
        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']
            template_id = ir_model_data.get_object_reference('music', 'email_template_music_audiofile')[1]
        except ValueError:
            template_id = False
            compose_form_id = ir_model_data.get_object_reference('mail', 'email_compose_message_wizard_form')[1]
        except ValueError:
            compose_form_id = False
        ctx = dict()
            '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?

Ask a Question
Keep Informed
1 follower(s)
About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now