Odoo Help

Welcome!

This community 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.

0

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

By
julien rey
on 3/18/14, 6:16 AM 1,863 views

Hi,

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

class quitus(osv.Model):
_name='quitus.quitus'
_columns={
    'order_id':fields.many2one('sale.order','Order',required=True),
    'employee_id':fields.many2one('hr.employee','Employee'),
    'description':fields.text('Description',required=True),
    'state':fields.selection([('draft','Draft'),('issued','Issued'),('confirmed','Confirmed')],'State',required=True,readonly=True), 
    'approver':fields.char('Approver',size=100,required=True),
    'scanned':fields.many2one('ir.attachment','Scanned Quitus'),
}
_defaults={
    'state':'draft',
}
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')
    try:
        template_id = ir_model_data.get_object_reference(cr, uid, 'quitus', 'email_quitus_template')[1]
    except ValueError:
        template_id = False
    try:
        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)
    ctx.update({
        '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,

quitus()}

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"> 
                    <header>
                        <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" 
                                widget="statusbar" 
                                statusbar_visible="draft,issued,confirmed" 
                                statusbar_colors="{"issued":"blue"}"/>
                    </header>
                    <sheet>
                        <h1>Quitus</h1>
                        <group> 
                            <field name="order_id" /> 
                            <field name="approver"/> 
                            <field name="description" /> 
                            <field name="employee_id"/>
                            <field name="scanned" />   
                        </group> 
                    </sheet>                        
            </form> 
        </field> 
    </record>

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">
            <![CDATA[
                <p>
                    Hello,
                </p>
                <p>
                    You'll find enclosed a copy of your quitus regarding order ${object.order_id.name} / ${object.order_id.client_order_ref}.
                </p>
                <p>
                    This quitus concerns :
                </p>
                <p>
                    ${object.description}
                </p>
                <p>
                    Best regards,
                </p>
                <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>
                <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:
                            ${object.order_id.company_id.street}<br/>
                        % endif
                        % if object.order_id.company_id.street2:
                            ${object.order_id.company_id.street2}<br/>
                        % 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
                        </span>
                        % 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}
                            </div>
                        % endif
                        % if object.order_id.company_id.website:
                            <div>
                                Web : <a href="${object.order_id.company_id.website}">${object.order_id.company_id.website}</a>
                            </div>
                        %endif
                        <p></p>
                    </div>
                </div>
            ]]>
        </field>
        <field name="attachment_ids">
            ${object.scanned}
        </field>
    </record>

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">
            ${object.scanned}
        </field>

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

cheers

Julien

1
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]
    try:
        #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

Cheers

Julien

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.

sengottuvel
on 11/4/14, 7:17 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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