Skip to Content
Menu
This question has been flagged
1 Reply
6804 Views

Hi everyone!

I would like to attach PDF (or whatever file type...) to the quotation sent by email..

For this, i'm trying to override the send_mail method from mail_compose_message class. I succeeded to create a new mail with hardcoded values and to attach my files to that email, but of course i would like to attach the files to the email created with the pop up view when "send by mail" is clicked.

How can i override that method? I think i should call super.send_mail() but i don't know how to add my attachment if i do that :/

Here is my code with hardcoded values:


class mail_compose_message(osv.Model):
_inherit = 'mail.compose.message'

@api.multi
def send_mail(self):
#retrieve order
order_id =self.env.context["active_id"]
order = self.env['sale.order'].browse([order_id])
attachment_ids = []
#create a new ir.attachment
ir_attachment = self.env['ir.attachment']
mail = self.env["mail.mail"].create({"email_from":"a@a.com","email_to":"b@b.com","subject":"subject","body_html":"d"})

#for each line, look for product fiches. Add fiches to attachment
for line in order.order_line:
for product_fiche in line.product_id.documents:
attachment_data = {'name': product_fiche.name,
'datas_fname': product_fiche.datas_fname,
'datas': product_fiche.datas,
'res_model': "mail.mail",
'res_id': mail.id,
}
 
attachment_ids.append(ir_attachment.create(attachment_data).id)

        mail.write({'attachment_ids': [(6, 0, attachment_ids)]})
return mail.send()


I suppose i shouldn't be creating the email myself, and only send the attachment_ids to the super method, but i don't know how to do that. Could someone help me?

Avatar
Discard
Best Answer

Hello Jerome,

You can create the attachments in "mail.compose.message" by overriding the send_mail method.

Ex:

@api.multi
def send_mail(self):
# create the attachments same way you have created in your code
# and append attachment ID in a list
for attachment_id in attachment_ids:
self.write({'attachment_ids': [(4, attachment_id)]})
# super call
return super(mail_compose_message, self).send_mail()

Hope this helps you.

Avatar
Discard