[MERGE] merge from trunk addons
[odoo/odoo.git] / addons / email_template / email_template_mailbox.py
old mode 100644 (file)
new mode 100755 (executable)
index 015d8ec..7483ade
@@ -25,6 +25,7 @@ import time
 import netsvc
 from tools.translate import _
 import tools
+import base64
 
 LOGGER = netsvc.Logger()
 
@@ -40,12 +41,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:
@@ -62,30 +63,50 @@ class email_template_mailbox(osv.osv):
         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')
                 values = self.read(cr, uid, id, [], context) 
-                payload = {}
+                attach_to_send = None
+                
                 if values['attachments_ids']:
-                    for attid in values['attachments_ids']:
-                        attachment = self.pool.get('ir.attachment').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]],
-                              {'To':values.get('email_to') or u'',
-                               'CC':values.get('email_cc') or u'',
-                               'BCC':values.get('email_bcc') or u'',
-                               'Reply-To':values.get('reply_to') or u''},
-                              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'], 
-                              context=context)
+                    attach_to_send = self.pool.get('ir.attachment').read(cr, uid, values['attachments_ids'], ['datas_fname','datas', 'name'])
+                    attach_to_send = map(lambda x: (x['datas_fname'] or x['name'], base64.decodestring(x['datas'])), attach_to_send)
+                
+                if values.get('body_html'):
+                    body = values.get('body_html')
+                    subtype = "html"
+                else :
+                    body = values.get('body_text')
+                    subtype = "plain"
+                    
+                result = tools.email_send(
+                    values.get('email_from') or u'',
+                    [values.get('email_to')],
+                    values['subject'] or u'', 
+                    body or u'',
+                    reply_to=values.get('reply_to') or u'',
+                    email_bcc=values.get('email_bcc') or u'',
+                    email_cc=values.get('email_cc') or u'',
+                    subtype=subtype,
+                    attach=attach_to_send,
+                    openobject_id=values['message_id']
+                )
+                
+
                 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)
@@ -100,7 +121,7 @@ class email_template_mailbox(osv.osv):
     def historise(self, cr, uid, ids, message='', context=None):
         for id in ids:
             history = self.read(cr, uid, id, ['history'], context).get('history', '')
-            self.write(cr, uid, id, {'history':history or '' + "\n" + time.strftime("%Y-%m-%d %H:%M:%S") + ": " + tools.ustr(message)}, context)
+            self.write(cr, uid, id, {'history': (history or '' )+ "\n" + time.strftime("%Y-%m-%d %H:%M:%S") + ": " + tools.ustr(message)}, context)
     
     _columns = {
             'email_from':fields.char(
@@ -171,8 +192,7 @@ class email_template_mailbox(osv.osv):
                             ('na', 'Not Applicable'),
                             ('sending', 'Sending'),
                             ], 'Status', required=True),
-            'date_mail':fields.datetime(
-                            'Rec/Sent Date'),
+            'date_mail':fields.datetime('Rec/Sent Date', help="Date on which Email Sent or Received"),
             'history':fields.text(
                             'History', 
                             readonly=True, 
@@ -184,6 +204,22 @@ class email_template_mailbox(osv.osv):
         'folder': lambda * a: 'outbox',
     } 
 
+    def unlink(self, cr, uid, ids, context=None):
+        """
+        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.
+        """
+        to_update = []
+        to_remove = []
+        for mail in self.browse(cr, uid, ids, context=context):
+            if mail.folder == 'trash':
+                to_remove.append(mail.id)
+            else:
+                to_update.append(mail.id)
+        # Changes the folder to trash
+        self.write(cr, uid, to_update, {'folder': 'trash'}, context=context)
+        return super(email_template_mailbox, self).unlink(cr, uid, to_remove, context=context)
+
 email_template_mailbox()
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: