diff --git a/openerp/addons/base/ir/ir_mail_server.py b/openerp/addons/base/ir/ir_mail_server.py index 96fa9f52a88..8da0f34f0f9 100644 --- a/openerp/addons/base/ir/ir_mail_server.py +++ b/openerp/addons/base/ir/ir_mail_server.py @@ -236,15 +236,18 @@ class ir_mail_server(osv.osv): return connection def build_email(self, email_from, email_to, subject, body, email_cc=None, email_bcc=None, reply_to=False, - attachments=None, message_id=None, references=None, object_id=False, subtype='plain', headers=None): + attachments=None, message_id=None, references=None, object_id=False, subtype='plain', headers=None, + body_alternative=None, subtype_alternative='plain'): """Constructs an RFC2822 email.message.Message object based on the keyword arguments passed, and returns it. :param string email_from: sender email address :param list email_to: list of recipient addresses (to be joined with commas) :param string subject: email subject (no pre-encoding/quoting necessary) - :param string body: email body, according to the ``subtype`` (by default, plaintext). + :param string body: email body, of the type ``subtype`` (by default, plaintext). If html subtype is used, the message will be automatically converted - to plaintext and wrapped in multipart/alternative. + to plaintext and wrapped in multipart/alternative, unless an explicit + ``body_alternative`` version is passed. + :param string body_alternative: optional alternative body, of the type specified in ``subtype_alternative`` :param string reply_to: optional value of Reply-To header :param string object_id: optional tracking identifier, to be included in the message-id for recognizing replies. Suggested format for object-id is "res_id-model", @@ -252,6 +255,8 @@ class ir_mail_server(osv.osv): :param string subtype: optional mime subtype for the text body (usually 'plain' or 'html'), must match the format of the ``body`` parameter. Default is 'plain', making the content part of the mail "text/plain". + :param string subtype_alternative: optional mime subtype of ``body_alternative`` (usually 'plain' + or 'html'). Default is 'plain'. :param list attachments: list of (filename, filecontents) pairs, where filecontents is a string containing the bytes of the attachment :param list email_cc: optional list of string values for CC header (to be joined with commas) @@ -276,7 +281,7 @@ class ir_mail_server(osv.osv): if not body: body = u'' email_body_utf8 = ustr(body).encode('utf-8') - email_text_part = MIMEText(email_body_utf8 or '', _subtype=subtype, _charset='utf-8') + email_text_part = MIMEText(email_body_utf8, _subtype=subtype, _charset='utf-8') msg = MIMEMultipart() if not message_id: @@ -304,13 +309,21 @@ class ir_mail_server(osv.osv): for key, value in headers.iteritems(): msg[ustr(key).encode('utf-8')] = encode_header(value) - if html2text and subtype == 'html': - # Always provide alternative text body if possible. + if subtype == 'html' and not body_alternative and html2text: + # Always provide alternative text body ourselves if possible. text_utf8 = tools.html2text(email_body_utf8.decode('utf-8')).encode('utf-8') alternative_part = MIMEMultipart(_subtype="alternative") alternative_part.attach(MIMEText(text_utf8, _charset='utf-8', _subtype='plain')) alternative_part.attach(email_text_part) msg.attach(alternative_part) + elif body_alternative: + # Include both alternatives, as specified, within a multipart/alternative part + alternative_part = MIMEMultipart(_subtype="alternative") + body_alternative_utf8 = ustr(body_alternative).encode('utf-8') + alternative_body_part = MIMEText(body_alternative_utf8, _subtype=subtype_alternative, _charset='utf-8') + alternative_part.attach(alternative_body_part) + alternative_part.attach(email_text_part) + msg.attach(alternative_part) else: msg.attach(email_text_part)