[MERGE] forward port of branch 7.0 up to be7c894
[odoo/odoo.git] / addons / mail / mail_mail.py
index 22fa781..9dcea07 100644 (file)
 import base64
 import logging
 import re
+from email.utils import formataddr
 from urllib import urlencode
 from urlparse import urljoin
 
 from openerp import tools
 from openerp import SUPERUSER_ID
+from openerp.addons.base.ir.ir_mail_server import MailDeliveryException
 from openerp.osv import fields, osv
-from openerp.osv.orm import except_orm
 from openerp.tools.translate import _
 
 _logger = logging.getLogger(__name__)
@@ -44,7 +45,6 @@ class mail_mail(osv.Model):
 
     _columns = {
         'mail_message_id': fields.many2one('mail.message', 'Message', required=True, ondelete='cascade'),
-        'mail_server_id': fields.many2one('ir.mail_server', 'Outgoing mail server', readonly=1),
         'state': fields.selection([
             ('outgoing', 'Outgoing'),
             ('sent', 'Sent'),
@@ -55,31 +55,18 @@ class mail_mail(osv.Model):
         'auto_delete': fields.boolean('Auto Delete',
             help="Permanently delete this email after sending it, to save space"),
         'references': fields.text('References', help='Message references, such as identifiers of previous messages', readonly=1),
-        'email_from': fields.char('From', help='Message sender, taken from user preferences.'),
         'email_to': fields.text('To', help='Message recipients (emails)'),
         'recipient_ids': fields.many2many('res.partner', string='To (Partners)'),
         'email_cc': fields.char('Cc', help='Carbon copy message recipients'),
         'body_html': fields.text('Rich-text Contents', help="Rich-text/HTML message"),
-        # If not set in create values, auto-detected based on create values (res_id, model, email_from)
-        'reply_to': fields.char('Reply-To',
-            help='Preferred response address for the message'),
         # Auto-detected based on create() - if 'mail_message_id' was passed then this mail is a notification
         # and during unlink() we will not cascade delete the parent and its attachments
         'notification': fields.boolean('Is Notification',
-            help='Mail has been created to notify people of an existing mail.message')
+            help='Mail has been created to notify people of an existing mail.message'),
     }
 
-    def _get_default_from(self, cr, uid, context=None):
-        this = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid, context=context)
-        if this.alias_domain:
-            return '%s <%s@%s>' % (this.name, this.alias_name, this.alias_domain)
-        elif this.email:
-            return '%s <%s>' % (this.name, this.email)
-        raise osv.except_osv(_('Invalid Action!'), _("Unable to send email, please configure the sender's email address or alias."))
-
     _defaults = {
         'state': 'outgoing',
-        'email_from': lambda self, cr, uid, ctx=None: self._get_default_from(cr, uid, ctx),
     }
 
     def default_get(self, cr, uid, fields, context=None):
@@ -89,61 +76,10 @@ class mail_mail(osv.Model):
             context = dict(context, default_type=None)
         return super(mail_mail, self).default_get(cr, uid, fields, context=context)
 
-    def _get_reply_to(self, cr, uid, values, context=None):
-        """ Return a specific reply_to: alias of the document through message_get_reply_to
-            or take the email_from
-        """
-        # if value specified: directly return it
-        if values.get('reply_to'):
-            return values.get('reply_to')
-
-        mailgateway = True  # tells whether the answer will go through the mailgateway, leading to the formatting of reply_to <Followers of ...>
-        ir_config_parameter = self.pool.get("ir.config_parameter")
-        catchall_domain = ir_config_parameter.get_param(cr, uid, "mail.catchall.domain", context=context)
-
-        # model, res_id, email_from, reply_to: comes from values OR related message
-        message = None
-        if values.get('mail_message_id'):
-            message = self.pool.get('mail.message').browse(cr, uid, values.get('mail_message_id'), context=context)
-        model = values.get('model', message and message.model or False)
-        res_id = values.get('res_id', message and message.res_id or False)
-        email_from = values.get('email_from', message and message.email_from or False)
-        email_reply_to = message and message.reply_to or False
-
-        # if model and res_id: try to use ``message_get_reply_to`` that returns the document alias
-        if not email_reply_to and model and res_id and hasattr(self.pool[model], 'message_get_reply_to'):
-            email_reply_to = self.pool[model].message_get_reply_to(cr, uid, [res_id], context=context)[0]
-        # no alias reply_to -> catchall alias
-        if not email_reply_to:
-            catchall_alias = ir_config_parameter.get_param(cr, uid, "mail.catchall.alias", context=context)
-            if catchall_domain and catchall_alias:
-                email_reply_to = '%s@%s' % (catchall_alias, catchall_domain)
-        # no alias reply_to -> reply_to will be the email_from, only the email part
-        if not email_reply_to and email_from:
-            emails = tools.email_split(email_from)
-            if emails:
-                email_reply_to = emails[0]
-                if emails[0].split('@')[1] != catchall_domain:
-                    mailgateway = False
-
-        # format 'Document name <email_address>'
-        if email_reply_to and model and res_id and mailgateway:
-            document_name = self.pool[model].name_get(cr, SUPERUSER_ID, [res_id], context=context)[0]
-            if document_name:
-                # sanitize document name
-                sanitized_doc_name = re.sub(r'[^\w+.]+', '-', document_name[1])
-                # generate reply to
-                email_reply_to = _('"Followers of %s" <%s>') % (sanitized_doc_name, email_reply_to)
-
-        return email_reply_to
-
     def create(self, cr, uid, values, context=None):
         # notification field: if not set, set if mail comes from an existing mail.message
         if 'notification' not in values and values.get('mail_message_id'):
             values['notification'] = True
-        # reply_to: if not set, set with default values that require creation values
-        if not values.get('reply_to'):
-            values['reply_to'] = self._get_reply_to(cr, uid, values, context=context)
         return super(mail_mail, self).create(cr, uid, values, context=context)
 
     def unlink(self, cr, uid, ids, context=None):
@@ -177,7 +113,7 @@ class mail_mail(osv.Model):
         if context is None:
             context = {}
         if not ids:
-            filters = ['&', ('state', '=', 'outgoing'), ('type', '=', 'email')]
+            filters = [('state', '=', 'outgoing')]
             if 'filters' in context:
                 filters.extend(context['filters'])
             ids = self.search(cr, uid, filters, context=context)
@@ -205,6 +141,32 @@ class mail_mail(osv.Model):
             self.unlink(cr, SUPERUSER_ID, [mail.id], context=context)
         return True
 
+    #------------------------------------------------------
+    # mail_mail formatting, tools and send mechanism
+    #------------------------------------------------------
+
+    def _get_partner_access_link(self, cr, uid, mail, partner=None, context=None):
+        """ Generate URLs for links in mails:
+            - partner is an user and has read access to the document: direct link to document with model, res_id
+        """
+        if partner and partner.user_ids:
+            base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
+            # the parameters to encode for the query and fragment part of url
+            query = {'db': cr.dbname}
+            fragment = {
+                'login': partner.user_ids[0].login,
+                'action': 'mail.action_mail_redirect',
+            }
+            if mail.notification:
+                fragment['message_id'] = mail.mail_message_id.id
+            elif mail.model and mail.res_id:
+                fragment.update(model=mail.model, res_id=mail.res_id)
+
+            url = urljoin(base_url, "/web?%s#%s" % (urlencode(query), urlencode(fragment)))
+            return _("""<span class='oe_mail_footer_access'><small>Access your messages and documents <a style='color:inherit' href="%s">in OpenERP</a></small></span>""") % url
+        else:
+            return None
+
     def send_get_mail_subject(self, cr, uid, mail, force=False, partner=None, context=None):
         """ If subject is void and record_name defined: '<Author> posted on <Resource>'
 
@@ -218,32 +180,6 @@ class mail_mail(osv.Model):
             return 'Re: %s' % (mail.parent_id.subject)
         return mail.subject
 
-    def send_get_mail_body_footer(self, cr, uid, mail, partner=None, context=None):
-        """ Return a specific footer for the ir_email body.  The main purpose of this method
-            is to be inherited by Portal, to add modify the link for signing in, in
-            each notification email a partner receives.
-        """
-        body_footer = ""
-        # partner is a user, link to a related document (incentive to install portal)
-        if partner and partner.user_ids and mail.model and mail.res_id \
-                and self.check_access_rights(cr, partner.user_ids[0].id, 'read', raise_exception=False):
-            related_user = partner.user_ids[0]
-            try:
-                self.pool[mail.model].check_access_rule(cr, related_user.id, [mail.res_id], 'read', context=context)
-                base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
-                # the parameters to encode for the query and fragment part of url
-                query = {'db': cr.dbname}
-                fragment = {
-                    'login': related_user.login,
-                    'model': mail.model,
-                    'id': mail.res_id,
-                }
-                url = urljoin(base_url, "?%s#%s" % (urlencode(query), urlencode(fragment)))
-                body_footer = _("""<small>Access this document <a style='color:inherit' href="%s">directly in OpenERP</a></small>""") % url
-            except except_orm, e:
-                pass
-        return body_footer
-
     def send_get_mail_body(self, cr, uid, mail, partner=None, context=None):
         """ Return a specific ir_email body. The main purpose of this method
             is to be inherited to add custom content depending on some module.
@@ -253,9 +189,10 @@ class mail_mail(osv.Model):
         """
         body = mail.body_html
 
-        # add footer
-        body_footer = self.send_get_mail_body_footer(cr, uid, mail, partner=partner, context=context)
-        body = tools.append_content_to_html(body, body_footer, plaintext=False, container_tag='div')
+        # generate footer
+        link = self._get_partner_access_link(cr, uid, mail, partner, context=context)
+        if link:
+            body = tools.append_content_to_html(body, link, plaintext=False, container_tag='div')
         return body
 
     def send_get_email_dict(self, cr, uid, mail, partner=None, context=None):
@@ -274,10 +211,9 @@ class mail_mail(osv.Model):
         # 2. if 'partner' is specified, but no related document: Partner Name <email>
         # 3; fallback on mail.email_to that we split to have an email addresses list
         if partner and mail.record_name:
-            sanitized_record_name = re.sub(r'[^\w+.]+', '-', mail.record_name)
-            email_to = [_('"Followers of %s" <%s>') % (sanitized_record_name, partner.email)]
+            email_to = [formataddr((_('Followers of %s') % mail.record_name, partner.email))]
         elif partner:
-            email_to = ['%s <%s>' % (partner.name, partner.email)]
+            email_to = [formataddr((partner.name, partner.email))]
         else:
             email_to = tools.email_split(mail.email_to)
 
@@ -288,7 +224,7 @@ class mail_mail(osv.Model):
             'email_to': email_to,
         }
 
-    def send(self, cr, uid, ids, auto_commit=False, context=None):
+    def send(self, cr, uid, ids, auto_commit=False, raise_exception=False, context=None):
         """ Sends the selected emails immediately, ignoring their current
             state (mails that have already been sent should not be passed
             unless they should actually be re-sent).
@@ -299,40 +235,70 @@ class mail_mail(osv.Model):
             :param bool auto_commit: whether to force a commit of the mail status
                 after sending each mail (meant only for scheduler processing);
                 should never be True during normal transactions (default: False)
+            :param bool raise_exception: whether to raise an exception if the
+                email sending process has failed
             :return: True
         """
         ir_mail_server = self.pool.get('ir.mail_server')
+        ir_attachment = self.pool['ir.attachment']
+
         for mail in self.browse(cr, SUPERUSER_ID, ids, context=context):
             try:
-                # handle attachments
-                attachments = []
-                for attach in mail.attachment_ids:
-                    attachments.append((attach.datas_fname, base64.b64decode(attach.datas)))
+                # load attachment binary data with a separate read(), as prefetching all
+                # `datas` (binary field) could bloat the browse cache, triggerring
+                # soft/hard mem limits with temporary data.
+                attachment_ids = [a.id for a in mail.attachment_ids]
+                attachments = [(a['datas_fname'], base64.b64decode(a['datas']))
+                                 for a in ir_attachment.read(cr, SUPERUSER_ID, attachment_ids,
+                                                             ['datas_fname', 'datas'])]
                 # specific behavior to customize the send email for notified partners
                 email_list = []
                 if mail.email_to:
                     email_list.append(self.send_get_email_dict(cr, uid, mail, context=context))
                 for partner in mail.recipient_ids:
                     email_list.append(self.send_get_email_dict(cr, uid, mail, partner=partner, context=context))
+                # headers
+                headers = {}
+                bounce_alias = self.pool['ir.config_parameter'].get_param(cr, uid, "mail.bounce.alias", context=context)
+                catchall_domain = self.pool['ir.config_parameter'].get_param(cr, uid, "mail.catchall.domain", context=context)
+                if bounce_alias and catchall_domain:
+                    if mail.model and mail.res_id:
+                        headers['Return-Path'] = '%s-%d-%s-%d@%s' % (bounce_alias, mail.id, mail.model, mail.res_id, catchall_domain)
+                    else:
+                        headers['Return-Path'] = '%s-%d@%s' % (bounce_alias, mail.id, catchall_domain)
 
                 # build an RFC2822 email.message.Message object and send it without queuing
+                res = None
                 for email in email_list:
                     msg = ir_mail_server.build_email(
-                        email_from = mail.email_from,
-                        email_to = email.get('email_to'),
-                        subject = email.get('subject'),
-                        body = email.get('body'),
-                        body_alternative = email.get('body_alternative'),
-                        email_cc = tools.email_split(mail.email_cc),
-                        reply_to = mail.reply_to,
-                        attachments = attachments,
-                        message_id = mail.message_id,
-                        references = mail.references,
-                        object_id = mail.res_id and ('%s-%s' % (mail.res_id, mail.model)),
-                        subtype = 'html',
-                        subtype_alternative = 'plain')
-                    res = ir_mail_server.send_email(cr, uid, msg,
-                        mail_server_id=mail.mail_server_id.id, context=context)
+                        email_from=mail.email_from,
+                        email_to=email.get('email_to'),
+                        subject=email.get('subject'),
+                        body=email.get('body'),
+                        body_alternative=email.get('body_alternative'),
+                        email_cc=tools.email_split(mail.email_cc),
+                        reply_to=mail.reply_to,
+                        attachments=attachments,
+                        message_id=mail.message_id,
+                        references=mail.references,
+                        object_id=mail.res_id and ('%s-%s' % (mail.res_id, mail.model)),
+                        subtype='html',
+                        subtype_alternative='plain',
+                        headers=headers)
+                    try:
+                        res = ir_mail_server.send_email(cr, uid, msg,
+                                                    mail_server_id=mail.mail_server_id.id,
+                                                    context=context)
+                    except AssertionError as error:
+                        if error.message == ir_mail_server.NO_VALID_RECIPIENT:
+                            # No valid recipient found for this particular
+                            # mail item -> ignore error to avoid blocking
+                            # delivery to next recipients, if any. If this is
+                            # the only recipient, the mail will show as failed.
+                            _logger.warning("Ignoring invalid recipients for mail.mail %s: %s",
+                                            mail.message_id, email.get('email_to'))
+                        else:
+                            raise
                 if res:
                     mail.write({'state': 'sent', 'message_id': res})
                     mail_sent = True
@@ -343,10 +309,25 @@ class mail_mail(osv.Model):
                 # /!\ can't use mail.state here, as mail.refresh() will cause an error
                 # see revid:odo@openerp.com-20120622152536-42b2s28lvdv3odyr in 6.1
                 if mail_sent:
+                    _logger.info('Mail with ID %r and Message-Id %r successfully sent', mail.id, mail.message_id)
                     self._postprocess_sent_message(cr, uid, mail, context=context)
-            except Exception:
+            except MemoryError:
+                # prevent catching transient MemoryErrors, bubble up to notify user or abort cron job
+                # instead of marking the mail as failed
+                _logger.exception('MemoryError while processing mail with ID %r and Msg-Id %r. '\
+                                      'Consider raising the --limit-memory-hard startup option',
+                                  mail.id, mail.message_id)
+                raise
+            except Exception as e:
                 _logger.exception('failed sending mail.mail %s', mail.id)
                 mail.write({'state': 'exception'})
+                if raise_exception:
+                    if isinstance(e, AssertionError):
+                        # get the args of the original error, wrap into a value and throw a MailDeliveryException
+                        # that is an except_orm, with name and value as arguments
+                        value = '. '.join(e.args)
+                        raise MailDeliveryException(_("Mail Delivery Failed"), value)
+                    raise
 
             if auto_commit == True:
                 cr.commit()