[MERGE] merge with latest stable
[odoo/odoo.git] / addons / email_template / email_template_mailbox.py
index 3f52b93..49d61bf 100644 (file)
@@ -40,12 +40,12 @@ class email_template_mailbox(osv.osv):
         to periodically send emails
         """
         try:
-            self.send_all_mail(cursor, user, context)
+            self.send_all_mail(cursor, user, context=context)
         except Exception, e:
             LOGGER.notifyChannel(
-                                 _("Email Template"),
+                                 "Email Template",
                                  netsvc.LOG_ERROR,
-                                 _("Error sending mail: %s" % str(e)))
+                                 _("Error sending mail: %s") % e)
         
     def send_all_mail(self, cr, uid, ids=None, context=None):
         if ids is None:
@@ -60,9 +60,11 @@ class email_template_mailbox(osv.osv):
         self.write(cr, uid, ids, {'state':'sending'}, context)
         self.send_this_mail(cr, uid, ids, context)
         return True
-    
+
     def send_this_mail(self, cr, uid, ids=None, context=None):
+        #previous method to send email (link with email account can be found at the revision 4172 and below
         result = True
+        attachment_pool = self.pool.get('ir.attachment')
         for id in (ids or []):
             try:
                 account_obj = self.pool.get('email_template.account')
@@ -70,7 +72,7 @@ class email_template_mailbox(osv.osv):
                 payload = {}
                 if values['attachments_ids']:
                     for attid in values['attachments_ids']:
-                        attachment = self.pool.get('ir.attachment').browse(cr, uid, attid, context)#,['datas_fname','datas'])
+                        attachment = attachment_pool.browse(cr, uid, attid, context)#,['datas_fname','datas'])
                         payload[attachment.datas_fname] = attachment.datas
                 result = account_obj.send_mail(cr, uid,
                               [values['account_id'][0]],
@@ -81,11 +83,18 @@ class email_template_mailbox(osv.osv):
                               values['subject'] or u'',
                               {'text':values.get('body_text') or u'', 'html':values.get('body_html') or u''},
                               payload=payload,
-                              message_id=values['message_id'], 
+                              message_id=values['message_id'],
                               context=context)
                 if result == True:
-                    self.write(cr, uid, id, {'folder':'sent', 'state':'na', 'date_mail':time.strftime("%Y-%m-%d %H:%M:%S")}, context)
-                    self.historise(cr, uid, [id], "Email sent successfully", context)
+                    account = account_obj.browse(cr, uid, values['account_id'][0], context=context)
+                    if account.auto_delete:
+                        self.write(cr, uid, id, {'folder': 'trash'}, context=context)
+                        self.unlink(cr, uid, [id], context=context)
+                        # Remove attachments for this mail
+                        attachment_pool.unlink(cr, uid, values['attachments_ids'], context=context)
+                    else:
+                        self.write(cr, uid, id, {'folder':'sent', 'state':'na', 'date_mail':time.strftime("%Y-%m-%d %H:%M:%S")}, context)
+                        self.historise(cr, uid, [id], "Email sent successfully", context)
                 else:
                     error = result['error_msg']
                     self.historise(cr, uid, [id], error, context)
@@ -188,8 +197,6 @@ class email_template_mailbox(osv.osv):
         It just changes the folder of the item to "Trash", if it is no in Trash folder yet, 
         or completely deletes it if it is already in Trash.
         """
-        if not context:
-            context = {}
         to_update = []
         to_remove = []
         for mail in self.browse(cr, uid, ids, context=context):