Odoo Help


Send a mail with automatic attachment ofa binary field from a model

julien rey
on 3/18/14, 6:16 AM 2,838 views


I have a model 'quitus.quitus' that contains a binary field 'scanned' as you can see below :

class quitus(osv.Model):
    'scanned':fields.many2one('ir.attachment','Scanned Quitus'),
def action_confirmed(self, cr, uid, ids):
    for o in self.browse(cr, uid, ids):
        if not o.employee_id:
            raise osv.except_osv(_('Error!'),_('You cannot confirm a quitus without employee'))
        if not o.scanned:
            raise osv.except_osv(_('Error!'),_('You cannot confirm a quitus without scanned doc'))  
    self.write(cr, uid, ids, { 'state' : 'confirmed' })
    return True
def action_issued(self, cr, uid, ids):
    self.write(cr, uid, ids, { 'state' : 'issued' })
    return True
def action_draft(self, cr, uid, ids):
    self.write(cr, uid, ids, { 'state' : 'draft' })
    return True
def action_send_mail(self, cr, uid, ids, context=None):
    assert len(ids) == 1, 'This option should only be used for a single id at a time.'
    ir_model_data = self.pool.get('ir.model.data')
        template_id = ir_model_data.get_object_reference(cr, uid, 'quitus', 'email_quitus_template')[1]
    except ValueError:
        template_id = False
        compose_form_id = ir_model_data.get_object_reference(cr, uid, 'mail', 'email_compose_message_wizard_form')[1]
    except ValueError:
        compose_form_id = False 
    ctx = dict(context)
        'default_model': 'quitus.quitus',
        'default_res_id': ids[0],
        'default_use_template': bool(template_id),
        'default_template_id': template_id,
        'default_composition_mode': 'comment',
        'mark_so_as_sent': 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,


Then on my form view, I have added a button to send a mail :

<record model="ir.ui.view" id="view_quitus_form">
        <field name="name">view.quitus.form</field> 
        <field name="model">quitus.quitus</field> 
        <!-- types: tree,form,calendar,search,graph,gantt,kanban --> 
        <field name="type">form</field> 
        <field name="priority" eval="16"/> 
        <field name="arch" type="xml"> 
            <form string="Quitus form" version="7.0"> 
                        <button name="button_issued" string="Ready for Sign-off" states="draft" type="workflow"/>
                        <button name="button_confirmed" string="Confirm signed-off" states="issued" type="workflow"/>
                        <button name="action_send_mail" string="Send" states="confirmed" type="object"/>
                        <field name="state" 
                            <field name="order_id" /> 
                            <field name="approver"/> 
                            <field name="description" /> 
                            <field name="employee_id"/>
                            <field name="scanned" />   

This button 'action_send_mail' works fine, and use the mail template below

<record id="email_quitus_template" model="email.template">
        <field name="model_id"  ref="quitus.model_quitus_quitus"></field>
        <field name="name">Send Quitus by mail</field>

        <field name="email_from"><![CDATA[${object.order_id.user_id.name} <${object.order_id.user_id.email}>]]></field>
        <field name="email_to"></field>
        <field name="subject">Copie de votre quitus</field>
        <field name="body_html">
                    You'll find enclosed a copy of your quitus regarding order ${object.order_id.name} / ${object.order_id.client_order_ref}.
                    This quitus concerns :
                    Best regards,
                <div style="width: 375px; margin: 0px; padding: 0px; background-color: #8E0000; border-top-left-radius: 5px 5px; border-top-right-radius: 5px 5px; background-repeat: repeat no-repeat;">
                        <h3 style="margin: 0px; padding: 2px 14px; font-size: 12px; color: #DDD;">
                            <strong style="text-transform:uppercase;">${object.order_id.company_id.name}</strong></h3>
                <div style="width: 347px; margin: 0px; padding: 5px 14px; line-height: 16px; background-color: #F2F2F2;">
                        <span style="color: #222; margin-bottom: 5px; display: block; ">
                        % if object.order_id.company_id.street:
                        % endif
                        % if object.order_id.company_id.street2:
                        % endif
                        % if object.order_id.company_id.city or object.order_id.company_id.zip:
                            ${object.order_id.company_id.zip} ${object.order_id.company_id.city}<br/>
                        % endif
                        % if object.order_id.company_id.country_id:
                            ${object.order_id.company_id.state_id and ('%s, ' % object.order_id.company_id.state_id.name) or ''} ${object.order_id.company_id.country_id.name or ''}<br/>
                        % endif
                        % if object.order_id.company_id.phone:
                            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
                                Tél:  ${object.order_id.company_id.phone}
                        % endif
                        % if object.order_id.company_id.website:
                                Web : <a href="${object.order_id.company_id.website}">${object.order_id.company_id.website}</a>
        <field name="attachment_ids">

However, I'd like to attach automatically the contents of the binary field 'scanned' to my mail ..I try this way below unsuccesfully

<field name="attachment_ids">

I need some help, i'm little bit confused.



julien rey
On 3/18/14, 8:38 PM

Hi again,

I have finnaly found an acceptable solution. I share my experience for everybody.

I have updated my function send_mail in my quitus object.

def action_send_mail(self, cr, uid, ids, context=None):
    assert len(ids) == 1, 'This option should only be used for a single id at a time.'
    ir_model_data = self.pool.get('ir.model.data')
    o = self.browse(cr, uid, ids)[0]
        #get the template_id
        template_id = ir_model_data.get_object_reference(cr, uid, 'quitus', 'email_quitus_template')[1]
        email_obj = self.pool.get('email.template')  
        #get the object corresponding to template_id
        email = email_obj.browse(cr, uid, template_id)
        #get the id of the document, stored into field scanned
        attachment_id = o.scanned.id
        #write the new attachment to mail template
        email_obj.write(cr, uid, template_id, {'attachment_ids': [(6, 0, [attachment_id ])]})  
    except ValueError:
        template_id = False .......

I change the email.template object before sending mail. I added an attachement from document (ir_attachement).

I hope this could help anyone



Hi, I have a requirement while confirm a sale order system should send the sale order copy to customer automaticlly with out any user input. Now if you click send mail button again user need to click send button. I created templete and format. How to get atttchment id for send the sale order copy. Kindly help me for complete this requirement. Thanks in advance.

on 11/4/14, 7:17 AM

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

Question tools

1 follower(s)


Asked: 3/18/14, 6:16 AM
Seen: 2838 times
Last updated: 3/16/15, 8:10 AM