This question has been flagged
2 Replies
15621 Views

In some legislations it is obligatory, to send certain company information with every business email, such as the tax number etc. Because having this long stuff in the signature is ugly, I prefer to attach a vcard ("vcf") file with all emails. This can be easily ignored by most and easily used by some.

How can I attach a file to every outgoing email? It is OK, if the file is always the same for all users of the same company. (My setup is one-company, btw.)

Avatar
Discard
Best Answer

One way that I can think of to accomplish what you are after is to create a module which defines a class that inherits from mail.mail.

You can override the send() function to first add your attachment to the mail message, and then call the superclass' send() function. That will give you exactly what you need, and probably configurability besides.

class autoattach_mail(osv.osv):
  _name='mail.mail'
  _inherit='mail.mail'

  def send(self, cr, uid, ids, auto_commit=False, recipient_ids=None, context=None):

    #TODO: add your attachment to each mail message in ids

    return super(autoattach_mail, self).send(cr, uid, ids, auto_commit=auto_commit, recipient_ids=recipient_ids, context=context)
Avatar
Discard
Author

I will definetely try this. I wonder, how I would make OpenERP use my derived class instead of the base class?

It should do so automatically, if you set _name to the same thing as _inherit. You'll need to update the affected modules also, so when you run the server again, run it with "--update all".

Author

It took some time for me, to get through the details, but now it works. I copied the complete send() method, and just added the attachment: with open("mycompany.vcf") as f: attachments.append(("mycompany.vcf", f.read()))

Very nicely done!

Best Answer

Create a email template. In the advanced tab, there you can easily attach attachments. Then select this email template for sending mails

Avatar
Discard
Author

This works indeed, but all the support staff has still to remember to use the template with every outgoing email. It is just one click to use the template, but in my experience in most cases it will not be done. The question remains how to do this automatically. (Nearby, I could not find how to set the MIME type of the attachment. It should be Content-Type: text/x-vcard; charset=utf-8;)

Author

Another (minor) problem with this approach is, that the VCard is visible in every comment sent out, taking a lot of screen real estate by default. One can probably solve this with a little bit of CSS, but still...