Odoo Help


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.


openerp 7 using wizard how to send email with attachment?... [Closed]

on 5/16/13, 6:41 AM 4,738 views

The Question has been closed

Sudhir Arya (SA)
on 04/15/2014 02:41:29

In openerp 7 using wizard able send email with the help Share Module. How to send email with attachment?

Sample Share module Code

def send_emails(self, cr, uid, wizard_data, context=None):
        _logger.info('Sending share notifications by email...')
        mail_mail = self.pool.get('mail.mail')
        user = self.pool.get('res.users').browse(cr, UID_ROOT, uid)
        if not user.email:
            raise osv.except_osv(_('Email required'), _('The current user must have an email address configured in User Preferences to be able to send outgoing emails.'))

        # TODO: also send an HTML version of this mail
        mail_ids = []
        for result_line in wizard_data.result_line_ids:
            email_to = result_line.user_id.email
            if not email_to:
            subject = wizard_data.name
            body = _("Hello,\n\n")
            body += _("I've shared %s with you!\n\n") % wizard_data.name
            mail_ids.append(mail_mail.create(cr, uid, {
                    'email_from': user.email,
                    'email_to': email_to,
                    'subject': subject,
                    'body_html': '<pre>%s</pre>' % body}, context=context))
        # force direct delivery, as users expect instant notification
        mail_mail.send(cr, uid, mail_ids, context=context)
        _logger.info('%d share notification(s) sent.', len(mail_ids))

In the above code the below line added.

attachments = []
attachments.append(("test.txt", base64.b64decode("Test Attached File")))
mail_ids.append(mail_mail.create(cr, uid, {
                    'email_from': email_from,
                    'email_to': email_to,
                    'subject': subject,
                    'body_html': '<pre>%s</pre>' % body,
                    'attachments': attachments}, context=context))

But the above code unable to email with attachment. How to send with attachment ?



Sudhir Arya (SA)

--Sudhir Arya (SA)--
| 6 8 8
Ahmedabad, India
--Sudhir Arya (SA)--

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow




Sudhir Arya (SA)
On 5/16/13, 7:18 AM

You have to create ir.attachment and then add attachment ID in mail.mail.

This is what I did to send attachment in one mail:

attachment_ids = []
for data in attachments:
    attachment_data = {
        'name': data[0],
        'datas_fname': data[0],
        'datas': data[1],
        'res_model': mail_mail._name,
        'res_id': mail_id,
    attachment_ids.append(ir_attachment.create(cursor, uid, attachment_data, context=context))
if attachment_ids:
    mail_mail.write(cursor, uid, mail_id, {'attachment_ids': [(6, 0, attachment_ids)]}, context=context)
mail_mail_obj.send(cursor, uid, [mail_id], context=context)

This code will create the attachment and then add the attachment IDs in mail.mail record.

thanks for reply the above code is working.

on 5/16/13, 7:30 AM

My pleasure.

Sudhir Arya (SA)
on 5/16/13, 7:47 AM

What do you mean by multiple client? Can you explain in detail?

Sudhir Arya (SA)
on 5/16/13, 7:51 AM

Yes. Just concatenate all email addresses in one string separated by comma.

Try this:

send_to_mail = " "
for user in rec.member_ids:
    send_to_mail += str(user.user_email) + " ,"
Sudhir Arya (SA)
on 5/16/13, 7:59 AM

There is alternate for that. You can create one wizard and then return that wizards action from your mail send method.

Sudhir Arya (SA)
on 5/16/13, 8:32 AM

Hi Sudhir Arya, I have requirement "When a product of a particular type is sold, a reminder email is sent in two months to same customer urging a re-order" currently automated Action configured to send email after 2 months but how to configure based on particular product type only?... Thanks

on 7/1/13, 8:29 AM

Hi,,I designed a new report for purchase order. My requirement is user click on PO confirm button a email should go with the customized PO report as a attachment. I don't know how to do that. Kindly help me to achieve this....Thanks and awaiting your valuable response

on 9/3/13, 12:28 PM

Create Email Template and advanced tab select "Optional report to print and attach" and PO confirm button override send email template. The below link is use to send email http://help.openerp.com/question/21588/send-email-template-through-code/

on 9/4/13, 10:58 AM

Prakash please help me how to attach a report automatically in Wizard report.

on 4/15/14, 2:33 AM
On 3/16/15, 3:29 AM

i want to add attachment in project_issue module using popup window (python code) . How to possible ?

About This Community

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

Question tools

0 follower(s)


Asked: 5/16/13, 6:41 AM
Seen: 4738 times
Last updated: 3/16/15, 8:10 AM