← Back to team overview

openerp-dev-web team mailing list archive

[Merge] lp:~openerp-dev/openobject-addons/addons_merge_mail_wizard_rha into lp:openobject-addons

 

Rifakat Haradwala (OpenERP) has proposed merging lp:~openerp-dev/openobject-addons/addons_merge_mail_wizard_rha into lp:openobject-addons.

Requested reviews:
  OpenERP Core Team (openerp)

For more details, see:
https://code.launchpad.net/~openerp-dev/openobject-addons/addons_merge_mail_wizard_rha/+merge/57154

-changes for email.compose.message fields subject and description
-improvements for mass mailing
-- 
The attached diff has been truncated due to its size.
https://code.launchpad.net/~openerp-dev/openobject-addons/addons_merge_mail_wizard_rha/+merge/57154
Your team OpenERP R&D Team is subscribed to branch lp:~openerp-dev/openobject-addons/addons_merge_mail_wizard_rha.
=== modified file 'account_followup/__openerp__.py'
--- account_followup/__openerp__.py	2011-03-25 10:24:13 +0000
+++ account_followup/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -44,7 +44,7 @@
     'author': 'OpenERP SA',
     'website': 'http://www.openerp.com',
     'images': ['images/follow_ups.jpeg','images/send_followups.jpeg'],
-    'depends': ['account'],
+    'depends': ['account', 'mail'],
     'init_xml': [],
     'update_xml': [
         'security/ir.model.access.csv',

=== modified file 'account_followup/wizard/account_followup_print.py'
--- account_followup/wizard/account_followup_print.py	2011-03-17 17:33:16 +0000
+++ account_followup/wizard/account_followup_print.py	2011-04-11 12:37:32 +0000
@@ -205,6 +205,7 @@
         move_obj = self.pool.get('account.move.line')
         user_obj = self.pool.get('res.users')
         line_obj = self.pool.get('account_followup.stat')
+        email_message_obj = self.pool.get('email.message')
 
         if context is None:
             context = {}
@@ -277,7 +278,7 @@
                 msg = ''
                 if dest:
                     try:
-                        tools.email_send(src, dest, sub, body)
+                        email_message_obj.schedule_with_attach(cr, uid, src, dest, sub, body, model='account.followup.print.all')
                         msg_sent += partner.name + '\n'
                     except Exception, e:
                         raise osv.except_osv('Error !', e )

=== modified file 'base_action_rule/__openerp__.py'
--- base_action_rule/__openerp__.py	2011-03-25 10:24:13 +0000
+++ base_action_rule/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -36,7 +36,7 @@
     """,
     'author': 'OpenERP SA',
     'website': 'http://www.openerp.com',
-    'depends': ['base'],
+    'depends': ['base', 'mail'],
     'init_xml': [
         'base_action_rule_data.xml'
     ],

=== modified file 'base_action_rule/base_action_rule.py'
--- base_action_rule/base_action_rule.py	2011-03-04 12:53:34 +0000
+++ base_action_rule/base_action_rule.py	2011-04-11 12:37:32 +0000
@@ -24,7 +24,7 @@
 from datetime import datetime
 from datetime import timedelta
 from tools.safe_eval import safe_eval
-import pooler 
+import pooler
 import re
 import time
 import tools
@@ -39,7 +39,7 @@
 
     _name = 'base.action.rule'
     _description = 'Action Rules'
-    
+
     def _state_get(self, cr, uid, context=None):
         """ Get State
             @param self: The object pointer
@@ -55,7 +55,7 @@
             @param uid: the current user’s ID for security checks,
             @param context: A standard dictionary for contextual values """
         return [('', '')]
-  
+
     def priority_get(self, cr, uid, context=None):
         """ Get Priority
             @param self: The object pointer
@@ -65,57 +65,57 @@
         return [('', '')]
 
     _columns = {
-        'name':  fields.char('Rule Name', size=64, required=True), 
-        'model_id': fields.many2one('ir.model', 'Object', required=True), 
-        'create_date': fields.datetime('Create Date', readonly=1), 
+        'name':  fields.char('Rule Name', size=64, required=True),
+        'model_id': fields.many2one('ir.model', 'Object', required=True),
+        'create_date': fields.datetime('Create Date', readonly=1),
         'active': fields.boolean('Active', help="If the active field is set to False,\
- it will allow you to hide the rule without removing it."), 
+ it will allow you to hide the rule without removing it."),
         'sequence': fields.integer('Sequence', help="Gives the sequence order \
-when displaying a list of rules."), 
+when displaying a list of rules."),
         'trg_date_type':  fields.selection([
-            ('none', 'None'), 
-            ('create', 'Creation Date'), 
-            ('action_last', 'Last Action Date'), 
-            ('date', 'Date'), 
-            ('deadline', 'Deadline'), 
-            ], 'Trigger Date', size=16), 
+            ('none', 'None'),
+            ('create', 'Creation Date'),
+            ('action_last', 'Last Action Date'),
+            ('date', 'Date'),
+            ('deadline', 'Deadline'),
+            ], 'Trigger Date', size=16),
         'trg_date_range': fields.integer('Delay after trigger date', \
                                          help="Delay After Trigger Date,\
 specifies you can put a negative number. If you need a delay before the \
-trigger date, like sending a reminder 15 minutes before a meeting."), 
+trigger date, like sending a reminder 15 minutes before a meeting."),
         'trg_date_range_type': fields.selection([('minutes', 'Minutes'), ('hour', 'Hours'), \
-                                ('day', 'Days'), ('month', 'Months')], 'Delay type'), 
-        'trg_user_id':  fields.many2one('res.users', 'Responsible'), 
-        'trg_partner_id': fields.many2one('res.partner', 'Partner'), 
-        'trg_partner_categ_id': fields.many2one('res.partner.category', 'Partner Category'), 
-        'trg_state_from': fields.selection(_state_get, 'State', size=16), 
-        'trg_state_to': fields.selection(_state_get, 'Button Pressed', size=16), 
+                                ('day', 'Days'), ('month', 'Months')], 'Delay type'),
+        'trg_user_id':  fields.many2one('res.users', 'Responsible'),
+        'trg_partner_id': fields.many2one('res.partner', 'Partner'),
+        'trg_partner_categ_id': fields.many2one('res.partner.category', 'Partner Category'),
+        'trg_state_from': fields.selection(_state_get, 'State', size=16),
+        'trg_state_to': fields.selection(_state_get, 'Button Pressed', size=16),
 
-        'act_method': fields.char('Call Object Method', size=64), 
-        'act_user_id': fields.many2one('res.users', 'Set Responsible to'), 
-        'act_state': fields.selection(_state_get, 'Set State to', size=16), 
+        'act_method': fields.char('Call Object Method', size=64),
+        'act_user_id': fields.many2one('res.users', 'Set Responsible to'),
+        'act_state': fields.selection(_state_get, 'Set State to', size=16),
         'act_email_cc': fields.char('Add Watchers (Cc)', size=250, help="\
 These people will receive a copy of the future communication between partner \
-and users by email"), 
+and users by email"),
         'act_remind_partner': fields.boolean('Remind Partner', help="Check \
-this if you want the rule to send a reminder by email to the partner."), 
+this if you want the rule to send a reminder by email to the partner."),
         'act_remind_user': fields.boolean('Remind Responsible', help="Check \
-this if you want the rule to send a reminder by email to the user."), 
-        'act_reply_to': fields.char('Reply-To', size=64), 
-        'act_remind_attach': fields.boolean('Remind with Attachment', help="Check this if you want that all documents attached to the object be attached to the reminder email sent."), 
+this if you want the rule to send a reminder by email to the user."),
+        'act_reply_to': fields.char('Reply-To', size=64),
+        'act_remind_attach': fields.boolean('Remind with Attachment', help="Check this if you want that all documents attached to the object be attached to the reminder email sent."),
         'act_mail_to_user': fields.boolean('Mail to Responsible', help="Check\
- this if you want the rule to send an email to the responsible person."), 
-        'act_mail_to_watchers': fields.boolean('Mail to Watchers (CC)', 
+ this if you want the rule to send an email to the responsible person."),
+        'act_mail_to_watchers': fields.boolean('Mail to Watchers (CC)',
                                                 help="Check this if you want \
-the rule to mark CC(mail to any other person defined in actions)."), 
+the rule to mark CC(mail to any other person defined in actions)."),
         'act_mail_to_email': fields.char('Mail to these Emails', size=128, \
-        help="Email-id of the persons whom mail is to be sent"), 
-        'act_mail_body': fields.text('Mail body', help="Content of mail"), 
+        help="Email-id of the persons whom mail is to be sent"),
+        'act_mail_body': fields.text('Mail body', help="Content of mail"),
         'regex_name': fields.char('Regex on Resource Name', size=128, help="Regular expression for matching name of the resource\
 \ne.g.: 'urgent.*' will search for records having name starting with the string 'urgent'\
-\nNote: This is case sensitive search."), 
-        'server_action_id': fields.many2one('ir.actions.server', 'Server Action', help="Describes the action name.\neg:on which object which action to be taken on basis of which condition"), 
-        'filter_id':fields.many2one('ir.filters', 'Filter', required=False), 
+\nNote: This is case sensitive search."),
+        'server_action_id': fields.many2one('ir.actions.server', 'Server Action', help="Describes the action name.\neg:on which object which action to be taken on basis of which condition"),
+        'filter_id':fields.many2one('ir.filters', 'Filter', required=False),
         'act_email_from' : fields.char('Email From', size=64, required=False,
                 help="Use a python expression to specify the right field on which one than we will use for the 'From' field of the header"),
         'act_email_to' : fields.char('Email To', size=64, required=False,
@@ -124,17 +124,17 @@
     }
 
     _defaults = {
-        'active': lambda *a: True, 
-        'trg_date_type': lambda *a: 'none', 
-        'trg_date_range_type': lambda *a: 'day', 
-        'act_mail_to_user': lambda *a: 0, 
-        'act_remind_partner': lambda *a: 0, 
-        'act_remind_user': lambda *a: 0, 
-        'act_mail_to_watchers': lambda *a: 0, 
+        'active': lambda *a: True,
+        'trg_date_type': lambda *a: 'none',
+        'trg_date_range_type': lambda *a: 'day',
+        'act_mail_to_user': lambda *a: 0,
+        'act_remind_partner': lambda *a: 0,
+        'act_remind_user': lambda *a: 0,
+        'act_mail_to_watchers': lambda *a: 0,
     }
-    
+
     _order = 'sequence'
-    
+
     def onchange_model_id(self, cr, uid, ids, name):
         #This is not a good solution as it will affect the domain only on onchange
         res = {'domain':{'filter_id':[]}}
@@ -174,7 +174,7 @@
                 self.pre_action(cr, uid, [new_id], model, context=context)
             return new_id
         return make_call_old
-    
+
     def _write(self, old_write, model, context=None):
         if context is None:
             context  = {}
@@ -202,9 +202,9 @@
         return True
     def create(self, cr, uid, vals, context=None):
         res_id = super(base_action_rule, self).create(cr, uid, vals, context=context)
-        self._register_hook(cr, uid, [res_id], context=context)        
+        self._register_hook(cr, uid, [res_id], context=context)
         return res_id
-    
+
     def write(self, cr, uid, ids, vals, context=None):
         res = super(base_action_rule, self).write(cr, uid, ids, vals, context=context)
         self._register_hook(cr, uid, ids, context=context)
@@ -272,18 +272,18 @@
             @param self: The object pointer """
 
         data = {
-            'object_id': obj.id, 
-            'object_subject': hasattr(obj, 'name') and obj.name or False, 
-            'object_date': hasattr(obj, 'date') and obj.date or False, 
-            'object_description': hasattr(obj, 'description') and obj.description or False, 
-            'object_user': hasattr(obj, 'user_id') and (obj.user_id and obj.user_id.name) or '/', 
+            'object_id': obj.id,
+            'object_subject': hasattr(obj, 'name') and obj.name or False,
+            'object_date': hasattr(obj, 'date') and obj.date or False,
+            'object_description': hasattr(obj, 'description') and obj.description or False,
+            'object_user': hasattr(obj, 'user_id') and (obj.user_id and obj.user_id.name) or '/',
             'object_user_email': hasattr(obj, 'user_id') and (obj.user_id and \
-                                    obj.user_id.address_id and obj.user_id.address_id.email) or '/', 
+                                    obj.user_id.address_id and obj.user_id.address_id.email) or '/',
             'object_user_phone': hasattr(obj, 'user_id') and (obj.user_id and\
-                                     obj.user_id.address_id and obj.user_id.address_id.phone) or '/', 
-            'partner': hasattr(obj, 'partner_id') and (obj.partner_id and obj.partner_id.name) or '/', 
+                                     obj.user_id.address_id and obj.user_id.address_id.phone) or '/',
+            'partner': hasattr(obj, 'partner_id') and (obj.partner_id and obj.partner_id.name) or '/',
             'partner_email': hasattr(obj, 'partner_address_id') and (obj.partner_address_id and\
-                                         obj.partner_address_id.email) or '/', 
+                                         obj.partner_address_id.email) or '/',
         }
         return self.format_body(body % data)
 
@@ -302,6 +302,7 @@
         if context is None:
             context = {}
 
+        email_message_obj = self.pool.get('email.message')
         body = self.format_mail(obj, body)
         if not emailfrom:
             if hasattr(obj, 'user_id')  and obj.user_id and obj.user_id.address_id and\
@@ -312,9 +313,9 @@
         emailfrom = tools.ustr(emailfrom)
         reply_to = emailfrom
         if not emailfrom:
-            raise osv.except_osv(_('Error!'), 
+            raise osv.except_osv(_('Error!'),
                     _("No E-Mail ID Found for your Company address!"))
-        return tools.email_send(emailfrom, emails, name, body, reply_to=reply_to, openobject_id=str(obj.id))
+        return email_message_obj.schedule_with_attach(cr, uid, emailfrom, emails, name, body, model='base.action.rule', reply_to=reply_to, openobject_id=str(obj.id))
 
 
     def do_check(self, cr, uid, action, obj, context=None):
@@ -325,7 +326,7 @@
             @param context: A standard dictionary for contextual values """
         if context is None:
             context = {}
-        ok = True 
+        ok = True
         if action.filter_id:
             if action.model_id.model == action.filter_id.model_id:
                 context.update(eval(action.filter_id.context))
@@ -480,15 +481,15 @@
         return True
 
     _constraints = [
-        (_check_mail, 'Error: The mail is not well formated', ['act_mail_body']), 
+        (_check_mail, 'Error: The mail is not well formated', ['act_mail_body']),
     ]
 
 base_action_rule()
 
 
 class ir_cron(osv.osv):
-    _inherit = 'ir.cron' 
-    
+    _inherit = 'ir.cron'
+
     def _poolJobs(self, db_name, check=False):
         try:
             db = pooler.get_db(db_name)

=== modified file 'base_calendar/__openerp__.py'
--- base_calendar/__openerp__.py	2011-03-18 14:06:18 +0000
+++ base_calendar/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -22,7 +22,7 @@
 {
     "name" : "Basic Calendar Functionality",
     "version" : "1.0",
-    "depends" : ["base"],
+    "depends" : ["base", "mail"],
     'description': """
 This is a full-featured calendar system.
 ========================================

=== modified file 'base_calendar/base_calendar.py'
--- base_calendar/base_calendar.py	2011-02-18 10:09:57 +0000
+++ base_calendar/base_calendar.py	2011-04-11 12:37:32 +0000
@@ -411,7 +411,7 @@
         cal = vobject.iCalendar()
         event = cal.add('vevent')
         if not event_obj.date_deadline or not event_obj.date:
-              raise osv.except_osv(_('Warning !'),_("Couldn't Invite because date is not specified!"))     
+              raise osv.except_osv(_('Warning !'),_("Couldn't Invite because date is not specified!"))
         event.add('created').value = ics_datetime(time.strftime('%Y-%m-%d %H:%M:%S'))
         event.add('dtstart').value = ics_datetime(event_obj.date)
         event.add('dtend').value = ics_datetime(event_obj.date_deadline)
@@ -456,7 +456,7 @@
             trigger.value = delta
             # Compute other details
             valarm.add('DESCRIPTION').value = alarm_data['name'] or 'OpenERP'
-                
+
         for attendee in event_obj.attendee_ids:
             attendee_add = event.add('attendee')
             attendee_add.params['CUTYPE'] = [str(attendee.cutype)]
@@ -480,6 +480,7 @@
             context = {}
 
         company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.name
+        email_message_obj = self.pool.get('email.message')
         for att in self.browse(cr, uid, ids, context=context):
             sign = att.sent_by_uid and att.sent_by_uid.signature or ''
             sign = '<br>'.join(sign and sign.split('\n') or [])
@@ -506,11 +507,12 @@
                 body = html_invitation % body_vals
                 if mail_to and email_from:
                     attach = self.get_ics_file(cr, uid, res_obj, context=context)
-                    tools.email_send(
+                    email_message_obj.schedule_with_attach(cr, uid,
                         email_from,
                         mail_to,
                         sub,
                         body,
+                        model='calendar.attendee',
                         attach=attach and [('invitation.ics', attach)] or None,
                         subtype='html',
                         reply_to=email_from
@@ -674,7 +676,7 @@
                     new_res_alarm = alarm_ids[0]
                 cr.execute('UPDATE %s ' % model_obj._table + \
                             ' SET base_calendar_alarm_id=%s, alarm_id=%s ' \
-                            ' WHERE id=%s', 
+                            ' WHERE id=%s',
                             (cal_alarm.id, new_res_alarm, data.id))
 
             self.do_alarm_unlink(cr, uid, [data.id], model)
@@ -809,6 +811,7 @@
         return True # XXX FIXME REMOVE THIS AFTER FIXING get_recurrent_dates!!
         if context is None:
             context = {}
+        email_message_obj = self.pool.get('email.message')
         current_datetime = datetime.now()
         request_obj = self.pool.get('res.request')
         alarm_ids = self.search(cr, uid, [('state', '!=', 'done')], context=context)
@@ -890,11 +893,12 @@
                     for att in alarm.attendee_ids:
                         mail_to.append(att.user_id.address_id.email)
                     if mail_to:
-                        tools.email_send(
+                        email_message_obj.schedule_with_attach(cr, uid,
                             tools.config.get('email_from', False),
                             mail_to,
                             sub,
-                            body
+                            body,
+                            model='calendar.alarm'
                         )
             if next_trigger_date:
                 update_vals.update({'trigger_date': next_trigger_date})
@@ -966,7 +970,7 @@
             value['date_deadline'] = end.strftime("%Y-%m-%d %H:%M:%S")
         elif end_date and duration and not allday:
             # we have both, keep them synchronized:
-            # set duration based on end_date (arbitrary decision: this avoid 
+            # set duration based on end_date (arbitrary decision: this avoid
             # getting dates like 06:31:48 instead of 06:32:00)
             end = datetime.strptime(end_date, "%Y-%m-%d %H:%M:%S")
             diff = end - start
@@ -1130,7 +1134,7 @@
         FREQ=MONTHLY;INTERVAL=2;COUNT=10;BYDAY=-1SU'),
         'rrule_type': fields.selection([('none', ''), ('daily', 'Daily'), \
                             ('weekly', 'Weekly'), ('monthly', 'Monthly'), \
-                            ('yearly', 'Yearly'),], 
+                            ('yearly', 'Yearly'),],
                             'Recurrency', states={'done': [('readonly', True)]},
                             help="Let the event automatically repeat at that interval"),
         'alarm_id': fields.many2one('res.alarm', 'Alarm', states={'done': [('readonly', True)]},
@@ -1148,7 +1152,7 @@
                                 ('weekly', 'Weeks'),
                                 ('monthly', 'Months'),
                                 ('yearly', 'Years'), ], 'Frequency'),
-          
+
         'end_type' : fields.selection([('forever', 'Forever'), ('count', 'Fix amout of times'), ('end_date','End date')], 'Way to end reccurency'),
         'interval': fields.integer('Repeat every', help="Repeat every (Days/Week/Month/Year)"),
         'count': fields.integer('Repeat', help="Repeat x times"),
@@ -1159,7 +1163,7 @@
         'fr': fields.boolean('Fri'),
         'sa': fields.boolean('Sat'),
         'su': fields.boolean('Sun'),
-        'select1': fields.selection([('date', 'Date of month'), 
+        'select1': fields.selection([('date', 'Date of month'),
                                     ('day', 'Day of month')], 'Option'),
         'day': fields.integer('Date of month'),
         'week_list': fields.selection([('MO', 'Monday'), ('TU', 'Tuesday'), \
@@ -1176,8 +1180,8 @@
         'allday': fields.boolean('All Day', states={'done': [('readonly', True)]}),
         'active': fields.boolean('Active', help="If the active field is set to \
          true, it will allow you to hide the event alarm information without removing it."),
-        'recurrency': fields.boolean('Recurrent', help="Recurrent Meeting"),                                    
-        'edit_all': fields.boolean('Edit All', help="Edit all Occurrences  of recurrent Meeting."),        
+        'recurrency': fields.boolean('Recurrent', help="Recurrent Meeting"),
+        'edit_all': fields.boolean('Edit All', help="Edit all Occurrences  of recurrent Meeting."),
     }
     def default_organizer(self, cr, uid, context=None):
         user_pool = self.pool.get('res.users')
@@ -1204,12 +1208,12 @@
     def onchange_edit_all(self, cr, uid, ids, rrule_type,edit_all, context=None):
         if not context:
             context = {}
-    
+
         value = {}
         if edit_all and rrule_type:
             for id in ids:
               base_calendar_id2real_id(id)
-        return value              
+        return value
 
     def modify_all(self, cr, uid, event_ids, defaults, context=None, *args):
         """
@@ -1342,7 +1346,7 @@
         freq=datas.get('rrule_type')
         if  freq == 'none':
             return ''
-            
+
         interval_srting = datas.get('interval') and (';INTERVAL=' + str(datas.get('interval'))) or ''
 
         if freq == 'weekly':
@@ -1358,7 +1362,7 @@
             elif datas.get('select1')=='date':
                 monthstring = ';BYMONTHDAY=' + str(datas.get('day'))
 
-       
+
         if datas.get('end_date'):
             datas['end_date'] = ''.join((re.compile('\d')).findall(datas.get('end_date'))) + 'T235959Z'
         enddate = (datas.get('count') and (';COUNT=' + str(datas.get('count'))) or '') +\
@@ -1403,7 +1407,7 @@
 
         res = self.get_recurrent_ids(cr, uid, res, start_date, until_date, limit)
         return res
-    
+
 
     def get_edit_all(self, cr, uid, id, vals=None):
         """
@@ -1414,10 +1418,10 @@
         if(vals and 'edit_all' in vals): #we jsut check edit_all
             return vals['edit_all']
         else: #it's a recurrent event and edit_all is already check
-            return meeting['recurrency'] and meeting['edit_all'] 
-
-
-        
+            return meeting['recurrency'] and meeting['edit_all']
+
+
+
 
     def write(self, cr, uid, ids, vals, context=None, check=True, update_check=True):
         """
@@ -1440,12 +1444,12 @@
         res = False
         for event_id in select:
             real_event_id = base_calendar_id2real_id(event_id)
-            
+
 
             if(self.get_edit_all(cr, uid, event_id, vals=vals)):
                 event_id = real_event_id
-            
-            
+
+
             if len(str(event_id).split('-')) > 1:
                 data = self.read(cr, uid, event_id, ['date', 'date_deadline', \
                                                     'rrule', 'duration', 'exdate'])
@@ -1459,15 +1463,15 @@
                         'edit_all': False,
                         'recurrency' : False,
                         })
-                    
+
                     new_id = self.copy(cr, uid, real_event_id, default=data, context=context)
-                    
+
                     date_new = event_id.split('-')[1]
                     date_new = time.strftime("%Y%m%dT%H%M%S", \
                                  time.strptime(date_new, "%Y%m%d%H%M%S"))
                     exdate = (data['exdate'] and (data['exdate'] + ',')  or '') + date_new
                     res = self.write(cr, uid, [real_event_id], {'exdate': exdate})
-                    
+
                     context.update({'active_id': new_id, 'active_ids': [new_id]})
                     continue
             if not real_event_id in new_ids:
@@ -1590,10 +1594,10 @@
         res = False
         for event_datas in self.read(cr, uid, ids, ['date', 'rrule', 'exdate'], context=context):
             event_id = event_datas['id']
-            
+
             if self.get_edit_all(cr, uid, event_id, vals=None):
                 event_id = base_calendar_id2real_id(event_id)
-            
+
             if isinstance(event_id, (int, long)):
                 res = super(calendar_event, self).unlink(cr, uid, event_id, context=context)
                 self.pool.get('res.alarm').do_alarm_unlink(cr, uid, [event_id], self._name)
@@ -1711,7 +1715,7 @@
         @param args: list of tuples of form [(‘name_of_the_field’, ‘operator’, value), ...].
         @param context: A standard dictionary for contextual values
         """
-        
+
         assert name == 'date'
         return self.write(cr, uid, id, { 'date_start': value }, context=context)
 
@@ -1743,9 +1747,9 @@
         for arg in args:
             args1.append(map(lambda x:str(x).split('-')[0], arg))
         return super(ir_attachment, self).search_count(cr, user, args1, context)
-        
-        
-    
+
+
+
     def create(self, cr, uid, vals, context=None):
         if context:
             id = context.get('default_res_id', False)

=== modified file 'crm/__openerp__.py'
--- crm/__openerp__.py	2011-03-25 10:24:13 +0000
+++ crm/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -57,7 +57,7 @@
         'base',
         'base_action_rule',
         'process',
-        'mail_gateway',
+        'mail',
         'base_calendar',
         'resource',
         'board'
@@ -84,7 +84,7 @@
         'wizard/crm_opportunity_to_phonecall_view.xml',
         'wizard/crm_partner_to_opportunity_view.xml',
 
-        'wizard/crm_send_email_view.xml',
+#        'wizard/crm_send_email_view.xml',
         'wizard/crm_add_note_view.xml',
         'wizard/crm_merge_opportunities_view.xml',
 

=== modified file 'crm/crm.py'
--- crm/crm.py	2011-03-04 12:53:34 +0000
+++ crm/crm.py	2011-04-11 12:37:32 +0000
@@ -308,8 +308,8 @@
             return {'value': {'phone': address.phone}}
 
     def _history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, email_from=False, message_id=False, attach=[], context=None):
-        mailgate_pool = self.pool.get('mailgate.thread')
-        return mailgate_pool.history(cr, uid, cases, keyword, history=history,\
+        thread_pool = self.pool.get('email.thread')
+        return thread_pool.history(cr, uid, cases, keyword, history=history,\
                                        subject=subject, email=email, \
                                        details=details, email_from=email_from,\
                                        message_id=message_id, attach=attach, \
@@ -452,12 +452,13 @@
         @param ids: List of case's IDs to remind
         @param context: A standard dictionary for contextual values
         """
+        email_message_obj = self.pool.get('email.message')
         for case in self.browse(cr, uid, ids, context=context):
             if not destination and not case.email_from:
                 raise osv.except_osv(_('Error!'), ("Partner Email is not specified in Case"))
             if not case.user_id.user_email:
                raise osv.except_osv(_('Error!'), ("User Email is not specified in Case"))
-            
+
             if destination and case.section_id.user_id:
                 case_email = case.section_id.user_id.user_email
             else:
@@ -485,19 +486,19 @@
                 attach_to_send = self.pool.get('ir.attachment').read(cr, uid, attach_ids, ['datas_fname', 'datas'])
                 attach_to_send = map(lambda x: (x['datas_fname'], base64.decodestring(x['datas'])), attach_to_send)
 
-                # Send an email
-            subject = "Reminder: [%s] %s" % (str(case.id), case.name,)
-            tools.email_send(
+            # Send an email
+            subject = "Reminder: [%s] %s" % (str(case.id), case.name, )
+            email_message_obj.schedule_with_attach(cr, uid,
                 src,
                 [dest],
                 subject,
                 body,
-                reply_to=case.section_id.reply_to or '',
+                model='crm.case',
+                reply_to=case.section_id.reply_to,
                 openobject_id=str(case.id),
                 attach=attach_to_send
             )
             self._history(cr, uid, [case], _('Send'), history=True, subject=subject, email=dest, details=body, email_from=src)
-
         return True
 
     def _check(self, cr, uid, ids=False, context=None):
@@ -537,7 +538,7 @@
     def format_mail(self, obj, body):
         return self.pool.get('base.action.rule').format_mail(obj, body)
 
-    def message_followers(self, cr, uid, ids, context=None):
+    def thread_followers(self, cr, uid, ids, context=None):
         """ Get a list of emails of the people following this thread
         """
         res = {}

=== modified file 'crm/crm_action_rule.py'
--- crm/crm_action_rule.py	2011-01-14 00:11:01 +0000
+++ crm/crm_action_rule.py	2011-04-11 12:37:32 +0000
@@ -37,25 +37,26 @@
     """ Base Action Rule """
     _inherit = 'base.action.rule'
     _description = 'Action Rules'
-    
+
     _columns = {
-        'trg_section_id': fields.many2one('crm.case.section', 'Sales Team'), 
-        'trg_max_history': fields.integer('Maximum Communication History'), 
-        'trg_categ_id':  fields.many2one('crm.case.categ', 'Category'), 
-        'regex_history' : fields.char('Regular Expression on Case History', size=128), 
-        'act_section_id': fields.many2one('crm.case.section', 'Set Team to'), 
-        'act_categ_id': fields.many2one('crm.case.categ', 'Set Category to'), 
+        'trg_section_id': fields.many2one('crm.case.section', 'Sales Team'),
+        'trg_max_history': fields.integer('Maximum Communication History'),
+        'trg_categ_id':  fields.many2one('crm.case.categ', 'Category'),
+        'regex_history' : fields.char('Regular Expression on Case History', size=128),
+        'act_section_id': fields.many2one('crm.case.section', 'Set Team to'),
+        'act_categ_id': fields.many2one('crm.case.categ', 'Set Category to'),
         'act_mail_to_partner': fields.boolean('Mail to Partner', help="Check \
-this if you want the rule to send an email to the partner."), 
+this if you want the rule to send an email to the partner."),
     }
-    
+
 
     def email_send(self, cr, uid, obj, emails, body, emailfrom=tools.config.get('email_from', False), context=None):
+        email_message_obj = self.pool.get('email.message')
         body = self.format_mail(obj, body)
         if not emailfrom:
             if hasattr(obj, 'user_id')  and obj.user_id and obj.user_id.address_id and obj.user_id.address_id.email:
                 emailfrom = obj.user_id.address_id.email
-            
+
         name = '[%d] %s' % (obj.id, tools.ustr(obj.name))
         emailfrom = tools.ustr(emailfrom)
         if hasattr(obj, 'section_id') and obj.section_id and obj.section_id.reply_to:
@@ -63,10 +64,10 @@
         else:
             reply_to = emailfrom
         if not emailfrom:
-            raise osv.except_osv(_('Error!'), 
+            raise osv.except_osv(_('Error!'),
                     _("No E-Mail ID Found for your Company address!"))
-        return tools.email_send(emailfrom, emails, name, body, reply_to=reply_to, openobject_id=str(obj.id))
-    
+        return email_message_obj.schedule_with_attach(cr, uid, emailfrom, emails, name, body, model='base.action.rule', reply_to=reply_to, openobject_id=str(obj.id))
+
     def do_check(self, cr, uid, action, obj, context=None):
         """ @param self: The object pointer
         @param cr: the current row, from the database cursor,
@@ -79,7 +80,7 @@
         if hasattr(obj, 'categ_id'):
             ok = ok and (not action.trg_categ_id or action.trg_categ_id.id==obj.categ_id.id)
 
-        #Cheking for history 
+        #Cheking for history
         regex = action.regex_history
         result_history = True
         if regex:
@@ -109,7 +110,7 @@
         @param context: A standard dictionary for contextual values """
         res = super(base_action_rule, self).do_action(cr, uid, action, model_obj, obj, context=context)
         write = {}
-        
+
         if hasattr(action, 'act_section_id') and action.act_section_id:
             obj.section_id = action.act_section_id
             write['section_id'] = action.act_section_id.id

=== modified file 'crm/crm_lead.py'
--- crm/crm_lead.py	2011-03-09 06:21:48 +0000
+++ crm/crm_lead.py	2011-04-11 12:37:32 +0000
@@ -40,7 +40,7 @@
     _name = "crm.lead"
     _description = "Lead/Opportunity"
     _order = "date_action, priority, id desc"
-    _inherit = ['mailgate.thread','res.partner.address']
+    _inherit = ['email.thread','res.partner.address']
     def _compute_day(self, cr, uid, ids, fields, args, context=None):
         """
         @param cr: the current row, from the database cursor,
@@ -148,7 +148,7 @@
                                   \nIf the case is in progress the state is set to \'Open\'.\
                                   \nWhen the case is over, the state is set to \'Done\'.\
                                   \nIf the case needs to be reviewed then the state is set to \'Pending\'.'),
-        'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
+        'message_ids': fields.one2many('email.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
     }
 
 
@@ -325,8 +325,9 @@
         @param self: The object pointer
         @param cr: the current row, from the database cursor,
         @param uid: the current user’s ID for security checks
+        @param msg: dictionary object to contain email message data
         """
-        mailgate_pool = self.pool.get('email.server.tools')
+        thread_pool = self.pool.get('email.thread')
 
         subject = msg.get('subject')
         body = msg.get('body')
@@ -343,26 +344,13 @@
         if msg.get('priority', False):
             vals['priority'] = priority
 
-        res = mailgate_pool.get_partner(cr, uid, msg.get('from') or msg.get_unixfrom())
+        res = thread_pool.get_partner(cr, uid, msg.get('from', False))
         if res:
             vals.update(res)
 
-        res = self.create(cr, uid, vals, context)
-        attachents = msg.get('attachments', [])
-        for attactment in attachents or []:
-            data_attach = {
-                'name': attactment,
-                'datas':binascii.b2a_base64(str(attachents.get(attactment))),
-                'datas_fname': attactment,
-                'description': 'Mail attachment',
-                'res_model': self._name,
-                'res_id': res,
-            }
-            self.pool.get('ir.attachment').create(cr, uid, data_attach)
-
-        return res
-
-    def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context=None):
+        return self.create(cr, uid, vals, context)
+
+    def message_update(self, cr, uid, ids, msg, vals={}, default_act='pending', context=None):
         """
         @param self: The object pointer
         @param cr: the current row, from the database cursor,
@@ -399,18 +387,6 @@
             res = self.write(cr, uid, [case.id], values, context=context)
         return res
 
-    def msg_send(self, cr, uid, id, *args, **argv):
-
-        """ Send The Message
-            @param self: The object pointer
-            @param cr: the current row, from the database cursor,
-            @param uid: the current user’s ID for security checks,
-            @param ids: List of email’s IDs
-            @param *args: Return Tuple Value
-            @param **args: Return Dictionary of Keyword Value
-        """
-        return True
-
     def on_change_optin(self, cr, uid, ids, optin):
         return {'value':{'optin':optin,'optout':False}}
 

=== modified file 'crm/crm_lead_view.xml'
--- crm/crm_lead_view.xml	2011-03-04 12:53:34 +0000
+++ crm/crm_lead_view.xml	2011-04-11 12:37:32 +0000
@@ -105,10 +105,11 @@
                     </group>
                     <group colspan="2" col="3">
                         <separator string="Communication" colspan="4" col="3"/>
-                        <field name="email_from" widget="email"/><button string="Send Email"
-                        name="%(crm.action_crm_send_mail)d"
-                        context="{'mail':'new', 'model': 'crm.lead'}"
-                        icon="terp-mail-message-new" type="action" colspan="1"/>
+                        <field name="email_from" widget="email"/>
+                        <button string="Send Email"
+                            name="%(mail.action_email_compose_message_wizard)d"
+                            context="{'email_model':'crm.lead', 'email_res_id': active_id}"
+                            icon="terp-mail-message-new" type="action" colspan="1"/>
                         <newline/>
                         <field name="phone"/>
                         <newline/>
@@ -188,8 +189,8 @@
                             <field name="history" invisible="1"/>
                             <button
                                 string="Reply" attrs="{'invisible': [('history', '!=', True)]}"
-                                name="%(crm.action_crm_send_mail)d"
-                                context="{'mail':'reply', 'model': 'crm.lead', 'include_original' : True}"
+                                name="%(mail.action_email_compose_message_wizard)d"
+                                context="{'mail':'reply', 'message_id':active_id}"
                                 icon="terp-mail-replied" type="action" />
                         </tree>
                         <form string="Communication history">
@@ -198,18 +199,17 @@
                                 <field name="date"/>
                                 <field name="email_to" widget="char" size="512"/>
                                 <field name="email_cc" widget="char" size="512"/>
-                                <field name="name" colspan="4" widget="char" size="512"/>
+                                <field name="subject" colspan="4" widget="char" size="512"/>
                                 <field name="history" invisible="1"/>
                             </group>
                             <notebook colspan="4">
                                 <page string="Details">
                                     <group attrs="{'invisible': [('history', '!=', True)]}">
-                                        <field name="description" colspan="4" nolabel="1" height="250"/>
-                                        <button colspan="4"
-                                            string="Reply"
-                                            name="%(crm.action_crm_send_mail)d"
-                                            context="{'mail':'reply', 'model': 'crm.lead', 'include_original' : True}"
-                                            icon="terp-mail-replied" type="action" />
+                                        <field name="body" colspan="4" nolabel="1" height="250"/>
+                                        <button colspan="4" string="Reply"
+                                            name="%(mail.action_email_compose_message_wizard)d"
+                                            context="{'mail':'reply', 'message_id':active_id}"
+                                            icon="terp-mail-replied" type="action"/>
                                     </group>
                                     <group attrs="{'invisible': [('history', '=', True)]}">
                                         <field name="display_text" colspan="4" nolabel="1"  height="250"/>
@@ -226,9 +226,9 @@
                         context="{'model': 'crm.lead' }"
                         icon="terp-document-new" type="action" />
                     <button string="Send New Email"
-                        name="%(crm.action_crm_send_mail)d"
-                        context="{'mail':'new', 'model': 'crm.lead'}"
-                        icon="terp-mail-message-new" type="action" />
+                        name="%(mail.action_email_compose_message_wizard)d"
+                        context="{'email_model':'crm.lead', 'email_res_id': active_id}"
+                        icon="terp-mail-message-new" type="action"/>
                  </page>
                 </notebook>
             </form>

=== modified file 'crm/crm_meeting.py'
--- crm/crm_meeting.py	2011-01-18 22:45:39 +0000
+++ crm/crm_meeting.py	2011-04-11 12:37:32 +0000
@@ -42,15 +42,15 @@
     _name = 'crm.meeting'
     _description = "Meeting"
     _order = "id desc"
-    _inherit = ['mailgate.thread',"calendar.event"]
+    _inherit = ['email.thread',"calendar.event"]
     _columns = {
         # From crm.case
-        'name': fields.char('Summary', size=124, required=True, states={'done': [('readonly', True)]}), 
-        'partner_id': fields.many2one('res.partner', 'Partner', states={'done': [('readonly', True)]}), 
+        'name': fields.char('Summary', size=124, required=True, states={'done': [('readonly', True)]}),
+        'partner_id': fields.many2one('res.partner', 'Partner', states={'done': [('readonly', True)]}),
         'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
-                                 domain="[('partner_id','=',partner_id)]", states={'done': [('readonly', True)]}), 
+                                 domain="[('partner_id','=',partner_id)]", states={'done': [('readonly', True)]}),
         'section_id': fields.many2one('crm.case.section', 'Sales Team', states={'done': [('readonly', True)]}, \
-                        select=True, help='Sales team to which Case belongs to.'), 
+                        select=True, help='Sales team to which Case belongs to.'),
         'email_from': fields.char('Email', size=128, states={'done': [('readonly', True)]}, help="These people will receive email."),
         'id': fields.integer('ID'),
         'create_date': fields.datetime('Creation Date' , readonly=True),
@@ -67,7 +67,7 @@
                                  'event_id', 'attendee_id', 'Attendees', states={'done': [('readonly', True)]}),
         'date_closed': fields.datetime('Closed', readonly=True),
         'date_deadline': fields.datetime('Deadline', states={'done': [('readonly', True)]}),
-        'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
+        'message_ids': fields.one2many('email.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
         'state': fields.selection([('open', 'Confirmed'),
                                     ('draft', 'Unconfirmed'),
                                     ('cancel', 'Cancelled'),
@@ -75,7 +75,7 @@
                                     size=16, readonly=True),
     }
     _defaults = {
-        'state': 'draft', 
+        'state': 'draft',
         'active': 1,
         'user_id': lambda self, cr, uid, ctx: uid,
     }
@@ -147,7 +147,7 @@
         except:
             # Tolerate a missing shortcut. See product/product.py for similar code.
             logging.getLogger('orm').debug('Skipped meetings shortcut for user "%s"', data.get('name','<new'))
-            
+
         return user_id
 
 res_users()

=== modified file 'crm/crm_opportunity_view.xml'
--- crm/crm_opportunity_view.xml	2011-03-04 12:53:34 +0000
+++ crm/crm_opportunity_view.xml	2011-04-11 12:37:32 +0000
@@ -60,11 +60,11 @@
                                 <group col="3" colspan="2">
                                     <field name="email_from" string="Email" />
                                     <button string="Send Email"
-                                    name="%(crm.action_crm_send_mail)d"
-                                    context="{'mail':'new', 'model': 'crm.lead'}"
-                                    icon="terp-mail-message-new" type="action" />
+                                        name="%(mail.action_email_compose_message_wizard)d"
+                                        context="{'email_model': 'crm.lead', 'email_res_id': active_id}"
+                                        icon="terp-mail-message-new" type="action"/>
                                 </group>
-                                    <field name="phone"/>
+                                <field name="phone"/>
 
                             </group>
                             <group col="2" colspan="2">
@@ -144,11 +144,10 @@
                                 <tree string="History">
                                     <field name="display_text" string="History Information"/>
                                     <field name="history" invisible="1"/>
-                                    <button
-                                        string="Reply" attrs="{'invisible': [('history', '!=', True)]}"
-                                        name="%(crm.action_crm_send_mail)d"
-                                        context="{'mail':'reply', 'model': 'crm.lead', 'include_original' : True}"
-                                        icon="terp-mail-replied" type="action" />
+                                    <button string="Reply"
+                                        name="%(mail.action_email_compose_message_wizard)d"
+                                        context="{'mail':'reply', 'message_id':active_id}"
+                                        icon="terp-mail-replied" type="action"/>
                                 </tree>
                                 <form string="History">
                                     <group col="4" colspan="4">
@@ -156,19 +155,18 @@
                                         <field name="date"/>
                                         <field name="email_to" size="512"/>
                                         <field name="email_cc" size="512"/>
-                                        <field name="name" colspan="4" attrs="{'invisible': [('history', '=', True)]}"/>
+                                        <field name="subject" colspan="4" attrs="{'invisible': [('history', '=', True)]}"/>
                                         <field name="display_text" colspan="4"   attrs="{'invisible': [('history', '=', False)]}"/>
                                         <field name="history" invisible="1"/>
                                     </group>
                                     <notebook colspan="4">
                                         <page string="Details">
-
-                                                <field name="description" colspan="4" nolabel="1"/>
+                                             <field name="body" colspan="4" nolabel="1"/>
                                              <group attrs="{'invisible': [('history', '!=', True)]}">
                                                 <button colspan="4"
                                                     string="Reply"
-                                                    name="%(crm.action_crm_send_mail)d"
-                                                    context="{'mail':'reply', 'model': 'crm.lead', 'include_original' : True}"
+                                                    name="%(mail.action_email_compose_message_wizard)d"
+                                                    context="{'mail':'reply', 'message_id':active_id}"
                                                     icon="terp-mail-replied" type="action" />
                                             </group>
 
@@ -183,10 +181,10 @@
                                 name="%(crm.action_crm_add_note)d"
                                 context="{'model': 'crm.lead' }"
                                 icon="terp-document-new" type="action" />
-                            <button string="Send New Email"
-                                name="%(crm.action_crm_send_mail)d"
-                                context="{'mail':'new', 'model': 'crm.lead'}"
-                                icon="terp-mail-message-new" type="action" />
+		                    <button string="Send New Email"
+		                        name="%(mail.action_email_compose_message_wizard)d"
+		                        context="{'email_model': 'crm.lead', 'email_res_id': active_id}"
+		                        icon="terp-mail-message-new" type="action"/>
                         </page>
                         <page string="Extra Info" groups="base.group_extended">
                             <group col="2" colspan="2">

=== modified file 'crm/crm_phonecall.py'
--- crm/crm_phonecall.py	2011-01-31 13:29:31 +0000
+++ crm/crm_phonecall.py	2011-04-11 12:37:32 +0000
@@ -32,65 +32,65 @@
     _name = "crm.phonecall"
     _description = "Phonecall"
     _order = "id desc"
-    _inherit = ['mailgate.thread']
+    _inherit = ['email.thread']
     _columns = {
         # From crm.case
         'id': fields.integer('ID'),
         'name': fields.char('Call Summary', size=64),
-        'active': fields.boolean('Active', required=False), 
+        'active': fields.boolean('Active', required=False),
         'date_action_last': fields.datetime('Last Action', readonly=1),
-        'date_action_next': fields.datetime('Next Action', readonly=1), 
+        'date_action_next': fields.datetime('Next Action', readonly=1),
         'create_date': fields.datetime('Creation Date' , readonly=True),
         'section_id': fields.many2one('crm.case.section', 'Sales Team', \
-                        select=True, help='Sales team to which Case belongs to.'), 
-        'user_id': fields.many2one('res.users', 'Responsible'), 
-        'partner_id': fields.many2one('res.partner', 'Partner'), 
+                        select=True, help='Sales team to which Case belongs to.'),
+        'user_id': fields.many2one('res.users', 'Responsible'),
+        'partner_id': fields.many2one('res.partner', 'Partner'),
         'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
-                                 domain="[('partner_id','=',partner_id)]"), 
-        'company_id': fields.many2one('res.company', 'Company'), 
-        'description': fields.text('Description'), 
+                                 domain="[('partner_id','=',partner_id)]"),
+        'company_id': fields.many2one('res.company', 'Company'),
+        'description': fields.text('Description'),
         'state': fields.selection([
-                                    ('draft', 'Draft'), 
-                                    ('open', 'Todo'), 
-                                    ('cancel', 'Cancelled'), 
-                                    ('done', 'Done'), 
+                                    ('draft', 'Draft'),
+                                    ('open', 'Todo'),
+                                    ('cancel', 'Cancelled'),
+                                    ('done', 'Done'),
                                     ('pending', 'Pending'),
-                                ], 'State', size=16, readonly=True, 
+                                ], 'State', size=16, readonly=True,
                                   help='The state is set to \'Draft\', when a case is created.\
                                   \nIf the case is in progress the state is set to \'Open\'.\
                                   \nWhen the case is over, the state is set to \'Done\'.\
-                                  \nIf the case needs to be reviewed then the state is set to \'Pending\'.'), 
-        'email_from': fields.char('Email', size=128, help="These people will receive email."), 
+                                  \nIf the case needs to be reviewed then the state is set to \'Pending\'.'),
+        'email_from': fields.char('Email', size=128, help="These people will receive email."),
         'date_open': fields.datetime('Opened', readonly=True),
         # phonecall fields
-        'duration': fields.float('Duration', help="Duration in Minutes"), 
+        'duration': fields.float('Duration', help="Duration in Minutes"),
         'categ_id': fields.many2one('crm.case.categ', 'Category', \
                         domain="['|',('section_id','=',section_id),('section_id','=',False),\
-                        ('object_id.model', '=', 'crm.phonecall')]"), 
-        'partner_phone': fields.char('Phone', size=32), 
+                        ('object_id.model', '=', 'crm.phonecall')]"),
+        'partner_phone': fields.char('Phone', size=32),
         'partner_contact': fields.related('partner_address_id', 'name', \
-                                 type="char", string="Contact", size=128), 
-        'partner_mobile': fields.char('Mobile', size=32), 
-        'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'), 
+                                 type="char", string="Contact", size=128),
+        'partner_mobile': fields.char('Mobile', size=32),
+        'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
         'canal_id': fields.many2one('res.partner.canal', 'Channel', \
                         help="The channels represent the different communication\
                          modes available with the customer." \
                         " With each commercial opportunity, you can indicate\
-                         the canall which is this opportunity source."), 
-        'date_closed': fields.datetime('Closed', readonly=True), 
-        'date': fields.datetime('Date'), 
-        'opportunity_id': fields.many2one ('crm.lead', 'Opportunity'), 
-        'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
+                         the canall which is this opportunity source."),
+        'date_closed': fields.datetime('Closed', readonly=True),
+        'date': fields.datetime('Date'),
+        'opportunity_id': fields.many2one ('crm.lead', 'Opportunity'),
+        'message_ids': fields.one2many('email.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
     }
 
     _defaults = {
-        'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'), 
-        'priority': crm.AVAILABLE_PRIORITIES[2][0], 
-        'state':  'open', 
+        'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
+        'priority': crm.AVAILABLE_PRIORITIES[2][0],
+        'state':  'open',
         'user_id': lambda self,cr,uid,ctx: uid,
-        'active': 1, 
+        'active': 1,
     }
-    
+
     # From crm.case
 
     def onchange_partner_address_id(self, cr, uid, ids, add, email=False):
@@ -174,23 +174,23 @@
                 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
 
             context = {
-                        'default_phonecall_id': phonecall.id, 
-                        'default_partner_id': phonecall.partner_id and phonecall.partner_id.id or False, 
-                        'default_email': phonecall.email_from , 
+                        'default_phonecall_id': phonecall.id,
+                        'default_partner_id': phonecall.partner_id and phonecall.partner_id.id or False,
+                        'default_email': phonecall.email_from ,
                         'default_name': phonecall.name
                     }
 
             value = {
-                'name': _('Meetings'), 
-                'domain' : "[('user_id','=',%s)]" % (uid), 
-                'context': context, 
-                'view_type': 'form', 
-                'view_mode': 'calendar,form,tree', 
-                'res_model': 'crm.meeting', 
-                'view_id': False, 
-                'views': [(id1, 'calendar'), (id2, 'form'), (id3, 'tree')], 
-                'type': 'ir.actions.act_window', 
-                'search_view_id': res['res_id'], 
+                'name': _('Meetings'),
+                'domain' : "[('user_id','=',%s)]" % (uid),
+                'context': context,
+                'view_type': 'form',
+                'view_mode': 'calendar,form,tree',
+                'res_model': 'crm.meeting',
+                'view_id': False,
+                'views': [(id1, 'calendar'), (id2, 'form'), (id3, 'tree')],
+                'type': 'ir.actions.act_window',
+                'search_view_id': res['res_id'],
                 'nodestroy': True
                 }
 

=== modified file 'crm/report/crm_lead_report.py'
--- crm/report/crm_lead_report.py	2011-02-14 16:32:31 +0000
+++ crm/report/crm_lead_report.py	2011-04-11 12:37:32 +0000
@@ -109,7 +109,7 @@
                     c.planned_revenue,
                     c.planned_revenue*(c.probability/100) as probable_revenue,
                     1 as nbr,
-                    (SELECT count(id) FROM mailgate_message WHERE model='crm.lead' AND res_id=c.id AND history=True) AS email,
+                    (SELECT count(id) FROM email_message WHERE model='crm.lead' AND res_id=c.id AND history=True) AS email,
                     date_trunc('day',c.create_date) as create_date,
                     extract('epoch' from (c.date_closed-c.create_date))/(3600*24) as  delay_close,
                     abs(extract('epoch' from (c.date_deadline - c.date_closed))/(3600*24)) as  delay_expected,

=== modified file 'crm/security/ir.model.access.csv'
--- crm/security/ir.model.access.csv	2011-01-14 00:11:01 +0000
+++ crm/security/ir.model.access.csv	2011-04-11 12:37:32 +0000
@@ -27,15 +27,15 @@
 "access_res_partner_manager","res.partner.crm.manager","base.model_res_partner","base.group_sale_manager",1,0,0,0
 "access_res_partner_address_manager","res.partner.address.crm.user.manager","base.model_res_partner_address","base.group_sale_manager",1,0,0,0
 "access_res_partner_category_manager","res.partner.category.crm.manager","base.model_res_partner_category","base.group_sale_manager",1,0,0,0
-"mail_gateway_mailgate_message_manager","mail_gateway.mailgate.message.manager","mail_gateway.model_mailgate_message","base.group_sale_manager",1,0,0,0
-"mail_gateway_mailgate_thread_manager","mail_gateway.mailgate.thread.manager","mail_gateway.model_mailgate_thread","base.group_sale_manager",1,1,1,1
+"email_email_message_manager","mail.email.message.manager","mail.model_email_message","base.group_sale_manager",1,0,0,0
+"email_thread_manager","mail.thread.manager","mail.model_email_thread","base.group_sale_manager",1,1,1,1
 "access_calendar_attendee_crm_user","calendar.attendee.crm.user","model_calendar_attendee","base.group_sale_salesman",1,1,1,0
 "access_calendar_attendee_crm_manager","calendar.attendee.crm.manager","model_calendar_attendee","base.group_sale_manager",1,1,1,1
 "access_res_partner","res.partner.crm.user","base.model_res_partner","base.group_sale_salesman",1,1,1,0
 "access_res_partner_address","res.partner.address.crm.user","base.model_res_partner_address","base.group_sale_salesman",1,1,1,0
 "access_res_partner_category","res.partner.category.crm.user","base.model_res_partner_category","base.group_sale_salesman",1,1,1,0
-"mail_gateway_mailgate_thread","mail_gateway.mailgate.thread","mail_gateway.model_mailgate_thread","base.group_sale_salesman",1,1,1,1
-"mail_gateway_mailgate_message_user","mail_gateway.mailgate.message.user","mail_gateway.model_mailgate_message","base.group_sale_salesman",1,1,1,1
+"email_mailgate_thread","email.thread","mail.model_email_thread","base.group_sale_salesman",1,1,1,1
+"email_gateway_email_message_user","mail.email.message.user","mail.model_email_message","base.group_sale_salesman",1,1,1,1
 "access_crm_case_categ_manager","crm.case.categ manager","model_crm_case_categ","base.group_sale_manager",1,1,1,1
 "access_base_action_rule_manager","base.action.rule manager","model_base_action_rule","base.group_sale_manager",1,1,1,1
 "access_crm_lead_report_user","crm.lead.report user","model_crm_lead_report","base.group_sale_salesman",1,1,1,1

=== modified file 'crm/wizard/__init__.py'
--- crm/wizard/__init__.py	2011-01-14 00:11:01 +0000
+++ crm/wizard/__init__.py	2011-04-11 12:37:32 +0000
@@ -19,7 +19,8 @@
 #
 ##############################################################################
 
-import crm_send_email
+#import crm_send_email
+import email_compose_message
 import crm_add_note
 
 import crm_lead_to_partner

=== modified file 'crm/wizard/crm_add_note.py'
--- crm/wizard/crm_add_note.py	2011-02-08 13:36:40 +0000
+++ crm/wizard/crm_add_note.py	2011-04-11 12:37:32 +0000
@@ -5,6 +5,16 @@
 
 AVAILABLE_STATES = crm.AVAILABLE_STATES + [('unchanged', 'Unchanged')]
 
+class crm_add_note_email_attachment(osv.osv_memory):
+    _name = 'crm.add.note.email.attachment'
+
+    _columns = {
+        'binary' : fields.binary('Attachment', required=True),
+        'name' : fields.char('Name', size=128, required=True),
+        'wizard_id' : fields.many2one('crm.add.note', 'Wizard', required=True),
+    }
+
+crm_add_note_email_attachment()
 
 class crm_add_note(osv.osv_memory):
     """Adds a new note to the case."""
@@ -14,8 +24,8 @@
     _columns = {
         'body': fields.text('Note Body', required=True),
         'state': fields.selection(AVAILABLE_STATES, string='Set New State To',
-                                  required=True), 
-        'attachment_ids' : fields.one2many('crm.send.mail.attachment', 'wizard_id'),
+                                  required=True),
+        'attachment_ids' : fields.one2many('crm.add.note.email.attachment', 'wizard_id'),
     }
 
     def action_add(self, cr, uid, ids, context=None):
@@ -37,7 +47,7 @@
             attach = [
                 (x.name, base64.decodestring(x.binary)) for x in obj.attachment_ids
             ]
-            case_pool.history(cr, uid, [case], self.pool.get('mailgate.message').truncate_data(cr, uid, obj.body, context=context), history=False,
+            case_pool.history(cr, uid, [case], self.pool.get('email.message').truncate_data(cr, uid, obj.body, context=context), history=False,
                               details=obj.body, email_from=user_name, attach=attach)
 
             if obj.state == 'unchanged':

=== modified file 'crm/wizard/crm_lead_to_opportunity.py'
--- crm/wizard/crm_lead_to_opportunity.py	2011-02-22 16:24:03 +0000
+++ crm/wizard/crm_lead_to_opportunity.py	2011-04-11 12:37:32 +0000
@@ -117,7 +117,7 @@
         leads.history(cr, uid, [lead], _('Converted to opportunity'), details='Converted to Opportunity', context=context)
         if lead.partner_id:
             msg_ids = [ x.id for x in lead.message_ids]
-            self.pool.get('mailgate.message').write(cr, uid, msg_ids, {
+            self.pool.get('email.message').write(cr, uid, msg_ids, {
                         'partner_id': lead.partner_id.id
                     }, context=context)
             leads.log(cr, uid, lead.id, _("Lead '%s' has been converted to an opportunity.") % lead.name)

=== modified file 'crm/wizard/crm_merge_opportunities.py'
--- crm/wizard/crm_merge_opportunities.py	2011-02-24 15:03:09 +0000
+++ crm/wizard/crm_merge_opportunities.py	2011-04-11 12:37:32 +0000
@@ -60,7 +60,7 @@
             @param opp_ids : list of opportunities ids to merge
         """
         opp_obj = self.pool.get('crm.lead')
-        message_obj = self.pool.get('mailgate.message')
+        message_obj = self.pool.get('email.message')
 
         lead_ids = context and context.pop('lead_ids', []) or []
 
@@ -194,7 +194,6 @@
         context['lead_ids'] = [op_ids[0].id]
         return self.merge(cr, uid, op_ids, context)
 
-
     _columns = {
         'opportunity_ids' : fields.many2many('crm.lead',  'merge_opportunity_rel', 'merge_id', 'opportunity_id', 'Opportunities', domain=[('type', '=', 'opportunity')]),
     }

=== modified file 'crm/wizard/crm_send_email.py'
--- crm/wizard/crm_send_email.py	2011-02-28 11:08:04 +0000
+++ crm/wizard/crm_send_email.py	2011-04-11 12:37:32 +0000
@@ -31,18 +31,6 @@
 
 AVAILABLE_STATES = crm.AVAILABLE_STATES + [('unchanged', 'Unchanged')]
 
-
-class crm_send_new_email_attachment(osv.osv_memory):
-    _name = 'crm.send.mail.attachment'
-
-    _columns = {
-        'binary' : fields.binary('Attachment', required=True),
-        'name' : fields.char('Name', size=128, required=True),
-        'wizard_id' : fields.many2one('crm.send.mail', 'Wizard', required=True),
-    }
-
-crm_send_new_email_attachment()
-
 class crm_send_new_email(osv.osv_memory):
     """ Sends new email for the case"""
     _name = "crm.send.mail"
@@ -57,14 +45,13 @@
         'body': fields.text('Message Body', required=True),
         'state': fields.selection(AVAILABLE_STATES, string='Set New State To', required=True),
         'attachment_ids' : fields.one2many('crm.send.mail.attachment', 'wizard_id', 'Attachment'),
-        'html': fields.boolean('HTML formatting?', help="Select this if you want to send email with HTML formatting."), 
+        'html': fields.boolean('HTML formatting?', help="Select this if you want to send email with HTML formatting."),
     }
 
     def action_send(self, cr, uid, ids, context=None):
         """ This sends an email to ALL the addresses of the selected partners.
         """
-        hist_obj = self.pool.get('mailgate.message')
-
+        email_message_obj = self.pool.get('email.message')
         if context is None:
             context = {}
 
@@ -103,7 +90,7 @@
                 attach += attach_all
 
             else:
-                hist = hist_obj.browse(cr, uid, res_id, context=context)
+                hist = email_message_obj.browse(cr, uid, res_id, context=context)
                 message_id = hist.message_id
                 model = hist.model
                 case_pool = self.pool.get(model)
@@ -124,11 +111,12 @@
             if obj.html:
                 subtype = 'html'
 
-            flag = tools.email_send(
+            flag = email_message_obj.schedule_with_attach(cr, uid,
                 email_from,
                 emails,
                 obj.subject,
                 body,
+                model='crm.send.mail',
                 email_cc=email_cc,
                 attach=attach,
                 subtype=subtype,
@@ -204,7 +192,7 @@
         """
         This function gets default values for reply mail
         """
-        hist_obj = self.pool.get('mailgate.message')
+        hist_obj = self.pool.get('email.message')
         res_ids = context and context.get('active_ids', []) or []
 
         user_obj = self.pool.get('res.users')

=== added file 'crm/wizard/email_compose_message.py'
--- crm/wizard/email_compose_message.py	1970-01-01 00:00:00 +0000
+++ crm/wizard/email_compose_message.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,55 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+from osv import osv
+from osv import fields
+import tools
+
+email_model = [
+        'crm.lead',
+    ]
+
+class email_compose_message(osv.osv_memory):
+    _inherit = 'email.compose.message'
+
+    def get_value(self, cr, uid, model, resource_id, context=None):
+        if context is None:
+            context = {}
+        result = super(email_compose_message, self).get_value(cr, uid,  model, resource_id, context=context)
+        if model in email_model and resource_id:
+            model_obj = self.pool.get(model)
+            data = model_obj.browse(cr, uid , resource_id, context)
+            result.update({
+                    'subject' : data.name or False,
+                    'email_to' : data.email_from or False,
+                    'email_from' : data.user_id and data.user_id.address_id and data.user_id.address_id.email or False,
+                    'body' : '\n' + (tools.ustr(data.user_id.signature or '')),
+                    'email_cc' : tools.ustr(data.email_cc or ''),
+                    'model': model  or False,
+                    'res_id': resource_id  or False,
+                })
+            if hasattr(data, 'section_id'):
+                result.update({'reply_to' : data.section_id and data.section_id.reply_to or False})
+        return result
+
+email_compose_message()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'crm_claim/crm_claim.py'
--- crm_claim/crm_claim.py	2011-03-16 12:38:21 +0000
+++ crm_claim/crm_claim.py	2011-04-11 12:37:32 +0000
@@ -22,10 +22,12 @@
 from osv import fields, osv
 from crm import crm
 import time
+from crm import wizard
 import binascii
 import tools
 from tools.translate import _
 
+wizard.email_compose_message.email_model.append('crm.claim')
 CRM_CLAIM_PENDING_STATES = (
     crm.AVAILABLE_STATES[2][0], # Cancelled
     crm.AVAILABLE_STATES[3][0], # Done
@@ -39,49 +41,49 @@
     _name = "crm.claim"
     _description = "Claim"
     _order = "priority,date desc"
-    _inherit = ['mailgate.thread']
+    _inherit = ['email.thread']
     _columns = {
-        'id': fields.integer('ID', readonly=True), 
-        'name': fields.char('Claim Subject', size=128, required=True), 
+        'id': fields.integer('ID', readonly=True),
+        'name': fields.char('Claim Subject', size=128, required=True),
         'action_next': fields.char('Next Action', size=200),
         'date_action_next': fields.datetime('Next Action Date'),
-        'description': fields.text('Description'), 
-        'resolution': fields.text('Resolution'), 
-        'create_date': fields.datetime('Creation Date' , readonly=True), 
-        'write_date': fields.datetime('Update Date' , readonly=True), 
-        'date_deadline': fields.date('Deadline'), 
-        'date_closed': fields.datetime('Closed', readonly=True), 
-        'date': fields.datetime('Claim Date'), 
-        'ref' : fields.reference('Reference', selection=crm._links_get, size=128), 
+        'description': fields.text('Description'),
+        'resolution': fields.text('Resolution'),
+        'create_date': fields.datetime('Creation Date' , readonly=True),
+        'write_date': fields.datetime('Update Date' , readonly=True),
+        'date_deadline': fields.date('Deadline'),
+        'date_closed': fields.datetime('Closed', readonly=True),
+        'date': fields.datetime('Claim Date'),
+        'ref' : fields.reference('Reference', selection=crm._links_get, size=128),
         'categ_id': fields.many2one('crm.case.categ', 'Category', \
                             domain="[('section_id','=',section_id),\
-                            ('object_id.model', '=', 'crm.claim')]"), 
-        'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'), 
+                            ('object_id.model', '=', 'crm.claim')]"),
+        'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
         'type_action': fields.selection([('correction','Corrective Action'),('prevention','Preventive Action')], 'Action Type'),
-        'user_id': fields.many2one('res.users', 'Responsible'), 
-        'user_fault': fields.char('Trouble Responsible', size=64), 
+        'user_id': fields.many2one('res.users', 'Responsible'),
+        'user_fault': fields.char('Trouble Responsible', size=64),
         'section_id': fields.many2one('crm.case.section', 'Sales Team', \
                         select=True, help="Sales team to which Case belongs to."\
                                 "Define Responsible user and Email account for"\
-                                " mail gateway."), 
-        'company_id': fields.many2one('res.company', 'Company'), 
-        'partner_id': fields.many2one('res.partner', 'Partner'), 
+                                " mail gateway."),
+        'company_id': fields.many2one('res.company', 'Company'),
+        'partner_id': fields.many2one('res.partner', 'Partner'),
         'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
                                 # domain="[('partner_id','=',partner_id)]"
-                                 ), 
-        'email_cc': fields.text('Watchers Emails', size=252, help="These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma"), 
-        'email_from': fields.char('Email', size=128, help="These people will receive email."), 
-        'partner_phone': fields.char('Phone', size=32), 
-        'stage_id': fields.many2one ('crm.case.stage', 'Stage', domain="[('type','=','claim')]"), 
-        'cause': fields.text('Root Cause'), 
-        'state': fields.selection(crm.AVAILABLE_STATES, 'State', size=16, readonly=True, 
+                                 ),
+        'email_cc': fields.text('Watchers Emails', size=252, help="These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma"),
+        'email_from': fields.char('Email', size=128, help="These people will receive email."),
+        'partner_phone': fields.char('Phone', size=32),
+        'stage_id': fields.many2one ('crm.case.stage', 'Stage', domain="[('type','=','claim')]"),
+        'cause': fields.text('Root Cause'),
+        'state': fields.selection(crm.AVAILABLE_STATES, 'State', size=16, readonly=True,
                                   help='The state is set to \'Draft\', when a case is created.\
                                   \nIf the case is in progress the state is set to \'Open\'.\
                                   \nWhen the case is over, the state is set to \'Done\'.\
-                                  \nIf the case needs to be reviewed then the state is set to \'Pending\'.'), 
-        'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
+                                  \nIf the case needs to be reviewed then the state is set to \'Pending\'.'),
+        'message_ids': fields.one2many('email.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
     }
-    
+
     def stage_next(self, cr, uid, ids, context=None):
         stage = super(crm_claim, self).stage_next(cr, uid, ids, context=context)
         if stage:
@@ -95,7 +97,7 @@
             stage_obj = self.pool.get('crm.case.stage').browse(cr, uid, stage, context=context)
             self.history(cr, uid, ids, _("Changed Stage to: ") + stage_obj.name)
         return stage
-    
+
     def _get_stage_id(self, cr, uid, context=None):
         """Finds type of stage according to object.
         @param self: The object pointer
@@ -110,18 +112,18 @@
         return stage_ids and stage_ids[0] or False
 
     _defaults = {
-        'user_id': crm.crm_case._get_default_user, 
-        'partner_id': crm.crm_case._get_default_partner, 
-        'partner_address_id': crm.crm_case._get_default_partner_address, 
-        'email_from':crm.crm_case. _get_default_email, 
-        'state': lambda *a: 'draft', 
-        'section_id':crm.crm_case. _get_section, 
+        'user_id': crm.crm_case._get_default_user,
+        'partner_id': crm.crm_case._get_default_partner,
+        'partner_address_id': crm.crm_case._get_default_partner_address,
+        'email_from':crm.crm_case. _get_default_email,
+        'state': lambda *a: 'draft',
+        'section_id':crm.crm_case. _get_section,
         'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
-        'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.case', context=c), 
+        'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.case', context=c),
         'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
-        #'stage_id': _get_stage_id, 
+        #'stage_id': _get_stage_id,
     }
-    
+
     def onchange_partner_id(self, cr, uid, ids, part, email=False):
         """This function returns value of partner address based on partner
         @param self: The object pointer
@@ -133,7 +135,7 @@
         """
         if not part:
             return {'value': {'partner_address_id': False,
-                            'email_from': False, 
+                            'email_from': False,
                             'partner_phone': False,
                             'partner_mobile': False
                             }}
@@ -155,21 +157,21 @@
             return {'value': {'email_from': False}}
         address = self.pool.get('res.partner.address').browse(cr, uid, add)
         return {'value': {'email_from': address.email, 'partner_phone': address.phone, 'partner_mobile': address.mobile}}
-        
+
     def case_open(self, cr, uid, ids, *args):
         """
             Opens Claim
         """
         res = super(crm_claim, self).case_open(cr, uid, ids, *args)
         claims = self.browse(cr, uid, ids)
-        
+
         for i in xrange(0, len(ids)):
             if not claims[i].stage_id :
                 stage_id = self._find_first_stage(cr, uid, 'claim', claims[i].section_id.id or False)
                 self.write(cr, uid, [ids[i]], {'stage_id' : stage_id})
-        
+
         return res
-    
+
     def message_new(self, cr, uid, msg, context=None):
         """
         Automatically calls when new email message arrives
@@ -177,8 +179,9 @@
         @param self: The object pointer
         @param cr: the current row, from the database cursor,
         @param uid: the current user’s ID for security checks
+        @param msg: dictionary object to contain email message data
         """
-        mailgate_pool = self.pool.get('email.server.tools')
+        thread_pool = self.pool.get('email.thread')
 
         subject = msg.get('subject')
         body = msg.get('body')
@@ -195,31 +198,18 @@
         if msg.get('priority', False):
             vals['priority'] = priority
 
-        res = mailgate_pool.get_partner(cr, uid, msg.get('from') or msg.get_unixfrom())
+        res = thread_pool.get_partner(cr, uid, msg.get('from', False))
         if res:
             vals.update(res)
 
-        res = self.create(cr, uid, vals, context)
-        attachents = msg.get('attachments', [])
-        for attactment in attachents or []:
-            data_attach = {
-                'name': attactment,
-                'datas':binascii.b2a_base64(str(attachents.get(attactment))),
-                'datas_fname': attactment,
-                'description': 'Mail attachment',
-                'res_model': self._name,
-                'res_id': res,
-            }
-            self.pool.get('ir.attachment').create(cr, uid, data_attach)
-
-        return res
-
-    def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context=None):
+        return self.create(cr, uid, vals, context)
+
+    def message_update(self, cr, uid, ids, msg, vals={}, default_act='pending', context=None):
         """
         @param self: The object pointer
         @param cr: the current row, from the database cursor,
         @param uid: the current user’s ID for security checks,
-        @param ids: List of update mail’s IDs 
+        @param ids: List of update mail’s IDs
         """
         if isinstance(ids, (str, int, long)):
             ids = [ids]
@@ -251,34 +241,22 @@
             res = self.write(cr, uid, [case.id], values, context=context)
         return res
 
-    def msg_send(self, cr, uid, id, *args, **argv):
-
-        """ Send The Message
-            @param self: The object pointer
-            @param cr: the current row, from the database cursor,
-            @param uid: the current user’s ID for security checks,
-            @param ids: List of email’s IDs
-            @param *args: Return Tuple Value
-            @param **args: Return Dictionary of Keyword Value
-        """
-        return True
-
 crm_claim()
 
 
 class crm_stage_claim(osv.osv):
-    
+
     def _get_type_value(self, cr, user, context):
         list = super(crm_stage_claim, self)._get_type_value(cr, user, context)
         list.append(('claim','Claim'))
         return list
-    
+
     _inherit = "crm.case.stage"
     _columns = {
             'type': fields.selection(_get_type_value, 'Type'),
     }
-   
-    
+
+
 crm_stage_claim()
 
 

=== modified file 'crm_claim/crm_claim_view.xml'
--- crm_claim/crm_claim_view.xml	2011-03-16 12:38:21 +0000
+++ crm_claim/crm_claim_view.xml	2011-04-11 12:37:32 +0000
@@ -91,15 +91,10 @@
                     <group>
                         <field name="name" />
                         <field name="date"/>
-
-
                     </group>
-
                     <group colspan="4" col="6">
-
                         <field name="user_id"/>
                         <field name="section_id" widget="selection" />
-
                         <group colspan="2" col="4">
                             <field name="stage_id" domain="[('type','=','claim')]"/>
                             <button name="stage_previous" string="" type="object" icon="gtk-go-back" />
@@ -180,11 +175,11 @@
                                 <field name="message_ids" colspan="4" nolabel="1" mode="tree,form">
                                     <tree string="History">
                                         <field name="display_text" string="History Information"/>
-                                        <button
-                                            string="Reply"
-                                            name="%(crm.action_crm_send_mail)d"
-                                            context="{'mail':'reply', 'model': 'crm.claim', 'include_original' : True}"
-                                            icon="terp-mail-replied" type="action" attrs="{'invisible': [('history', '!=', True)]}" />
+			                            <button
+			                                string="Reply" attrs="{'invisible': [('history', '!=', True)]}"
+			                                name="%(mail.action_email_compose_message_wizard)d"
+			                                context="{'mail':'reply', 'message_id':active_id}"
+			                                icon="terp-mail-replied" type="action" />
                                     </tree>
                                     <form string="Communication &amp; History">
                                         <group col="4" colspan="4">
@@ -192,18 +187,17 @@
                                             <field name="date"/>
                                             <field name="email_to" widget="char" size="512"/>
                                             <field name="email_cc" widget="char" size="512"/>
-                                            <field name="name" colspan="4" widget="char" size="512"/>
+                                            <field name="subject" colspan="4" widget="char" size="512"/>
                                             <field name="history" invisible="1"/>
                                         </group>
                                         <notebook colspan="4">
                                             <page string="Details">
                                                 <group attrs="{'invisible': [('history', '!=', True)]}">
-                                                    <field name="description" colspan="4" nolabel="1" height="250"/>
-                                                    <button colspan="4"
-                                                        string="Reply"
-                                                        name="%(crm.action_crm_send_mail)d"
-                                                        context="{'mail':'reply', 'model': 'crm.claim', 'include_original' : True}"
-                                                        icon="terp-mail-replied" type="action" />
+                                                    <field name="body" colspan="4" nolabel="1" height="250"/>
+			                                        <button colspan="4" string="Reply"
+			                                            name="%(mail.action_email_compose_message_wizard)d"
+			                                            context="{'mail':'reply', 'message_id':active_id}"
+			                                            icon="terp-mail-replied" type="action"/>
                                                 </group>
                                                 <group attrs="{'invisible': [('history', '=', True)]}">
                                                     <field name="display_text" colspan="4" nolabel="1"  height="250"/>
@@ -219,10 +213,10 @@
                                     name="%(crm.action_crm_add_note)d"
                                     context="{'model': 'crm.lead' }"
                                     icon="terp-document-new" type="action" />
-                                <button string="Send New Email"
-                                    name="%(crm.action_crm_send_mail)d"
-                                    context="{'mail':'new', 'model': 'crm.claim'}"
-                                    icon="terp-mail-message-new" type="action" />
+			                    <button string="Send New Email"
+			                        name="%(mail.action_email_compose_message_wizard)d"
+			                        context="{'email_model':'crm.claim', 'email_res_id': active_id}"
+			                        icon="terp-mail-message-new" type="action"/>
                             </page>
                         </notebook>
                     </group>

=== modified file 'crm_claim/report/crm_claim_report.py'
--- crm_claim/report/crm_claim_report.py	2011-01-17 13:37:19 +0000
+++ crm_claim/report/crm_claim_report.py	2011-04-11 12:37:32 +0000
@@ -60,7 +60,7 @@
                                   ('11', 'November'), ('12', 'December')], 'Month', readonly=True),
         'company_id': fields.many2one('res.company', 'Company', readonly=True),
         'create_date': fields.datetime('Create Date', readonly=True, select=True),
-        'day': fields.char('Day', size=128, readonly=True), 
+        'day': fields.char('Day', size=128, readonly=True),
         'delay_close': fields.float('Delay to close', digits=(16,2),readonly=True, group_operator="avg",help="Number of Days to close the case"),
         'stage_id': fields.many2one ('crm.case.stage', 'Stage', readonly=True, domain="[('type','=','claim')]"),
         'categ_id': fields.many2one('crm.case.categ', 'Category',\
@@ -71,8 +71,8 @@
         'company_id': fields.many2one('res.company', 'Company', readonly=True),
         'priority': fields.selection(AVAILABLE_PRIORITIES, 'Priority'),
         'type_action': fields.selection([('correction','Corrective Action'),('prevention','Preventive Action')], 'Action Type'),
-        'date_closed': fields.date('Close Date', readonly=True, select=True), 
-        'date_deadline': fields.date('Deadline', readonly=True, select=True), 
+        'date_closed': fields.date('Close Date', readonly=True, select=True),
+        'date_deadline': fields.date('Deadline', readonly=True, select=True),
         'delay_expected': fields.float('Overpassed Deadline',digits=(16,2),readonly=True, group_operator="avg"),
         'email': fields.integer('# Emails', size=128, readonly=True),
          'probability': fields.float('Probability',digits=(16,2),readonly=True, group_operator="avg")
@@ -106,7 +106,7 @@
                     c.type_action as type_action,
                     date_trunc('day',c.create_date) as create_date,
                     avg(extract('epoch' from (c.date_closed-c.create_date)))/(3600*24) as  delay_close,
-                    (SELECT count(id) FROM mailgate_message WHERE model='crm.claim' AND res_id=c.id AND history=True) AS email,
+                    (SELECT count(id) FROM email_message WHERE model='crm.claim' AND res_id=c.id AND history=True) AS email,
                     (SELECT avg(probability) FROM crm_case_stage WHERE type='claim' AND id=c.stage_id) AS probability,
                     extract('epoch' from (c.date_deadline - c.date_closed))/(3600*24) as  delay_expected
                 from

=== modified file 'crm_fundraising/crm_fundraising.py'
--- crm_fundraising/crm_fundraising.py	2011-01-14 00:11:01 +0000
+++ crm_fundraising/crm_fundraising.py	2011-04-11 12:37:32 +0000
@@ -21,6 +21,9 @@
 
 from osv import fields, osv
 from crm import crm
+from crm import wizard
+
+wizard.email_compose_message.email_model.append('crm.fundraising')
 
 class crm_fundraising(crm.crm_case, osv.osv):
     """ Fund Raising Cases """
@@ -28,85 +31,85 @@
     _name = "crm.fundraising"
     _description = "Fund Raising"
     _order = "id desc"
-    _inherit = ['mailgate.thread']
+    _inherit = ['email.thread']
     _columns = {
-            'id': fields.integer('ID'), 
+            'id': fields.integer('ID'),
             'name': fields.char('Name', size=128, required=True),
-            'active': fields.boolean('Active', required=False), 
+            'active': fields.boolean('Active', required=False),
             'date_action_last': fields.datetime('Last Action', readonly=1),
-            'date_action_next': fields.datetime('Next Action', readonly=1), 
-            'description': fields.text('Description'), 
-            'create_date': fields.datetime('Creation Date' , readonly=True), 
-            'write_date': fields.datetime('Update Date' , readonly=True), 
-            'date_deadline': fields.date('Deadline'), 
-            'user_id': fields.many2one('res.users', 'Responsible'), 
+            'date_action_next': fields.datetime('Next Action', readonly=1),
+            'description': fields.text('Description'),
+            'create_date': fields.datetime('Creation Date' , readonly=True),
+            'write_date': fields.datetime('Update Date' , readonly=True),
+            'date_deadline': fields.date('Deadline'),
+            'user_id': fields.many2one('res.users', 'Responsible'),
             'section_id': fields.many2one('crm.case.section', 'Sales Team', \
-                            select=True, help='Sales team to which Case belongs to. Define Responsible user and Email account for mail gateway.'), 
-            'company_id': fields.many2one('res.company', 'Company'), 
-            'partner_id': fields.many2one('res.partner', 'Partner'), 
+                            select=True, help='Sales team to which Case belongs to. Define Responsible user and Email account for mail gateway.'),
+            'company_id': fields.many2one('res.company', 'Company'),
+            'partner_id': fields.many2one('res.partner', 'Partner'),
             'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
-                                 domain="[('partner_id','=',partner_id)]"), 
-            'email_cc': fields.text('Watchers Emails', size=252 , help="These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma"), 
-            'email_from': fields.char('Email', size=128, help="These people will receive email."), 
-            'date_closed': fields.datetime('Closed', readonly=True), 
-            'date': fields.datetime('Date'), 
-            'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'), 
+                                 domain="[('partner_id','=',partner_id)]"),
+            'email_cc': fields.text('Watchers Emails', size=252 , help="These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma"),
+            'email_from': fields.char('Email', size=128, help="These people will receive email."),
+            'date_closed': fields.datetime('Closed', readonly=True),
+            'date': fields.datetime('Date'),
+            'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
             'categ_id': fields.many2one('crm.case.categ', 'Category', \
                                 domain="[('section_id','=',section_id),\
-                                ('object_id.model', '=', 'crm.fundraising')]"), 
-            'planned_revenue': fields.float('Planned Revenue'), 
-            'planned_cost': fields.float('Planned Costs'), 
-            'probability': fields.float('Probability (%)'), 
-            'partner_name': fields.char("Employee's Name", size=64), 
-            'partner_name2': fields.char('Employee Email', size=64), 
-            'partner_phone': fields.char('Phone', size=32), 
-            'partner_mobile': fields.char('Mobile', size=32), 
-            'stage_id': fields.many2one ('crm.case.stage', 'Stage', domain="[('type', '=', 'fundraising')]"), 
+                                ('object_id.model', '=', 'crm.fundraising')]"),
+            'planned_revenue': fields.float('Planned Revenue'),
+            'planned_cost': fields.float('Planned Costs'),
+            'probability': fields.float('Probability (%)'),
+            'partner_name': fields.char("Employee's Name", size=64),
+            'partner_name2': fields.char('Employee Email', size=64),
+            'partner_phone': fields.char('Phone', size=32),
+            'partner_mobile': fields.char('Mobile', size=32),
+            'stage_id': fields.many2one ('crm.case.stage', 'Stage', domain="[('type', '=', 'fundraising')]"),
             'type_id': fields.many2one('crm.case.resource.type', 'Campaign', \
-                             domain="[('section_id','=',section_id)]"), 
-            'duration': fields.float('Duration'), 
-            'ref': fields.reference('Reference', selection=crm._links_get, size=128), 
-            'ref2': fields.reference('Reference 2', selection=crm._links_get, size=128), 
+                             domain="[('section_id','=',section_id)]"),
+            'duration': fields.float('Duration'),
+            'ref': fields.reference('Reference', selection=crm._links_get, size=128),
+            'ref2': fields.reference('Reference 2', selection=crm._links_get, size=128),
             'canal_id': fields.many2one('res.partner.canal', 'Channel', \
                         help="The channels represent the different communication \
- modes available with the customer."), 
-            'state': fields.selection(crm.AVAILABLE_STATES, 'State', size=16, readonly=True, 
+ modes available with the customer."),
+            'state': fields.selection(crm.AVAILABLE_STATES, 'State', size=16, readonly=True,
                                   help='The state is set to \'Draft\', when a case is created.\
                                   \nIf the case is in progress the state is set to \'Open\'.\
                                   \nWhen the case is over, the state is set to \'Done\'.\
-                                  \nIf the case needs to be reviewed then the state is set to \'Pending\'.'), 
-            'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
+                                  \nIf the case needs to be reviewed then the state is set to \'Pending\'.'),
+            'message_ids': fields.one2many('email.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
         }
 
     _defaults = {
-            'active': lambda *a: 1, 
-            'user_id': crm.crm_case._get_default_user, 
-            'partner_id': crm.crm_case._get_default_partner, 
-            'partner_address_id': crm.crm_case._get_default_partner_address, 
-            'email_from': crm.crm_case. _get_default_email, 
-            'state': lambda *a: 'draft', 
-            'section_id': crm.crm_case. _get_section, 
-            'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.case', context=c), 
-            'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0], 
-            'probability': lambda *a:0.0, 
-            'planned_cost': lambda *a:0.0, 
-            'planned_revenue': lambda *a:0.0, 
+            'active': lambda *a: 1,
+            'user_id': crm.crm_case._get_default_user,
+            'partner_id': crm.crm_case._get_default_partner,
+            'partner_address_id': crm.crm_case._get_default_partner_address,
+            'email_from': crm.crm_case. _get_default_email,
+            'state': lambda *a: 'draft',
+            'section_id': crm.crm_case. _get_section,
+            'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.case', context=c),
+            'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
+            'probability': lambda *a:0.0,
+            'planned_cost': lambda *a:0.0,
+            'planned_revenue': lambda *a:0.0,
             }
 
 crm_fundraising()
 
 
 class crm_stage_fundraising(osv.osv):
-    
+
     def _get_type_value(self, cr, user, context):
         list = super(crm_stage_fundraising, self)._get_type_value(cr, user, context)
         list.append(('fundraising','Fundraising'))
         return list
-    
+
     _inherit = "crm.case.stage"
     _columns = {
             'type': fields.selection(_get_type_value, 'Type'),
     }
-   
-    
+
+
 crm_stage_fundraising()

=== modified file 'crm_fundraising/crm_fundraising_view.xml'
--- crm_fundraising/crm_fundraising_view.xml	2011-02-22 14:15:01 +0000
+++ crm_fundraising/crm_fundraising_view.xml	2011-04-11 12:37:32 +0000
@@ -166,11 +166,11 @@
                                 <tree string="History">
                                     <field name="display_text" string="History Information"/>
                                     <field name="history" invisible="1"/>
-                                    <button
-                                        string="Reply" attrs="{'invisible': [('history', '!=', True)]}"
-                                        name="%(crm.action_crm_send_mail)d"
-                                        context="{'mail':'reply', 'model': 'crm.fundraising', 'include_original' : True}"
-                                        icon="terp-mail-replied" type="action" />
+		                            <button
+		                                string="Reply" attrs="{'invisible': [('history', '!=', True)]}"
+		                                name="%(mail.action_email_compose_message_wizard)d"
+		                                context="{'mail':'reply', 'message_id':active_id}"
+		                                icon="terp-mail-replied" type="action" />
                                 </tree>
                                 <form string="History">
                                     <group col="4" colspan="4">
@@ -178,18 +178,17 @@
                                         <field name="date"/>
                                         <field name="email_to" widget="char" size="512"/>
                                         <field name="email_cc" widget="char" size="512"/>
-                                        <field name="name" colspan="4" widget="char" size="512"/>
+                                        <field name="subject" colspan="4" widget="char" size="512"/>
                                         <field name="history" invisible="1"/>
                                     </group>
                                     <notebook colspan="4">
                                         <page string="Details">
                                             <group attrs="{'invisible': [('history', '!=', True)]}">
-                                                <field name="description" colspan="4" nolabel="1" height="250"/>
-                                                <button colspan="4"
-                                                    string="Reply"
-                                                    name="%(crm.action_crm_send_mail)d"
-                                                    context="{'mail':'reply', 'model': 'crm.fundraising', 'include_original' : True}"
-                                                    icon="terp-mail-replied" type="action" />
+                                                <field name="body" colspan="4" nolabel="1" height="250"/>
+		                                        <button colspan="4" string="Reply"
+		                                            name="%(mail.action_email_compose_message_wizard)d"
+		                                            context="{'mail':'reply', 'message_id':active_id}"
+		                                            icon="terp-mail-replied" type="action"/>
                                             </group>
                                             <group attrs="{'invisible': [('history', '=', True)]}">
                                                 <field name="display_text" colspan="4" nolabel="1"  height="250"/>
@@ -205,10 +204,10 @@
                                 name="%(crm.action_crm_add_note)d"
                                 context="{'model': 'crm.lead' }"
                                 icon="terp-document-new" type="action" />
-                            <button string="Send New Email"
-                                name="%(crm.action_crm_send_mail)d"
-                                context="{'mail':'new', 'model': 'crm.fundraising'}"
-                                icon="terp-mail-message-new" type="action" />
+		                    <button string="Send New Email"
+		                        name="%(mail.action_email_compose_message_wizard)d"
+		                        context="{'email_model':'crm.fundraising', 'email_res_id': active_id}"
+		                        icon="terp-mail-message-new" type="action"/>
                         </page>
                         <page string="Extra Info" groups="base.group_extended">
                             <group col="2" colspan="2">

=== modified file 'crm_helpdesk/__init__.py'
--- crm_helpdesk/__init__.py	2011-01-14 00:11:01 +0000
+++ crm_helpdesk/__init__.py	2011-04-11 12:37:32 +0000
@@ -21,6 +21,7 @@
 
 import crm_helpdesk
 import report
+import wizard
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 

=== modified file 'crm_helpdesk/crm_helpdesk.py'
--- crm_helpdesk/crm_helpdesk.py	2011-02-15 09:42:10 +0000
+++ crm_helpdesk/crm_helpdesk.py	2011-04-11 12:37:32 +0000
@@ -22,6 +22,7 @@
 from crm import crm
 from osv import fields, osv
 import time
+from crm import wizard
 import binascii
 import tools
 
@@ -37,61 +38,61 @@
     _name = "crm.helpdesk"
     _description = "Helpdesk"
     _order = "id desc"
-    _inherit = ['mailgate.thread']
+    _inherit = ['email.thread']
     _columns = {
-            'id': fields.integer('ID', readonly=True), 
-            'name': fields.char('Name', size=128, required=True), 
-            'active': fields.boolean('Active', required=False), 
-            'date_action_last': fields.datetime('Last Action', readonly=1), 
-            'date_action_next': fields.datetime('Next Action', readonly=1), 
-            'description': fields.text('Description'), 
-            'create_date': fields.datetime('Creation Date' , readonly=True), 
-            'write_date': fields.datetime('Update Date' , readonly=True), 
-            'date_deadline': fields.date('Deadline'), 
-            'user_id': fields.many2one('res.users', 'Responsible'), 
+            'id': fields.integer('ID', readonly=True),
+            'name': fields.char('Name', size=128, required=True),
+            'active': fields.boolean('Active', required=False),
+            'date_action_last': fields.datetime('Last Action', readonly=1),
+            'date_action_next': fields.datetime('Next Action', readonly=1),
+            'description': fields.text('Description'),
+            'create_date': fields.datetime('Creation Date' , readonly=True),
+            'write_date': fields.datetime('Update Date' , readonly=True),
+            'date_deadline': fields.date('Deadline'),
+            'user_id': fields.many2one('res.users', 'Responsible'),
             'section_id': fields.many2one('crm.case.section', 'Sales Team', \
                             select=True, help='Sales team to which Case belongs to.\
-                                 Define Responsible user and Email account for mail gateway.'), 
-            'company_id': fields.many2one('res.company', 'Company'), 
-            'date_closed': fields.datetime('Closed', readonly=True), 
-            'partner_id': fields.many2one('res.partner', 'Partner'), 
+                                 Define Responsible user and Email account for mail gateway.'),
+            'company_id': fields.many2one('res.company', 'Company'),
+            'date_closed': fields.datetime('Closed', readonly=True),
+            'partner_id': fields.many2one('res.partner', 'Partner'),
             'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
-                                 domain="[('partner_id','=',partner_id)]"), 
-            'email_cc': fields.text('Watchers Emails', size=252 , help="These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma"), 
-            'email_from': fields.char('Email', size=128, help="These people will receive email."), 
-            'date': fields.datetime('Date'), 
-            'ref' : fields.reference('Reference', selection=crm._links_get, size=128), 
-            'ref2' : fields.reference('Reference 2', selection=crm._links_get, size=128), 
+                                 domain="[('partner_id','=',partner_id)]"),
+            'email_cc': fields.text('Watchers Emails', size=252 , help="These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma"),
+            'email_from': fields.char('Email', size=128, help="These people will receive email."),
+            'date': fields.datetime('Date'),
+            'ref' : fields.reference('Reference', selection=crm._links_get, size=128),
+            'ref2' : fields.reference('Reference 2', selection=crm._links_get, size=128),
             'canal_id': fields.many2one('res.partner.canal', 'Channel', \
                             help="The channels represent the different communication \
- modes available with the customer."), 
-            'planned_revenue': fields.float('Planned Revenue'), 
-            'planned_cost': fields.float('Planned Costs'), 
-            'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'), 
-            'probability': fields.float('Probability (%)'), 
+ modes available with the customer."),
+            'planned_revenue': fields.float('Planned Revenue'),
+            'planned_cost': fields.float('Planned Costs'),
+            'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
+            'probability': fields.float('Probability (%)'),
             'categ_id': fields.many2one('crm.case.categ', 'Category', \
                             domain="[('section_id','=',section_id),\
-                            ('object_id.model', '=', 'crm.helpdesk')]"), 
-            'duration': fields.float('Duration', states={'done': [('readonly', True)]}), 
-            'state': fields.selection(crm.AVAILABLE_STATES, 'State', size=16, readonly=True, 
+                            ('object_id.model', '=', 'crm.helpdesk')]"),
+            'duration': fields.float('Duration', states={'done': [('readonly', True)]}),
+            'state': fields.selection(crm.AVAILABLE_STATES, 'State', size=16, readonly=True,
                                   help='The state is set to \'Draft\', when a case is created.\
                                   \nIf the case is in progress the state is set to \'Open\'.\
                                   \nWhen the case is over, the state is set to \'Done\'.\
                                   \nIf the case needs to be reviewed then the state is set to \'Pending\'.'),
-            'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
+            'message_ids': fields.one2many('email.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
     }
 
     _defaults = {
-        'active': lambda *a: 1, 
-        'user_id': crm.crm_case._get_default_user, 
-        'partner_id': crm.crm_case._get_default_partner, 
-        'partner_address_id': crm.crm_case._get_default_partner_address, 
-        'email_from': crm.crm_case. _get_default_email, 
-        'state': lambda *a: 'draft', 
+        'active': lambda *a: 1,
+        'user_id': crm.crm_case._get_default_user,
+        'partner_id': crm.crm_case._get_default_partner,
+        'partner_address_id': crm.crm_case._get_default_partner_address,
+        'email_from': crm.crm_case. _get_default_email,
+        'state': lambda *a: 'draft',
         'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
-        'section_id': crm.crm_case. _get_section, 
-        'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c), 
-        'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0], 
+        'section_id': crm.crm_case. _get_section,
+        'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c),
+        'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
     }
 
     def message_new(self, cr, uid, msg, context=None):
@@ -101,8 +102,9 @@
         @param self: The object pointer
         @param cr: the current row, from the database cursor,
         @param uid: the current user’s ID for security checks
+        @param msg: dictionary object to contain email message data
         """
-        mailgate_pool = self.pool.get('email.server.tools')
+        thread_pool = self.pool.get('email.thread')
 
         subject = msg.get('subject')
         body = msg.get('body')
@@ -119,31 +121,18 @@
         if msg.get('priority', False):
             vals['priority'] = priority
 
-        res = mailgate_pool.get_partner(cr, uid, msg.get('from') or msg.get_unixfrom())
+        res = thread_pool.get_partner(cr, uid, msg.get('from', False))
         if res:
             vals.update(res)
 
-        res = self.create(cr, uid, vals, context)
-        attachents = msg.get('attachments', [])
-        for attactment in attachents or []:
-            data_attach = {
-                'name': attactment,
-                'datas':binascii.b2a_base64(str(attachents.get(attactment))),
-                'datas_fname': attactment,
-                'description': 'Mail attachment',
-                'res_model': self._name,
-                'res_id': res,
-            }
-            self.pool.get('ir.attachment').create(cr, uid, data_attach)
-
-        return res
-
-    def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context=None):
+        return self.create(cr, uid, vals, context)
+
+    def message_update(self, cr, uid, ids, msg, vals={}, default_act='pending', context=None):
         """
         @param self: The object pointer
         @param cr: the current row, from the database cursor,
         @param uid: the current user’s ID for security checks,
-        @param ids: List of update mail’s IDs 
+        @param ids: List of update mail’s IDs
         """
         if isinstance(ids, (str, int, long)):
             ids = [ids]
@@ -175,18 +164,6 @@
             res = self.write(cr, uid, [case.id], values, context=context)
         return res
 
-    def msg_send(self, cr, uid, id, *args, **argv):
-
-        """ Send The Message
-            @param self: The object pointer
-            @param cr: the current row, from the database cursor,
-            @param uid: the current user’s ID for security checks,
-            @param ids: List of email’s IDs
-            @param *args: Return Tuple Value
-            @param **args: Return Dictionary of Keyword Value
-        """
-        return True
-
 crm_helpdesk()
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'crm_helpdesk/crm_helpdesk_view.xml'
--- crm_helpdesk/crm_helpdesk_view.xml	2011-02-22 08:55:27 +0000
+++ crm_helpdesk/crm_helpdesk_view.xml	2011-04-11 12:37:32 +0000
@@ -100,11 +100,11 @@
                                 <tree string="History">
                                     <field name="display_text" string="History Information"/>
                                     <field name="history" invisible="1"/>
-                                    <button
-                                        string="Reply" attrs="{'invisible': [('history', '!=', True)]}"
-                                        name="%(crm.action_crm_send_mail)d"
-                                        context="{'mail':'reply', 'model': 'crm.helpdesk', 'include_original' : True}"
-                                        icon="terp-mail-replied" type="action" />
+		                            <button
+		                                string="Reply" attrs="{'invisible': [('history', '!=', True)]}"
+		                                name="%(mail.action_email_compose_message_wizard)d"
+		                                context="{'mail':'reply', 'message_id':active_id}"
+		                                icon="terp-mail-replied" type="action" />
                                 </tree>
                                 <form string="History">
                                     <group col="4" colspan="4">
@@ -112,18 +112,17 @@
                                         <field name="date"/>
                                         <field name="email_to" widget="char" size="512"/>
                                         <field name="email_cc" widget="char" size="512"/>
-                                        <field name="name" colspan="4" widget="char" size="512"/>
+                                        <field name="subject" colspan="4" widget="char" size="512"/>
                                         <field name="history" invisible="1"/>
                                     </group>
                                     <notebook colspan="4">
                                         <page string="Details">
                                             <group attrs="{'invisible': [('history', '!=', True)]}">
-                                                <field name="description" colspan="4" nolabel="1" height="250"/>
-                                                <button colspan="4"
-                                                    string="Reply"
-                                                    name="%(crm.action_crm_send_mail)d"
-                                                    context="{'mail':'reply', 'model': 'crm.helpdesk', 'include_original' : True}"
-                                                    icon="terp-mail-replied" type="action" />
+                                                <field name="body" colspan="4" nolabel="1" height="250"/>
+		                                        <button colspan="4" string="Reply"
+		                                            name="%(mail.action_email_compose_message_wizard)d"
+		                                            context="{'mail':'reply', 'message_id':active_id}"
+		                                            icon="terp-mail-replied" type="action"/>
                                              </group>
                                             <group attrs="{'invisible': [('history', '=', True)]}">
                                                 <field name="display_text" colspan="4" nolabel="1"  height="250"/>
@@ -139,10 +138,10 @@
                                 name="%(crm.action_crm_add_note)d"
                                 context="{'model': 'crm.lead' }"
                                 icon="terp-document-new" type="action" />
-                            <button string="Send New Email"
-                                name="%(crm.action_crm_send_mail)d"
-                                context="{'mail':'new', 'model': 'crm.helpdesk'}"
-                                icon="terp-mail-message-new" type="action" />
+		                    <button string="Send New Email"
+		                        name="%(mail.action_email_compose_message_wizard)d"
+		                        context="{'email_model':'crm.helpdesk', 'email_res_id': active_id}"
+		                        icon="terp-mail-message-new" type="action"/>
                         </page>
                         <page string="Extra Info" groups="base.group_extended">
                             <group colspan="2" col="2">

=== modified file 'crm_helpdesk/report/crm_helpdesk_report.py'
--- crm_helpdesk/report/crm_helpdesk_report.py	2011-01-17 13:37:19 +0000
+++ crm_helpdesk/report/crm_helpdesk_report.py	2011-04-11 12:37:32 +0000
@@ -56,7 +56,7 @@
         'date_deadline': fields.date('Deadline', select=True),
         'priority': fields.selection([('5', 'Lowest'), ('4', 'Low'), \
                     ('3', 'Normal'), ('2', 'High'), ('1', 'Highest')], 'Priority'),
-        'canal_id': fields.many2one('res.partner.canal', 'Channel'), 
+        'canal_id': fields.many2one('res.partner.canal', 'Channel'),
         'categ_id': fields.many2one('crm.case.categ', 'Category', \
                             domain="[('section_id','=',section_id),\
                             ('object_id.model', '=', 'crm.helpdesk')]"),
@@ -97,7 +97,7 @@
                     c.planned_cost,
                     count(*) as nbr,
                     extract('epoch' from (c.date_closed-c.create_date))/(3600*24) as  delay_close,
-                    (SELECT count(id) FROM mailgate_message WHERE model='crm.helpdesk' AND res_id=c.id AND history=True) AS email,
+                    (SELECT count(id) FROM email_message WHERE model='crm.helpdesk' AND res_id=c.id AND history=True) AS email,
                     abs(avg(extract('epoch' from (c.date_deadline - c.date_closed)))/(3600*24)) as delay_expected
                 from
                     crm_helpdesk c

=== added directory 'crm_helpdesk/wizard'
=== added file 'crm_helpdesk/wizard/__init__.py'
--- crm_helpdesk/wizard/__init__.py	1970-01-01 00:00:00 +0000
+++ crm_helpdesk/wizard/__init__.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,25 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details.
+#
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+import email_compose_message
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
+

=== added file 'crm_helpdesk/wizard/email_compose_message.py'
--- crm_helpdesk/wizard/email_compose_message.py	1970-01-01 00:00:00 +0000
+++ crm_helpdesk/wizard/email_compose_message.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,49 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+from osv import osv
+from osv import fields
+import tools
+
+class email_compose_message(osv.osv_memory):
+    _inherit = 'email.compose.message'
+
+    def get_value(self, cr, uid, model, resource_id, context=None):
+        if context is None:
+            context = {}
+        result = super(email_compose_message, self).get_value(cr, uid,  model, resource_id, context=context)
+        if model == 'crm.helpdesk' and resource_id:
+            model_obj = self.pool.get(model)
+            data = model_obj.browse(cr, uid , resource_id, context)
+            result.update({
+                    'subject' : data.name or False,
+                    'email_to' : data.email_from or False,
+                    'email_from' : data.user_id and data.user_id.address_id and data.user_id.address_id.email or False,
+                    'body' : '\n' + (tools.ustr(data.user_id.signature or '')),
+                    'email_cc' : tools.ustr(data.email_cc or ''),
+                    'model': model  or False,
+                    'res_id': resource_id  or False,
+                })
+        return result
+
+email_compose_message()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'crm_partner_assign/test/test_crm_partner_assign.yml'
--- crm_partner_assign/test/test_crm_partner_assign.yml	2011-02-08 10:47:14 +0000
+++ crm_partner_assign/test/test_crm_partner_assign.yml	2011-04-11 12:37:32 +0000
@@ -1,43 +1,43 @@
 -
   In order to test Forward Partner functionality, I create an opportunity and forward it to partner.
-- 
+-
   I assign an email address to Administrator.
-- 
+-
   !record {model: res.users, id: base.user_root}:
     user_email: admin@xxxxxxxxxxx
-- 
+-
   I create some partner grades.
 -
   I create a grade 'First'.
-- 
+-
   !record {model: res.partner.grade, id: res_partner_grade_first0}:
     name: First
     sequence: 1
-- 
+-
   I create another grade 'Second'.
-- 
+-
   !record {model: res.partner.grade, id: res_partner_grade_second0}:
     name: Second
     sequence: 2
-- 
+-
   I create one more grade 'Third'.
-- 
+-
   !record {model: res.partner.grade, id: res_partner_grade_third0}:
     name: Third
     sequence: 3
-- 
+-
   I assign grade 'First' to the partner 'Axelor'.
-- 
+-
   !record {model: res.partner, id: base.res_partner_desertic_hispafuentes}:
     grade_id: res_partner_grade_first0
 -
   I assgin a reply-to email address to Sales Team.
-- 
+-
   !record {model: crm.case.section, id: crm.section_sales_department}:
     reply_to: sales_openerp@xxxxxxxxxxx
-- 
+-
   I create an opportunity 'Questionnaire on OpenERP'.
-- 
+-
   !record {model: crm.lead, id: crm_lead_questionnaireonopenerp0}:
     categ_id: crm.categ_oppor7
     section_id: crm.section_sales_department
@@ -62,7 +62,7 @@
   !python {model: crm.lead.forward.to.partner}: |
     import tools
     vals = {
-       'name': 'email',
+       'subject': 'email',
        'email_to': 'info@xxxxxxxxxx',
        'email_from': 'Administrator <admin@xxxxxxxxxxx>',
        'reply_to': 'sales_openerp@xxxxxxxxxxx'
@@ -70,4 +70,4 @@
     ids = self.create(cr, uid, vals, context={'active_id': ref('crm_lead_questionnaireonopenerp0'), 'active_model': 'crm.lead'})
     assert tools.config.get('smtp_user', False), 'SMTP not configured !'
     self.action_forward(cr, uid, [ids], context={'active_id': ref('crm_lead_questionnaireonopenerp0'), 'active_model': 'crm.lead'})
-    
+

=== modified file 'crm_partner_assign/wizard/crm_forward_to_partner.py'
--- crm_partner_assign/wizard/crm_forward_to_partner.py	2011-02-24 15:03:09 +0000
+++ crm_partner_assign/wizard/crm_forward_to_partner.py	2011-04-11 12:37:32 +0000
@@ -30,10 +30,10 @@
 class crm_lead_forward_to_partner(osv.osv_memory):
     """Forwards lead history"""
     _name = 'crm.lead.forward.to.partner'
-    _inherit = "crm.send.mail"
+    _inherit = "email.compose.message"
 
     _columns = {
-        'name': fields.selection([('user', 'User'), ('partner', 'Partner'), \
+        'send_to': fields.selection([('user', 'User'), ('partner', 'Partner'), \
                          ('email', 'Email Address')], 'Send to', required=True),
         'user_id': fields.many2one('res.users', "User"),
         'partner_id' : fields.many2one('res.partner', 'Partner'),
@@ -42,7 +42,7 @@
     }
 
     _defaults = {
-        'name' : 'email',
+        'send_to' : 'email',
         'history': 'latest',
         'email_from': lambda self, cr, uid, *a: self.pool.get('res.users')._get_email_from(cr, uid, uid)[uid]
     }
@@ -69,13 +69,13 @@
         @param hist_id: Id of latest history
         @param context: A standard dictionary for contextual values
         """
-        log_pool = self.pool.get('mailgate.message')
+        log_pool = self.pool.get('email.message')
         hist = log_pool.browse(cr, uid, hist_id, context=context)
         header = '-------- Original Message --------'
         sender = 'From: %s' %(hist.email_from or '')
         to = 'To: %s' % (hist.email_to or '')
         sentdate = 'Date: %s' % (hist.date or '')
-        desc = '\n%s'%(hist.description)
+        desc = '\n%s'%(hist.body)
         original = [header, sender, to, sentdate, desc]
         original = '\n'.join(original)
         return original
@@ -87,7 +87,7 @@
         @param uid: the current user’s ID for security checks,
         @param ids: List of Mail’s IDs
         @param user: Changed User id
-        @param partner: Changed Partner id  
+        @param partner: Changed Partner id
         """
         if not user:
             return {'value': {'email_to': False}}
@@ -110,7 +110,7 @@
         res_id = context.get('active_id')
         msg_val = self._get_case_history(cr, uid, history_type, res_id, context=context)
         if msg_val:
-            res = {'value': {'body' : '\n\n' + msg_val}}
+            res = {'value': {'description' : '\n\n' + msg_val}}
         return res
 
     def _get_case_history(self, cr, uid, history_type, res_id, context=None):
@@ -146,7 +146,7 @@
         @param uid: the current user’s ID for security checks,
         @param ids: List of Mail’s IDs
         @param user: Changed User id
-        @param partner: Changed Partner id  
+        @param partner: Changed Partner id
         """
         if not partner_id:
             return {'value' : {'email_to' : False, 'address_id': False}}
@@ -155,13 +155,13 @@
         addr = partner_obj.address_get(cr, uid, [partner_id], ['contact'])
         data = {'address_id': addr['contact']}
         data.update(self.on_change_address(cr, uid, ids, addr['contact'])['value'])
-        
+
         partner = partner_obj.browse(cr, uid, [partner_id])
         user_id = partner and partner[0].user_id or False
         email = user_id and user_id.user_email or ''
         data.update({'email_cc' : email})
         return {
-            'value' : data, 
+            'value' : data,
             'domain' : {'address_id' : partner_id and "[('partner_id', '=', partner_id)]" or "[]"}
             }
 
@@ -171,6 +171,13 @@
             email = self.pool.get('res.partner.address').browse(cr, uid, address_id).email
         return {'value': {'email_to' : email}}
 
+    def send_mail(self, cr, uid, ids, context=None):
+        if context is None:
+            context = {}
+        super(crm_lead_forward_to_partner, self).send_mail(cr, uid, ids, context=context)
+        self.action_forward(cr, uid, ids, context)
+        return {'type': 'ir.actions.act_window_close'}
+
     def action_forward(self, cr, uid, ids, context=None):
         """
         Forward the lead to a partner
@@ -181,15 +188,13 @@
         case_pool = self.pool.get(context.get('active_model'))
         res_id = context and context.get('active_id', False) or False
         case = case_pool.browse(cr, uid, res_id, context=context)
-
         context.update({'mail': 'forward'})
-        super(crm_lead_forward_to_partner, self).action_send(cr, uid, ids, context=context)
 
         to_write = {'date_assign': time.strftime('%Y-%m-%d')}
-        if (this.name == 'partner' and this.partner_id):
+        if (this.send_to == 'partner' and this.partner_id):
             to_write['partner_assigned_id'] = this.partner_id.id
 
-        if this.name == 'user':
+        if this.send_to == 'user':
             to_write.update({'user_id' : this.user_id.id})
         email_re = r'([^ ,<@]+@[^> ,]+)'
         email_cc = re.findall(email_re, case.email_cc or '')
@@ -203,7 +208,6 @@
                 new_cc.append(to)
         to_write.update({'email_cc' : ', '.join(new_cc) })
         case_pool.write(cr, uid, case.id, to_write, context=context)
-
         return {'type': 'ir.actions.act_window_close'}
 
     def get_lead_details(self, cr, uid, lead_id, context=None):
@@ -237,22 +241,22 @@
         elif lead.type == 'opportunity':
             pa = lead.partner_address_id
             body = [
-                "Partner: %s" % (lead.partner_id and lead.partner_id.name_get()[0][1]), 
-                "Contact: %s" % (pa.name or ''), 
-                "Title: %s" % (pa.title or ''), 
-                "Function: %s" % (pa.function or ''), 
-                "Street: %s" % (pa.street or ''), 
-                "Street2: %s" % (pa.street2 or ''), 
-                "Zip: %s" % (pa.zip or ''), 
-                "City: %s" % (pa.city or ''), 
-                "Country: %s" % (pa.country_id and pa.country_id.name_get()[0][1] or ''), 
-                "State: %s" % (pa.state_id and pa.state_id.name_get()[0][1] or ''), 
-                "Email: %s" % (pa.email or ''), 
-                "Phone: %s" % (pa.phone or ''), 
-                "Fax: %s" % (pa.fax or ''), 
-                "Mobile: %s" % (pa.mobile or ''), 
-                "Lead Category: %s" % (lead.categ_id and lead.categ_id.name or ''), 
-                "Details: %s" % (lead.description or ''), 
+                "Partner: %s" % (lead.partner_id and lead.partner_id.name_get()[0][1]),
+                "Contact: %s" % (pa.name or ''),
+                "Title: %s" % (pa.title or ''),
+                "Function: %s" % (pa.function or ''),
+                "Street: %s" % (pa.street or ''),
+                "Street2: %s" % (pa.street2 or ''),
+                "Zip: %s" % (pa.zip or ''),
+                "City: %s" % (pa.city or ''),
+                "Country: %s" % (pa.country_id and pa.country_id.name_get()[0][1] or ''),
+                "State: %s" % (pa.state_id and pa.state_id.name_get()[0][1] or ''),
+                "Email: %s" % (pa.email or ''),
+                "Phone: %s" % (pa.phone or ''),
+                "Fax: %s" % (pa.fax or ''),
+                "Mobile: %s" % (pa.mobile or ''),
+                "Lead Category: %s" % (lead.categ_id and lead.categ_id.name or ''),
+                "Details: %s" % (lead.description or ''),
             ]
         return "\n".join(body + ['---'])
 

=== modified file 'crm_partner_assign/wizard/crm_forward_to_partner_view.xml'
--- crm_partner_assign/wizard/crm_forward_to_partner_view.xml	2011-01-14 00:11:01 +0000
+++ crm_partner_assign/wizard/crm_forward_to_partner_view.xml	2011-04-11 12:37:32 +0000
@@ -1,49 +1,63 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <openerp>
     <data>
+
+
         <record model="ir.ui.view" id="crm_lead_forward_to_partner_form">
             <field name="name">crm_lead_forward_to_partner</field>
             <field name="model">crm.lead.forward.to.partner</field>
             <field name="type">form</field>
-            <field name="inherit_id" ref="crm.crm_send_new_mail_view"/>
             <field name="arch" type="xml">
-                <field name="email_from" position="before">
+                <form string="Send Mail">
                     <separator string="Forward to Partner" colspan="4" />
                     <group col="4" colspan="6">
                         <field name="history" colspan="2" on_change="on_change_history(history, context)"/>
-                        <field name="name" colspan="2" />
-                        <group col="2" colspan="2" attrs="{ 'invisible' : [('name','!=','user')]}">
+                        <field name="send_to" colspan="2" />
+                        <group col="2" colspan="2" attrs="{ 'invisible' : [('send_to','!=','user')]}">
                             <field name="user_id"
-                                attrs="{ 'required' : [('name','=','user')]}"
+                                attrs="{ 'required' : [('send_to','=','user')]}"
                                 on_change="on_change_email(user_id)" />
                         </group>
-                        <group col="4" colspan="4" attrs="{'invisible' : [('name','!=','partner')]}">
-                            <field name="partner_id" attrs="{'required' : [('name','=','partner')]}" on_change="on_change_partner(partner_id)" colspan="2" />
+                        <group col="4" colspan="4" attrs="{'invisible' : [('send_to','!=','partner')]}">
+                            <field name="partner_id" attrs="{'required' : [('send_to','=','partner')]}" on_change="on_change_partner(partner_id)" colspan="2" />
                             <field name="address_id" string="Contact" on_change="on_change_address(address_id)" colspan="2" />
                         </group>
                     </group>
                     <separator string="" colspan="4" />
-                </field>
+                    <group col="6" colspan="4">
+                        <field name="smtp_server_id" widget="selection" colspan="4"/>
+                        <field name="email_from" colspan="4" required="1"/>
+                        <field name="email_to" colspan="4" required="1"/>
+                        <field name="email_cc" colspan="4"/>
+                        <field name="email_bcc" colspan="4"/>
+                        <field name="reply_to" colspan="4"/>
+                        <field name="subject" colspan="4" widget="char" size="512"/>
+                    </group>
+                    <separator string="" colspan="4"/>
+                    <notebook colspan="4">
+                        <page string="Body">
+                            <field name="body" colspan="4" nolabel="1"/>
+                        </page>
+                        <page string="Attachments">
+                            <label string="Add here all attachments of the current document you want to include in the Email." colspan="4"/>
+                            <field name="attachment_ids" colspan="4" nolabel="1"/>
+                        </page>
+                    </notebook>
+                    <group col="4" colspan="4">
+                        <label string="" colspan="1"/>
+                        <button icon="gtk-close" special="cancel" string="Close"/>
+                        <button icon="gtk-ok" name="send_mail" string="Send now" type="object"/>
+                    </group>
+                </form>
             </field>
         </record>
 
-        <record model="ir.ui.view" id="crm_lead_forward_to_partner_form1">
-            <field name="name">crm_lead_forward_to_partner1</field>
-            <field name="model">crm.lead.forward.to.partner</field>
-            <field name="type">form</field>
-            <field name="inherit_id" ref="crm.crm_send_new_mail_view"/>
-            <field name="arch" type="xml">
-                <button name="action_send" position="replace">
-                    <button name="action_forward" string="Forward" icon="gtk-go-forward" type="object" />
-                </button>
-            </field>
-        </record>
         <record model="ir.actions.act_window" id="crm_lead_forward_to_partner_act">
             <field name="name">Forward to Partner</field>
             <field name="res_model">crm.lead.forward.to.partner</field>
             <field name="view_type">form</field>
             <field name="view_mode">form</field>
-            <field name="view_id" ref="crm_lead_forward_to_partner_form1"/>
+            <field name="view_id" ref="crm_lead_forward_to_partner_form"/>
             <field name="target">new</field>
         </record>
 

=== modified file 'email_template/__init__.py'
--- email_template/__init__.py	2011-01-14 00:11:01 +0000
+++ email_template/__init__.py	2011-04-11 12:37:32 +0000
@@ -3,7 +3,7 @@
 #
 #    OpenERP, Open Source Management Solution
 #    Copyright (C) 2009 Sharoon Thomas
-#    Copyright (C) 2010-2010 OpenERP SA (<http://www.openerp.com>)
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
 #
 #    This program is free software: you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
@@ -19,9 +19,8 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
 #
 ##############################################################################
-
-import email_template_account
+import mako_template
 import email_template
-import email_template_mailbox
 import wizard
 
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'email_template/__openerp__.py'
--- email_template/__openerp__.py	2011-03-25 10:24:13 +0000
+++ email_template/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -26,7 +26,7 @@
     "author" : "Openlabs",
     "website" : "http://openerp.com";,
     "category" : "Tools",
-    "depends" : ['marketing', 'base_tools'],
+    "depends" : ['mail'],
     "description": """
 Email Template is extraction of Power Email basically just to send emails.
 ==========================================================================
@@ -38,14 +38,12 @@
 that will be related to the object. So if you choose to do marketing campaigns
 for leads, the action will be added to the right side panel of the Lead form.
     """,
-    "init_xml": ['email_template_scheduler_data.xml'],
+    "init_xml": [],
     "update_xml": [
-        'security/email_template_security.xml',
-        'email_template_workflow.xml',
-        'email_template_account_view.xml',
+        'wizard/email_template_preview_view.xml',
         'email_template_view.xml',
-        'email_template_mailbox_view.xml',
         'wizard/email_template_send_wizard_view.xml',
+        'wizard/email_compose_message_view.xml',
         'security/ir.model.access.csv'
     ],
     "installable": True,

=== modified file 'email_template/email_template.py'
--- email_template/email_template.py	2011-03-08 11:22:08 +0000
+++ email_template/email_template.py	2011-04-11 12:37:32 +0000
@@ -3,7 +3,7 @@
 #
 #    OpenERP, Open Source Management Solution
 #    Copyright (C) 2009 Sharoon Thomas
-#    Copyright (C) 2010-2010 OpenERP SA (<http://www.openerp.com>)
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
 #
 #    This program is free software: you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
@@ -20,133 +20,55 @@
 #
 ##############################################################################
 
+from osv import osv
+from osv import fields
 import base64
 import random
 import netsvc
 import logging
 import re
-
-TEMPLATE_ENGINES = []
-
-from osv import osv, fields
 from tools.translate import _
-
-try:
-    from mako.template import Template as MakoTemplate
-    TEMPLATE_ENGINES.append(('mako', 'Mako Templates'))
-except ImportError:
-    logging.getLogger('init').warning("module email_template: Mako templates not installed")
-
-try:
-    from django.template import Context, Template as DjangoTemplate
-    #Workaround for bug:
-    #http://code.google.com/p/django-tagging/issues/detail?id=110
-    from django.conf import settings
-    settings.configure()
-    #Workaround ends
-    TEMPLATE_ENGINES.append(('django', 'Django Template'))
-except ImportError:
-    logging.getLogger('init').warning("module email_template: Django templates not installed")
-
 import tools
 import pooler
-import logging
-
-def get_value(cursor, user, recid, message=None, template=None, context=None):
-    """
-    Evaluates an expression and returns its value
-    @param cursor: Database Cursor
-    @param user: ID of current user
-    @param recid: ID of the target record under evaluation
-    @param message: The expression to be evaluated
-    @param template: BrowseRecord object of the current template
-    @param context: OpenERP Context
-    @return: Computed message (unicode) or u""
-    """
-    pool = pooler.get_pool(cursor.dbname)
-    if message is None:
-        message = {}
-    #Returns the computed expression
-    if message:
-        try:
-            message = tools.ustr(message)
-            object = pool.get(template.model_int_name).browse(cursor, user, recid, context=context)
-            env = {
-                'user':pool.get('res.users').browse(cursor, user, user, context=context),
-                'db':cursor.dbname
-                   }
-            if template.template_language == 'mako':
-                templ = MakoTemplate(message, input_encoding='utf-8')
-                reply = MakoTemplate(message).render_unicode(object=object,
-                                                             peobject=object,
-                                                             env=env,
-                                                             format_exceptions=True)
-            elif template.template_language == 'django':
-                templ = DjangoTemplate(message)
-                env['object'] = object
-                env['peobject'] = object
-                reply = templ.render(Context(env))
-            return reply or False
-        except Exception:
-            logging.exception("can't render %r", message)
-            return u""
-    else:
-        return message
 
 class email_template(osv.osv):
     "Templates for sending Email"
-
+    _inherit = 'email.message.common'
     _name = "email.template"
     _description = 'Email Templates for Models'
 
-    def change_model(self, cursor, user, ids, object_name, context=None):
-        if object_name:
-            mod_name = self.pool.get('ir.model').read(
-                                              cursor,
-                                              user,
-                                              object_name,
-                                              ['model'], context)['model']
-        else:
-            mod_name = False
-        return {
-                'value':{'model_int_name':mod_name}
-                }
+    def get_template_value(self, cr, uid, message=None, model=None, record_id=None, context=None):
+        import mako_template
+        return mako_template.get_value(cr, uid, message=message, model=model, record_id=record_id, context=context)
+
+    def get_email_template(self, cr, uid, template_id=False, record_id=None, context=None):
+        "Return Template Object"
+        if context is None:
+            context = {}
+        if not template_id:
+            template_id = context.get('template_id', False)
+        if not template_id:
+            return False
+
+        template = self.browse(cr, uid, int(template_id), context)
+        lang = self.get_template_value(cr, uid, template.lang, template.model, record_id, context)
+        if lang:
+            # Use translated template if necessary
+            ctx = context.copy()
+            ctx['lang'] = lang
+            template = self.browse(cr, uid, template.id, ctx)
+        return template
+
+    def onchange_model_id(self, cr, uid, ids, model_id, context=None):
+        mod_name = False
+        if model_id:
+            mod_name = self.pool.get('ir.model').browse(cr, uid, model_id, context).model
+        return {'value':{'model':mod_name}}
 
     _columns = {
-        'name' : fields.char('Name', size=100, required=True),
-        'object_name':fields.many2one('ir.model', 'Resource'),
-        'model_int_name':fields.char('Model Internal Name', size=200,),
-        'from_account':fields.many2one(
-                   'email_template.account',
-                   string="Email Account",
-                   help="Emails will be sent from this approved account."),
-        'def_to':fields.char(
-                 'Recipient (To)',
-                 size=250,
-                 help="The Recipient of email. "
-                 "Placeholders can be used here. "
-                 "e.g. ${object.email_to}"),
-        'def_cc':fields.char(
-                 'CC',
-                 size=250,
-                 help="Carbon Copy address(es), comma-separated."
-                    " Placeholders can be used here. "
-                    "e.g. ${object.email_cc}"),
-        'def_bcc':fields.char(
-                  'BCC',
-                  size=250,
-                  help="Blind Carbon Copy address(es), comma-separated."
-                    " Placeholders can be used here. "
-                    "e.g. ${object.email_bcc}"),
-        'reply_to':fields.char('Reply-To',
-                    size=250,
-                    help="The address recipients should reply to,"
-                    " if different from the From address."
-                    " Placeholders can be used here. "
-                    "e.g. ${object.email_reply_to}"),
-        'message_id':fields.char('Message-ID',
-                    size=250,
-                    help="Specify the Message-ID SMTP header to use in outgoing emails. Please note that this overrides the Resource tracking option! Placeholders can be used here."),
+        'name': fields.char('Name', size=250),
+        'model_id':fields.many2one('ir.model', 'Resource'),
+        'model': fields.related('model_id', 'model', string='Model', type="char", size=128, store=True, readonly=True),
         'track_campaign_item':fields.boolean('Resource Tracking',
                                 help="Enable this is you wish to include a special \
 tracking marker in outgoing emails so you can identify replies and link \
@@ -158,25 +80,25 @@
                    help="The default language for the email."
                    " Placeholders can be used here. "
                    "eg. ${object.partner_id.lang}"),
-        'def_subject':fields.char(
+        'subject':fields.char(
                   'Subject',
                   size=200,
                   help="The subject of email."
                   " Placeholders can be used here.",
                   translate=True),
-        'def_body_text':fields.text(
-                    'Standard Body (Text)',
-                    help="The text version of the mail",
-                    translate=True),
-        'def_body_html':fields.text(
-                    'Body (Text-Web Client Only)',
-                    help="The text version of the mail",
-                    translate=True),
-        'use_sign':fields.boolean(
+#        'description':fields.text(
+#                    'Standard Body (Text)',
+#                    help="The text version of the mail",
+#                    translate=True),
+#        'body_html':fields.text(
+#                    'Body (Text-Web Client Only)',
+#                    help="The text version of the mail",
+#                    translate=True),
+        'user_signature':fields.boolean(
                   'Signature',
                   help="the signature from the User details"
                   " will be appended to the mail"),
-        'file_name':fields.char(
+        'report_name':fields.char(
                 'Report Filename',
                 size=200,
                 help="Name of the generated report file. Placeholders can be used in the filename. eg: 2009_SO003.pdf",
@@ -202,13 +124,6 @@
                    'Wizard Button',
                    help="Button in the side bar of the form view of this Resource that will invoke the Window Action",
                    readonly=True),
-        'allowed_groups':fields.many2many(
-                  'res.groups',
-                  'template_group_rel',
-                  'templ_id', 'group_id',
-                  string="Allowed User Groups",
-                  help="Only users from these groups will be"
-                  " allowed to send mails from this Template"),
         'model_object_field':fields.many2one(
                  'ir.model.fields',
                  string="Field",
@@ -216,46 +131,36 @@
                  "\nIf it is a relationship field you will be able to "
                  "choose the nested values in the box below\n(Note:If "
                  "there are no values make sure you have selected the"
-                 " correct model)",
-                 store=False),
+                 " correct model)"),
         'sub_object':fields.many2one(
                  'ir.model',
                  'Sub-model',
                  help='When a relation field is used this field'
-                 ' will show you the type of field you have selected',
-                 store=False),
+                 ' will show you the type of field you have selected'),
         'sub_model_object_field':fields.many2one(
                  'ir.model.fields',
                  'Sub Field',
                  help="When you choose relationship fields "
-                 "this field will specify the sub value you can use.",
-                 store=False),
+                 "this field will specify the sub value you can use."),
         'null_value':fields.char(
                  'Null Value',
                  help="This Value is used if the field is empty",
-                 size=50, store=False),
+                 size=50),
         'copyvalue':fields.char(
                 'Expression',
                 size=100,
                 help="Copy and paste the value in the "
-                "location you want to use a system value.",
-                store=False),
-        'table_html':fields.text(
-             'HTML code',
-             help="Copy this html code to your HTML message"
-             " body for displaying the info in your mail.",
-             store=False),
-        #Template language(engine eg.Mako) specifics
-        'template_language':fields.selection(
-                TEMPLATE_ENGINES,
-                'Templating Language',
-                required=True
-                )
-    }
-
-    _defaults = {
-        'template_language' : lambda *a:'mako',
-
+                "location you want to use a system value."),
+        'auto_delete': fields.boolean('Auto Delete', help="Permanently delete emails after sending"),
+        'model': fields.related('model_id','model', type='char', size=128, string='Object', help="Placeholders can be used here."),
+        'email_from': fields.char('From', size=128, help="Email From. Placeholders can be used here."),
+        'email_to': fields.char('To', size=256, help="Email Recipients. Placeholders can be used here."),
+        'email_cc': fields.char('Cc', size=256, help="Carbon Copy Email Recipients. Placeholders can be used here."),
+        'email_bcc': fields.char('Bcc', size=256, help="Blind Carbon Copy Email Recipients. Placeholders can be used here."),
+        'message_id': fields.char('Message Id', size=1024, select=1, help="Message Id on Email. Placeholders can be used here."),
+        'reply_to':fields.char('Reply-To', size=250, help="Placeholders can be used here."),
+        'body': fields.text('Description', translate=True, help="Placeholders can be used here."),
+        'body_html': fields.text('HTML', help="Contains HTML version of email. Placeholders can be used here."),
     }
 
     _sql_constraints = [
@@ -266,31 +171,35 @@
         vals = {}
         if context is None:
             context = {}
-        template_obj = self.browse(cr, uid, ids, context=context)[0]
-        src_obj = template_obj.object_name.model
-        vals['ref_ir_act_window'] = self.pool.get('ir.actions.act_window').create(cr, uid, {
-             'name': template_obj.name,
-             'type': 'ir.actions.act_window',
-             'res_model': 'email_template.send.wizard',
-             'src_model': src_obj,
-             'view_type': 'form',
-             'context': "{'src_model':'%s','template_id':'%d','src_rec_id':active_id,'src_rec_ids':active_ids}" % (src_obj, template_obj.id),
-             'view_mode':'form,tree',
-             'view_id': self.pool.get('ir.ui.view').search(cr, uid, [('name', '=', 'email_template.send.wizard.form')], context=context)[0],
-             'target': 'new',
-             'auto_refresh':1
-        }, context)
-        vals['ref_ir_value'] = self.pool.get('ir.values').create(cr, uid, {
-             'name': _('Send Mail (%s)') % template_obj.name,
-             'model': src_obj,
-             'key2': 'client_action_multi',
-             'value': "ir.actions.act_window," + str(vals['ref_ir_act_window']),
-             'object': True,
-         }, context)
+        action_obj = self.pool.get('ir.actions.act_window')
+        data_obj = self.pool.get('ir.model.data')
+        for template in self.browse(cr, uid, ids, context=context):
+            src_obj = template.model_id.model
+            model_data_id = data_obj._get_id(cr, uid, 'mail', 'email_compose_message_wizard_form')
+            res_id = data_obj.browse(cr, uid, model_data_id, context=context).res_id
+            vals['ref_ir_act_window'] = action_obj.create(cr, uid, {
+                 'name': template.name,
+                 'type': 'ir.actions.act_window',
+                 'res_model': 'email.compose.message',
+                 'src_model': src_obj,
+                 'view_type': 'form',
+                 'context': "{'email_model':'%s', 'email_res_id': active_id,'template_id':'%d','src_rec_id':active_id,'src_rec_ids':active_ids}" % (src_obj, template.id),
+                 'view_mode':'form,tree',
+                 'view_id': res_id,
+                 'target': 'new',
+                 'auto_refresh':1
+            }, context)
+            vals['ref_ir_value'] = self.pool.get('ir.values').create(cr, uid, {
+                 'name': _('Send Mail (%s)') % template.name,
+                 'model': src_obj,
+                 'key2': 'client_action_multi',
+                 'value': "ir.actions.act_window," + str(vals['ref_ir_act_window']),
+                 'object': True,
+             }, context)
         self.write(cr, uid, ids, {
-            'ref_ir_act_window': vals['ref_ir_act_window'],
-            'ref_ir_value': vals['ref_ir_value'],
-        }, context)
+                    'ref_ir_act_window': vals.get('ref_ir_act_window',False),
+                    'ref_ir_value': vals.get('ref_ir_value',False),
+                }, context)
         return True
 
     def unlink_action(self, cr, uid, ids, context=None):
@@ -303,10 +212,6 @@
             except:
                 raise osv.except_osv(_("Warning"), _("Deletion of Record failed"))
 
-    def delete_action(self, cr, uid, ids, context=None):
-        self.unlink_action(cr, uid, ids, context=context)
-        return True
-
     def unlink(self, cr, uid, ids, context=None):
         self.unlink_action(cr, uid, ids, context=context)
         return super(email_template, self).unlink(cr, uid, ids, context=context)
@@ -323,500 +228,219 @@
         default.update({'name':new_name})
         return super(email_template, self).copy(cr, uid, id, default, context)
 
-    def build_expression(self, field_name, sub_field_name, null_value, template_language='mako'):
+    def build_expression(self, field_name, sub_field_name, null_value):
         """
         Returns a template expression based on data provided
         @param field_name: field name
         @param sub_field_name: sub field name (M2O)
         @param null_value: default value if the target value is empty
-        @param template_language: name of template engine
         @return: computed expression
         """
-
         expression = ''
-        if template_language == 'mako':
-            if field_name:
-                expression = "${object." + field_name
-                if sub_field_name:
-                    expression += "." + sub_field_name
-                if null_value:
-                    expression += " or '''%s'''" % null_value
-                expression += "}"
-        elif template_language == 'django':
-            if field_name:
-                expression = "{{object." + field_name
-                if sub_field_name:
-                    expression += "." + sub_field_name
-                if null_value:
-                    expression += "|default: '''%s'''" % null_value
-                expression += "}}"
+        if field_name:
+            expression = "${object." + field_name
+            if sub_field_name:
+                expression += "." + sub_field_name
+            if null_value:
+                expression += " or '''%s'''" % null_value
+            expression += "}"
         return expression
-
-    def onchange_model_object_field(self, cr, uid, ids, model_object_field, template_language, context=None):
-        if not model_object_field:
-            return {}
-        result = {}
-        field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
-        #Check if field is relational
-        if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
-            res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
-            if res_ids:
-                result['sub_object'] = res_ids[0]
-                result['copyvalue'] = self.build_expression(False,
-                                                      False,
-                                                      False,
-                                                      template_language)
-                result['sub_model_object_field'] = False
-                result['null_value'] = False
-        else:
-            #Its a simple field... just compute placeholder
-            result['sub_object'] = False
-            result['copyvalue'] = self.build_expression(field_obj.name,
-                                                  False,
-                                                  False,
-                                                  template_language
-                                                  )
-            result['sub_model_object_field'] = False
-            result['null_value'] = False
-        return {'value':result}
-
-    def onchange_sub_model_object_field(self, cr, uid, ids, model_object_field, sub_model_object_field, template_language, context=None):
-        if not model_object_field or not sub_model_object_field:
-            return {}
-        result = {}
-        field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
-        if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
-            res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
-            sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context)
-            if res_ids:
-                result['sub_object'] = res_ids[0]
-                result['copyvalue'] = self.build_expression(field_obj.name,
-                                                      sub_field_obj.name,
-                                                      False,
-                                                      template_language
-                                                      )
-                result['sub_model_object_field'] = sub_model_object_field
-                result['null_value'] = False
-        else:
-            #Its a simple field... just compute placeholder
-            result['sub_object'] = False
-            result['copyvalue'] = self.build_expression(field_obj.name,
-                                                  False,
-                                                  False,
-                                                  template_language
-                                                  )
-            result['sub_model_object_field'] = False
-            result['null_value'] = False
-        return {'value':result}
-
-    def onchange_null_value(self, cr, uid, ids, model_object_field, sub_model_object_field, null_value, template_language, context=None):
-        if not model_object_field and not null_value:
-            return {}
-        result = {}
-        field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
-        if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
-            res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
-            sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context)
-            if res_ids:
-                result['sub_object'] = res_ids[0]
-                result['copyvalue'] = self.build_expression(field_obj.name,
-                                                      sub_field_obj.name,
-                                                      null_value,
-                                                      template_language
-                                                      )
-                result['sub_model_object_field'] = sub_model_object_field
-                result['null_value'] = null_value
-        else:
-            #Its a simple field... just compute placeholder
-            result['sub_object'] = False
-            result['copyvalue'] = self.build_expression(field_obj.name,
-                                                  False,
-                                                  null_value,
-                                                  template_language
-                                                  )
-            result['sub_model_object_field'] = False
-            result['null_value'] = null_value
-        return {'value':result}
-
-    def _add_attachment(self, cursor, user, mailbox_id, name, data, filename, context=None):
-        """
-        Add an attachment to a given mailbox entry.
-
-        :param data: base64 encoded attachment data to store
-        """
-        attachment_obj = self.pool.get('ir.attachment')
-        attachment_data = {
-            'name':  (name or '') + _(' (Email Attachment)'),
-            'datas': data,
-            'datas_fname': filename,
-            'description': name or _('No Description'),
-            'res_model':'email_template.mailbox',
-            'res_id': mailbox_id,
-        }
-        attachment_id = attachment_obj.create(cursor,
-                                              user,
-                                              attachment_data,
-                                              context)
-        if attachment_id:
-            self.pool.get('email_template.mailbox').write(
-                              cursor,
-                              user,
-                              mailbox_id,
-                              {
-                               'attachments_ids':[(4, attachment_id)],
-                               'mail_type':'multipart/mixed'
-                              },
-                              context)
-
-    def generate_attach_reports(self,
-                                 cursor,
-                                 user,
-                                 template,
-                                 record_id,
-                                 mail,
-                                 context=None):
-        """
-        Generate report to be attached and attach it
-        to the email, and add any directly attached files as well.
-
-        @param cursor: Database Cursor
-        @param user: ID of User
-        @param template: Browse record of
-                         template
-        @param record_id: ID of the target model
-                          for which this mail has
-                          to be generated
-        @param mail: Browse record of email object
-        @return: True
-        """
-        if template.report_template:
-            reportname = 'report.' + \
-                self.pool.get('ir.actions.report.xml').read(
-                                             cursor,
-                                             user,
-                                             template.report_template.id,
-                                             ['report_name'],
-                                             context)['report_name']
-            service = netsvc.LocalService(reportname)
-            data = {}
-            data['model'] = template.model_int_name
-            (result, format) = service.create(cursor,
-                                              user,
-                                              [record_id],
-                                              data,
-                                              context)
-            fname = tools.ustr(get_value(cursor, user, record_id,
-                                         template.file_name, template, context)
-                               or 'Report')
-            ext = '.' + format
-            if not fname.endswith(ext):
-                fname += ext
-            self._add_attachment(cursor, user, mail.id, mail.subject, base64.b64encode(result), fname, context)
-
-        if template.attachment_ids:
-            for attachment in template.attachment_ids:
-                self._add_attachment(cursor, user, mail.id, attachment.name, attachment.datas, attachment.datas_fname, context)
-
-        return True
-
-    def _generate_mailbox_item_from_template(self,
-                                      cursor,
-                                      user,
-                                      template,
-                                      record_id,
-                                      context=None):
+#
+#    def onchange_model_object_field(self, cr, uid, ids, model_object_field, context=None):
+#        if not model_object_field:
+#            return {}
+#        result = {}
+#        field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
+#        #Check if field is relational
+#        if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
+#            res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
+#            if res_ids:
+#                result['sub_object'] = res_ids[0]
+#                result['copyvalue'] = self.build_expression(False, False, False)
+#                result['sub_model_object_field'] = False
+#                result['null_value'] = False
+#        else:
+#            #Its a simple field... just compute placeholder
+#            result['sub_object'] = False
+#            result['copyvalue'] = self.build_expression(field_obj.name, False, False)
+#            result['sub_model_object_field'] = False
+#            result['null_value'] = False
+#        return {'value':result}
+#
+#    def onchange_sub_model_object_field(self, cr, uid, ids, model_object_field, sub_model_object_field, context=None):
+#        if not model_object_field or not sub_model_object_field:
+#            return {}
+#        result = {}
+#        field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
+#        if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
+#            res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
+#            sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context)
+#            if res_ids:
+#                result['sub_object'] = res_ids[0]
+#                result['copyvalue'] = self.build_expression(field_obj.name, sub_field_obj.name, False)
+#                result['sub_model_object_field'] = sub_model_object_field
+#                result['null_value'] = False
+#        else:
+#            #Its a simple field... just compute placeholder
+#            result['sub_object'] = False
+#            result['copyvalue'] = self.build_expression(field_obj.name, False, False)
+#            result['sub_model_object_field'] = False
+#            result['null_value'] = False
+#        return {'value':result}
+#
+#
+#    def onchange_null_value(self, cr, uid, ids, model_object_field, sub_model_object_field, null_value, template_language, context=None):
+#        if not model_object_field and not null_value:
+#            return {}
+#        result = {}
+#        field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
+#        if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
+#            res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
+#            sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context)
+#            if res_ids:
+#                result['sub_object'] = res_ids[0]
+#                result['copyvalue'] = self.build_expression(field_obj.name,
+#                                                      sub_field_obj.name,
+#                                                      null_value,
+#                                                      template_language
+#                                                      )
+#                result['sub_model_object_field'] = sub_model_object_field
+#                result['null_value'] = null_value
+#        else:
+#            #Its a simple field... just compute placeholder
+#            result['sub_object'] = False
+#            result['copyvalue'] = self.build_expression(field_obj.name,
+#                                                  False,
+#                                                  null_value,
+#                                                  template_language
+#                                                  )
+#            result['sub_model_object_field'] = False
+#            result['null_value'] = null_value
+#        return {'value':result}
+
+    def onchange_sub_model_object_value_field(self, cr, uid, ids, model_object_field, sub_model_object_field=False, null_value=None, context=None):
+        result = {
+            'sub_object': False,
+            'copyvalue': False,
+            'sub_model_object_field': False,
+            'null_value': False
+            }
+        if model_object_field:
+            fields_obj = self.pool.get('ir.model.fields')
+            field_value = fields_obj.browse(cr, uid, model_object_field, context)
+            if field_value.ttype in ['many2one', 'one2many', 'many2many']:
+                res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_value.relation)], context=context)
+                sub_field_value = False
+                if sub_model_object_field:
+                    sub_field_value = fields_obj.browse(cr, uid, sub_model_object_field, context)
+                if res_ids:
+                    result.update({
+                        'sub_object': res_ids[0],
+                        'copyvalue': self.build_expression(field_value.name, sub_field_value and sub_field_value.name or False, null_value or False),
+                        'sub_model_object_field': sub_model_object_field or False,
+                        'null_value': null_value or False
+                        })
+            else:
+                result.update({
+                        'copyvalue': self.build_expression(field_value.name, False, null_value or False),
+                        'null_value': null_value or False
+                        })
+        return {'value':result}
+
+
+    def generate_email(self, cr, uid, template_id, record_id, context=None):
         """
         Generates an email from the template for
         record record_id of target object
-
-        @param cursor: Database Cursor
-        @param user: ID of User
-        @param template: Browse record of
-                         template
-        @param record_id: ID of the target model
-                          for which this mail has
-                          to be generated
-        @return: ID of created object
         """
         if context is None:
             context = {}
-        #If account to send from is in context select it, else use enforced account
-        if 'account_id' in context.keys():
-            from_account = self.pool.get('email_template.account').read(
-                                                    cursor,
-                                                    user,
-                                                    context.get('account_id'),
-                                                    ['name', 'email_id'],
-                                                    context
-                                                    )
+        smtp_pool = self.pool.get('ir.mail_server')
+        email_message_pool = self.pool.get('email.message')
+        report_xml_pool = self.pool.get('ir.actions.report.xml')
+        template = self.get_email_template(cr, uid, template_id, record_id, context)
+        smtp_server_id = context.get('smtp_server_id', False)
+        if not smtp_server_id and template.smtp_server_id:
+            smtp_server_id = template.smtp_server_id.id
         else:
-            from_account = {
-                            'id':template.from_account.id,
-                            'name':template.from_account.name,
-                            'email_id':template.from_account.email_id
-                            }
-        lang = get_value(cursor,
-                         user,
-                         record_id,
-                         template.lang,
-                         template,
-                         context)
-        if lang:
-            ctx = context.copy()
-            ctx.update({'lang':lang})
-            template = self.browse(cursor, user, template.id, context=ctx)
+            smtp_ids = smtp_pool.search(cr, uid, [])
+            smtp_server_id = smtp_ids and smtp_ids[0]
+        smtp_server = smtp_pool.browse(cr, uid, smtp_server_id, context=context)
+        # determine name of sender, either it is specified in email_id
 
-        # determine name of sender, either it is specified in email_id or we
-        # use the account name
-        email_id = from_account['email_id'].strip()
+        email_id = smtp_server.email_id.strip()
         email_from = re.findall(r'([^ ,<@]+@[^> ,]+)', email_id)[0]
         if email_from != email_id:
-            # we should keep it all, name is probably specified in the address
-            email_from = from_account['email_id']
+            email_from = smtp_server.email_id
         else:
-            email_from = tools.ustr(from_account['name']) + "<" + tools.ustr(email_id) + ">"
+            email_from = tools.ustr(smtp_server.name) + "<" + tools.ustr(email_id) + ">"
 
-        # FIXME: should do this in a loop and rename template fields to the corresponding
-        # mailbox fields. (makes no sense to have different names I think.
-        mailbox_values = {
+        model = template.model_id.model
+        values = {
             'email_from': email_from,
-            'email_to':get_value(cursor,
-                               user,
-                               record_id,
-                               template.def_to,
-                               template,
-                               context),
-            'email_cc':get_value(cursor,
-                               user,
-                               record_id,
-                               template.def_cc,
-                               template,
-                               context),
-            'email_bcc':get_value(cursor,
-                                user,
-                                record_id,
-                                template.def_bcc,
-                                template,
-                                context),
-            'reply_to':get_value(cursor,
-                                user,
-                                record_id,
-                                template.reply_to,
-                                template,
-                                context),
-            'subject':get_value(cursor,
-                                    user,
-                                    record_id,
-                                    template.def_subject,
-                                    template,
-                                    context),
-            'body_text':get_value(cursor,
-                                      user,
-                                      record_id,
-                                      template.def_body_text,
-                                      template,
-                                      context),
-            'body_html':get_value(cursor,
-                                      user,
-                                      record_id,
-                                      template.def_body_html,
-                                      template,
-                                      context),
-            'account_id' :from_account['id'],
-            #This is a mandatory field when automatic emails are sent
-            'state':'na',
-            'folder':'drafts',
-            'mail_type':'multipart/alternative',
+            'email_to': self.get_template_value(cr, uid, template.email_to, model, record_id, context),
+            'email_cc': self.get_template_value(cr, uid, template.email_cc, model, record_id, context),
+            'email_bcc': self.get_template_value(cr, uid, template.email_bcc, model, record_id, context),
+            'reply_to': self.get_template_value(cr, uid, template.reply_to, model, record_id, context),
+            'subject': self.get_template_value(cr, uid, template.subject, model, record_id, context),
+            'body': self.get_template_value(cr, uid, template.description, model, record_id, context),
+            'auto_delete': self.get_template_value(cr, uid, template.auto_delete, model, record_id, context),
+            #'body_html': self.get_template_value(cr, uid, template.body_html, model, record_id, context),
         }
 
-        if template['message_id']:
+        if template.message_id:
             # use provided message_id with placeholders
-            mailbox_values.update({'message_id': get_value(cursor, user, record_id, template['message_id'], template, context)})
+            values.update({'message_id': self.get_template_value(cr, uid, template.message_id, model, record_id, context)})
 
         elif template['track_campaign_item']:
             # get appropriate message-id
-            mailbox_values.update({'message_id': tools.misc.generate_tracking_message_id(record_id)})
+            values.update({'message_id': tools.generate_tracking_message_id(record_id)})
 
-        if not mailbox_values['account_id']:
-            raise Exception("Unable to send the mail. No account linked to the template.")
         #Use signatures if allowed
-        if template.use_sign:
-            sign = self.pool.get('res.users').read(cursor,
-                                                   user,
-                                                   user,
-                                                   ['signature'],
-                                                   context)['signature']
-            if mailbox_values['body_text']:
-                mailbox_values['body_text'] += sign
-            if mailbox_values['body_html']:
-                mailbox_values['body_html'] += sign
-        mailbox_id = self.pool.get('email_template.mailbox').create(
-                                                             cursor,
-                                                             user,
-                                                             mailbox_values,
-                                                             context)
-
-        return mailbox_id
-
-
-    def generate_mail(self,
-                      cursor,
-                      user,
-                      template_id,
-                      record_ids,
-                      context=None):
-        if context is None:
-            context = {}
-        template = self.browse(cursor, user, template_id, context=context)
-        if not template:
-            raise Exception("The requested template could not be loaded")
-        result = True
-        mailbox_obj = self.pool.get('email_template.mailbox')
-        for record_id in record_ids:
-            mailbox_id = self._generate_mailbox_item_from_template(
-                                                                cursor,
-                                                                user,
-                                                                template,
-                                                                record_id,
-                                                                context)
-            mail = mailbox_obj.browse(
-                                        cursor,
-                                        user,
-                                        mailbox_id,
-                                        context=context
-                                              )
-            if template.report_template or template.attachment_ids:
-                self.generate_attach_reports(
-                                              cursor,
-                                              user,
-                                              template,
-                                              record_id,
-                                              mail,
-                                              context
-                                              )
-
-            self.pool.get('email_template.mailbox').write(
-                                                cursor,
-                                                user,
-                                                mailbox_id,
-                                                {'folder':'outbox'},
-                                                context=context
-            )
-            # TODO : manage return value of all the records
-            result = self.pool.get('email_template.mailbox').send_this_mail(cursor, user, [mailbox_id], context)
-        return result
+        if template.user_signature:
+            sign = self.pool.get('res.users').read(cr, uid, uid, ['signature'], context)['signature']
+            if values['description']:
+                values['description'] += '\n\n' + sign
+            #if values['body_html']:
+            #    values['body_html'] += sign
+
+        attachment = []
+
+        # Add report as a Document
+        if template.report_template:
+            report_name = template.report_name
+            reportname = 'report.' + report_xml_pool.browse(cr, uid, template.report_template.id, context).report_name
+            data = {}
+            data['model'] = template.model
+
+            # Ensure report is rendered using template's language
+            ctx = context.copy()
+            if template.lang:
+                ctx['lang'] = self.get_template_value(cr, uid, template.lang, template.model, record_id, context)
+            service = netsvc.LocalService(reportname)
+            (result, format) = service.create(cr, uid, [record_id], data, ctx)
+            result = base64.b64encode(result)
+            if not report_name:
+                report_name = reportname
+            report_name = report_name + "." + format
+            attachment.append((report_name, result))
+
+
+        # Add document attachments
+        for attach in template.attachment_ids:
+            #attach = attahcment_obj.browse(cr, uid, attachment_id, context)
+            attachment.append((attach.datas_fname, attach.datas))
+
+        #Send emails
+        context.update({'notemplate':True})
+        email_id = email_message_pool.schedule_with_attach(cr, uid, values.get('email_from'), values.get('email_to'), values.get('name'),
+                    values.get('description'), model=model, email_cc=values.get('email_cc'), email_bcc=values.get('email_bcc'),
+                    reply_to=values.get('reply_to'), attach=attachment, message_id=values.get('message_id'), openobject_id=record_id,
+                    debug=True, subtype='plain', x_headers={}, priority='3', smtp_server_id=smtp_server.id, auto_delete=values.get('auto_delete'), context=context)
+        email_message_pool.write(cr, uid, email_id, {'template_id': context.get('template_id',template.id)})
+        return email_id
 
 email_template()
 
 
-## FIXME: this class duplicates a lot of features of the email template send wizard,
-##        one of the 2 should inherit from the other!
-
-class email_template_preview(osv.osv_memory):
-    _name = "email_template.preview"
-    _description = "Email Template Preview"
-
-    def _get_model_recs(self, cr, uid, context=None):
-        if context is None:
-            context = {}
-            #Fills up the selection box which allows records from the selected object to be displayed
-        self.context = context
-        if 'template_id' in context:
-            ref_obj_id = self.pool.get('email.template').read(cr, uid, context['template_id'], ['object_name'], context)
-            ref_obj_name = self.pool.get('ir.model').read(cr, uid, ref_obj_id['object_name'][0], ['model'], context)['model']
-            model_obj = self.pool.get(ref_obj_name)
-            ref_obj_ids = model_obj.search(cr, uid, [], 0, 20, 'id', context=context)
-            if not ref_obj_ids:
-                ref_obj_ids = []
-
-            # also add the default one if requested, otherwise it won't be available for selection:
-            default_id = context.get('default_rel_model_ref')
-            if default_id and default_id not in ref_obj_ids:
-                ref_obj_ids.insert(0, default_id)
-            return model_obj.name_get(cr, uid, ref_obj_ids, context)
-        return []
-
-    def default_get(self, cr, uid, fields, context=None):
-        if context is None:
-            context = {}
-        result = super(email_template_preview, self).default_get(cr, uid, fields, context=context)
-        if (not fields or 'rel_model_ref' in fields) and 'template_id' in context \
-           and not result.get('rel_model_ref'):
-            selectables = self._get_model_recs(cr, uid, context=context)
-            result['rel_model_ref'] = selectables and selectables[0][0] or False
-        return result
-
-    def _default_model(self, cursor, user, context=None):
-        """
-        Returns the default value for model field
-        @param cursor: Database Cursor
-        @param user: ID of current user
-        @param context: OpenERP Context
-        """
-        return self.pool.get('email.template').read(
-                                                   cursor,
-                                                   user,
-                                                   context['template_id'],
-                                                   ['object_name'],
-                                                   context).get('object_name', False)
-
-    _columns = {
-        'ref_template':fields.many2one(
-                                       'email.template',
-                                       'Template', readonly=True),
-        'rel_model':fields.many2one('ir.model', 'Model', readonly=True),
-        'rel_model_ref':fields.selection(_get_model_recs, 'Referred Document'),
-        'to':fields.char('To', size=250, readonly=True),
-        'cc':fields.char('CC', size=250, readonly=True),
-        'bcc':fields.char('BCC', size=250, readonly=True),
-        'reply_to':fields.char('Reply-To',
-                    size=250,
-                    help="The address recipients should reply to,"
-                         " if different from the From address."
-                         " Placeholders can be used here."),
-        'message_id':fields.char('Message-ID',
-                    size=250,
-                    help="The Message-ID header value, if you need to"
-                         "specify it, for example to automatically recognize the replies later."
-                        " Placeholders can be used here."),
-        'subject':fields.char('Subject', size=200, readonly=True),
-        'body_text':fields.text('Body', readonly=True),
-        'body_html':fields.text('Body', readonly=True),
-        'report':fields.char('Report Name', size=100, readonly=True),
-    }
-    _defaults = {
-        'ref_template': lambda self, cr, uid, ctx:ctx['template_id'] or False,
-        'rel_model': _default_model,
-    }
-    def on_change_ref(self, cr, uid, ids, rel_model_ref, context=None):
-        if context is None:
-            context = {}
-        if not rel_model_ref:
-            return {}
-        vals = {}
-        if context == {}:
-            context = self.context
-        template = self.pool.get('email.template').browse(cr, uid, context['template_id'], context)
-        #Search translated template
-        lang = get_value(cr, uid, rel_model_ref, template.lang, template, context)
-        if lang:
-            ctx = context.copy()
-            ctx.update({'lang':lang})
-            template = self.pool.get('email.template').browse(cr, uid, context['template_id'], ctx)
-        vals['to'] = get_value(cr, uid, rel_model_ref, template.def_to, template, context)
-        vals['cc'] = get_value(cr, uid, rel_model_ref, template.def_cc, template, context)
-        vals['bcc'] = get_value(cr, uid, rel_model_ref, template.def_bcc, template, context)
-        vals['reply_to'] = get_value(cr, uid, rel_model_ref, template.reply_to, template, context)
-        if template.message_id:
-            vals['message_id'] = get_value(cr, uid, rel_model_ref, template.message_id, template, context)
-        elif template.track_campaign_item:
-            vals['message_id'] = tools.misc.generate_tracking_message_id(rel_model_ref)
-        vals['subject'] = get_value(cr, uid, rel_model_ref, template.def_subject, template, context)
-        vals['body_text'] = get_value(cr, uid, rel_model_ref, template.def_body_text, template, context)
-        vals['body_html'] = get_value(cr, uid, rel_model_ref, template.def_body_html, template, context)
-        vals['report'] = get_value(cr, uid, rel_model_ref, template.file_name, template, context)
-        return {'value':vals}
-
-email_template_preview()
-
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== removed file 'email_template/email_template_account.py'
--- email_template/email_template_account.py	2011-01-14 00:11:01 +0000
+++ email_template/email_template_account.py	1970-01-01 00:00:00 +0000
@@ -1,470 +0,0 @@
-# -*- coding: utf-8 -*-
-##############################################################################
-#
-#    OpenERP, Open Source Management Solution
-#    Copyright (C) 2009 Sharoon Thomas
-#    Copyright (C) 2004-2010 OpenERP SA (<http://www.openerp.com>)
-#
-#    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
-#
-#    This program is distributed in the hope that it will be useful,
-#    but WITHOUT ANY WARRANTY; without even the implied warranty of
-#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU General Public License for more details.
-#
-#    You should have received a copy of the GNU General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>
-#
-##############################################################################
-
-from osv import osv, fields
-import re
-import smtplib
-import base64
-from email import Encoders
-from email.mime.base import MIMEBase
-from email.mime.multipart import MIMEMultipart
-from email.mime.text import MIMEText
-from email.header import decode_header, Header
-from email.utils import formatdate
-import netsvc
-import datetime
-from tools.translate import _
-import tools
-import logging
-
-EMAIL_PATTERN = re.compile(r'([^()\[\] ,<:\\>@";]+@[^()\[\] ,<:\\>@";]+)') # See RFC822
-def extract_emails(emails_str):
-    """
-    Returns a list of email addresses recognized in a string, ignoring the rest of the string.
-    extract_emails('a@xxxxx,c@bcom, "John Doe" <d@xxxxx> , e@xxxxx') -> ['a@xxxxx','c@bcom', 'd@xxxxx', 'e@xxxxx']"
-    """
-    return EMAIL_PATTERN.findall(emails_str)
-
-
-def extract_emails_from_dict(addresses={}):
-    """
-    Extracts email addresses from a dictionary with comma-separated address string values, handling
-    separately the To, CC, BCC and Reply-To addresses.
-
-    :param addresses: a dictionary of addresses in the form {'To': 'a@xxxxx,c@bcom; d@xxxxx;e@xxxxx' , 'CC': 'e@xxxxx;f@xxxxx', ... }
-    :return: a dictionary with a list of separate addresses for each header (To, CC, BCC), with an additional key 'all-recipients'
-             containing all addresses for the 'To', 'CC', 'BCC' entries.
-    """
-    result = {'all-recipients':[]}
-    keys = ['To', 'CC', 'BCC', 'Reply-To']
-    for each in keys:
-        emails = extract_emails(addresses.get(each, u''))
-        while u'' in emails:
-            emails.remove(u'')
-        result[each] = emails
-        if each != 'Reply-To':
-            result['all-recipients'].extend(emails)
-    return result
-
-class email_template_account(osv.osv):
-    """
-    Object to store email account settings
-    """
-    _name = "email_template.account"
-    _known_content_types = ['multipart/mixed',
-                            'multipart/alternative',
-                            'multipart/related',
-                            'text/plain',
-                            'text/html'
-                            ]
-    _columns = {
-        'name': fields.char('Description',
-                        size=64, required=True,
-                        readonly=True, select=True,
-                        help="The description is used as the Sender name along with the provided From Email, \
-unless it is already specified in the From Email, e.g: John Doe <john@xxxxxxx>", 
-                        states={'draft':[('readonly', False)]}),
-        'auto_delete': fields.boolean('Auto Delete', size=64, readonly=True, 
-                                      help="Permanently delete emails after sending", 
-                                      states={'draft':[('readonly', False)]}),
-        'user':fields.many2one('res.users',
-                        'Related User', required=True,
-                        readonly=True, states={'draft':[('readonly', False)]}),
-        'email_id': fields.char('From Email',
-                        size=120, required=True,
-                        readonly=True, states={'draft':[('readonly', False)]} ,
-                        help="eg: 'john@xxxxxxx' or 'John Doe <john@xxxxxxx>'"),
-        'smtpserver': fields.char('Server',
-                        size=120, required=True,
-                        readonly=True, states={'draft':[('readonly', False)]},
-                        help="Enter name of outgoing server, eg: smtp.yourdomain.com"),
-        'smtpport': fields.integer('SMTP Port',
-                        size=64, required=True,
-                        readonly=True, states={'draft':[('readonly', False)]},
-                        help="Enter port number, eg: 25 or 587"),
-        'smtpuname': fields.char('User Name',
-                        size=120, required=False,
-                        readonly=True, states={'draft':[('readonly', False)]},
-                        help="Specify the username if your SMTP server requires authentication, "
-                        "otherwise leave it empty."),
-        'smtppass': fields.char('Password',
-                        size=120, invisible=True,
-                        required=False, readonly=True,
-                        states={'draft':[('readonly', False)]}),
-        'smtptls':fields.boolean('TLS',
-                        states={'draft':[('readonly', False)]}, readonly=True),
-                                
-        'smtpssl':fields.boolean('SSL/TLS (only in python 2.6)',
-                        states={'draft':[('readonly', False)]}, readonly=True),
-        'send_pref':fields.selection([
-                                      ('html', 'HTML, otherwise Text'),
-                                      ('text', 'Text, otherwise HTML'),
-                                      ('alternative', 'Both HTML & Text (Alternative)'),
-                                      ('mixed', 'Both HTML & Text (Mixed)')
-                                      ], 'Mail Format', required=True),
-        'company':fields.selection([
-                        ('yes', 'Yes'),
-                        ('no', 'No')
-                        ], 'Corporate',
-                        readonly=True,
-                        help="Select if this mail account does not belong " \
-                        "to specific user but to the organization as a whole. " \
-                        "eg: info@xxxxxxxxxxxxxxxxx",
-                        required=True, states={
-                                           'draft':[('readonly', False)]
-                                           }),
-
-        'state':fields.selection([
-                                  ('draft', 'Initiated'),
-                                  ('suspended', 'Suspended'),
-                                  ('approved', 'Approved')
-                                  ],
-                        'State', required=True, readonly=True),
-    }
-
-    _defaults = {
-         'name':lambda self, cursor, user, context:self.pool.get(
-                                                'res.users'
-                                                ).read(
-                                                        cursor,
-                                                        user,
-                                                        user,
-                                                        ['name'],
-                                                        context
-                                                        )['name'],
-         'state':lambda * a:'draft',
-         'smtpport':lambda *a:25,
-         'smtpserver':lambda *a:'localhost',
-         'company':lambda *a:'yes',
-         'user':lambda self, cursor, user, context:user,
-         'send_pref':lambda *a: 'html',
-         'smtptls':lambda *a:True,
-     }
-    
-    _sql_constraints = [
-        (
-         'email_uniq',
-         'unique (email_id)',
-         'Another setting already exists with this email ID !')
-    ]
-
-    def name_get(self, cr, uid, ids, context=None):
-        return [(a["id"], "%s (%s)" % (a['email_id'], a['name'])) for a in self.read(cr, uid, ids, ['name', 'email_id'], context=context)]
-
-    def _constraint_unique(self, cursor, user, ids, context=None):
-        """
-        This makes sure that you dont give personal 
-        users two accounts with same ID (Validated in sql constaints)
-        However this constraint exempts company accounts. 
-        Any no of co accounts for a user is allowed
-        """
-        if self.read(cursor, user, ids, ['company'])[0]['company'] == 'no':
-            accounts = self.search(cursor, user, [
-                                                 ('user', '=', user),
-                                                 ('company', '=', 'no')
-                                                 ])
-            if len(accounts) > 1 :
-                return False
-            else :
-                return True
-        else:
-            return True
-        
-    _constraints = [
-        (_constraint_unique,
-         'Error: You are not allowed to have more than 1 account.',
-         [])
-    ]
-
-    def get_outgoing_server(self, cursor, user, ids, context=None):
-        """
-        Returns the Out Going Connection (SMTP) object
-        
-        @attention: DO NOT USE except_osv IN THIS METHOD
-        @param cursor: Database Cursor
-        @param user: ID of current user
-        @param ids: ID/list of ids of current object for 
-                    which connection is required
-                    First ID will be chosen from lists
-        @param context: Context
-        
-        @return: SMTP server object or Exception
-        """
-        #Type cast ids to integer
-        if type(ids) == list:
-            ids = ids[0]
-        this_object = self.browse(cursor, user, ids, context=context)
-        if this_object:
-            if this_object.smtpserver and this_object.smtpport: 
-                try:
-                    if this_object.smtpssl:
-                        serv = smtplib.SMTP_SSL(this_object.smtpserver, this_object.smtpport)
-                    else:
-                        serv = smtplib.SMTP(this_object.smtpserver, this_object.smtpport)
-                    if this_object.smtptls:
-                        serv.ehlo()
-                        serv.starttls()
-                        serv.ehlo()
-                except Exception, error:
-                    raise error
-                try:
-                    if serv.has_extn('AUTH') or this_object.smtpuname or this_object.smtppass:
-                        serv.login(str(this_object.smtpuname), str(this_object.smtppass))
-                except Exception, error:
-                    raise error
-                return serv
-            raise Exception(_("SMTP SERVER or PORT not specified"))
-        raise Exception(_("Core connection for the given ID does not exist"))
-    
-    def check_outgoing_connection(self, cursor, user, ids, context=None):
-        """
-        checks SMTP credentials and confirms if outgoing connection works
-        (Attached to button)
-        @param cursor: Database Cursor
-        @param user: ID of current user
-        @param ids: list of ids of current object for 
-                    which connection is required
-        @param context: Context
-        """
-        try:
-            self.get_outgoing_server(cursor, user, ids, context)
-            raise osv.except_osv(_("SMTP Test Connection Was Successful"), '')
-        except osv.except_osv, success_message:
-            raise success_message
-        except Exception, error:
-            raise osv.except_osv(
-                                 _("Out going connection test failed"),
-                                 _("Reason: %s") % error
-                                 )
-    
-    def do_approval(self, cr, uid, ids, context=None):
-        #TODO: Check if user has rights
-        self.write(cr, uid, ids, {'state':'approved'}, context=context)
-#        wf_service = netsvc.LocalService("workflow")
-
-    def smtp_connection(self, cursor, user, id, context=None):
-        """
-        This method should now wrap smtp_connection
-        """
-        #This function returns a SMTP server object
-        logger = netsvc.Logger()
-        core_obj = self.browse(cursor, user, id, context=context)
-        if core_obj.smtpserver and core_obj.smtpport and core_obj.state == 'approved':
-            try:
-                serv = self.get_outgoing_server(cursor, user, id, context)
-            except Exception, error:
-                logger.notifyChannel(_("Email Template"), netsvc.LOG_ERROR, _("Mail from Account %s failed on login. Probable Reason:Could not login to server\nError: %s") % (id, error))
-                return False
-            #Everything is complete, now return the connection
-            return serv
-        else:
-            logger.notifyChannel(_("Email Template"), netsvc.LOG_ERROR, _("Mail from Account %s failed. Probable Reason:Account not approved") % id)
-            return False
-                      
-#**************************** MAIL SENDING FEATURES ***********************#
-
-
-
-    
-    def send_mail(self, cr, uid, ids, addresses, subject='', body=None, payload=None, message_id=None, context=None):
-        #TODO: Replace all this with a single email object
-        if body is None:
-            body = {}
-        if payload is None:
-            payload = {}
-        if context is None:
-            context = {}
-        logger = netsvc.Logger()
-        for id in ids:  
-            core_obj = self.browse(cr, uid, id, context)
-            serv = self.smtp_connection(cr, uid, id)
-            if serv:
-                try:
-                    # Prepare multipart containers depending on data
-                    text_subtype = (core_obj.send_pref == 'alternative') and 'alternative' or 'mixed'
-                    # Need a multipart/mixed wrapper for attachments if content is alternative
-                    if payload and text_subtype == 'alternative':
-                        payload_part = MIMEMultipart(_subtype='mixed')
-                        text_part = MIMEMultipart(_subtype=text_subtype)
-                        payload_part.attach(text_part)
-                    else:
-                        # otherwise a single multipart/mixed will do the whole job 
-                        payload_part = text_part = MIMEMultipart(_subtype=text_subtype)
-
-                    if subject:
-                        payload_part['Subject'] = subject
-                    from_email = core_obj.email_id
-                    if '<' in from_email:
-                        # We have a structured email address, keep it untouched
-                        payload_part['From'] = Header(core_obj.email_id, 'utf-8').encode()
-                    else:
-                        # Plain email address, construct a structured one based on the name:
-                        sender_name = Header(core_obj.name, 'utf-8').encode()
-                        payload_part['From'] = sender_name + " <" + core_obj.email_id + ">"
-                    payload_part['Organization'] = tools.ustr(core_obj.user.company_id.name)
-                    payload_part['Date'] = formatdate()
-                    addresses_l = extract_emails_from_dict(addresses) 
-                    if addresses_l['To']:
-                        payload_part['To'] = u','.join(addresses_l['To'])
-                    if addresses_l['CC']:
-                        payload_part['CC'] = u','.join(addresses_l['CC'])
-                    if addresses_l['Reply-To']:
-                        payload_part['Reply-To'] = addresses_l['Reply-To'][0]
-                    if message_id:
-                        payload_part['Message-ID'] = message_id
-                    if body.get('text', False):
-                        temp_body_text = body.get('text', '')
-                        l = len(temp_body_text.replace(' ', '').replace('\r', '').replace('\n', ''))
-                        if l == 0:
-                            body['text'] = u'No Mail Message'
-                    # Attach parts into message container.
-                    # According to RFC 2046, the last part of a multipart message, in this case
-                    # the HTML message, is best and preferred.
-                    if core_obj.send_pref in ('text', 'mixed', 'alternative'):
-                        body_text = body.get('text', u'<Empty Message>')
-                        body_text = tools.ustr(body_text)
-                        text_part.attach(MIMEText(body_text.encode("utf-8"), _charset='UTF-8'))
-                    if core_obj.send_pref in ('html', 'mixed', 'alternative'):
-                        html_body = body.get('html', u'')
-                        if len(html_body) == 0 or html_body == u'':
-                            html_body = body.get('text', u'<p>&lt;Empty Message&gt;</p>').replace('\n', '<br/>').replace('\r', '<br/>')
-                        html_body = tools.ustr(html_body)
-                        text_part.attach(MIMEText(html_body.encode("utf-8"), _subtype='html', _charset='UTF-8'))
-
-                    #Now add attachments if any, wrapping into a container multipart/mixed if needed
-                    if payload:
-                        for file in payload:
-                            part = MIMEBase('application', "octet-stream")
-                            part.set_payload(base64.decodestring(payload[file]))
-                            part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)
-                            Encoders.encode_base64(part)
-                            payload_part.attach(part)
-                except Exception, error:
-                    logger.notifyChannel(_("Email Template"), netsvc.LOG_ERROR, _("Mail from Account %s failed. Probable Reason:MIME Error\nDescription: %s") % (id, error))
-                    return {'error_msg': _("Server Send Error\nDescription: %s")%error}
-                try:
-                    serv.sendmail(payload_part['From'], addresses_l['all-recipients'], payload_part.as_string())
-                except Exception, error:
-                    logging.getLogger('email_template').error(_("Mail from Account %s failed. Probable Reason: Server Send Error\n Description: %s"), id, error, exc_info=True)
-                    return {'error_msg': _("Server Send Error\nDescription: %s")%error}
-                #The mail sending is complete
-                serv.close()
-                logger.notifyChannel(_("Email Template"), netsvc.LOG_INFO, _("Mail from Account %s successfully Sent.") % (id))
-                return True
-            else:
-                logger.notifyChannel(_("Email Template"), netsvc.LOG_ERROR, _("Mail from Account %s failed. Probable Reason:Account not approved") % id)
-                return {'nodestroy':True,'error_msg': _("Mail from Account %s failed. Probable Reason:Account not approved")% id}
-
-    def extracttime(self, time_as_string):
-        """
-        TODO: DOC THis
-        """
-        logger = netsvc.Logger()
-        #The standard email dates are of format similar to:
-        #Thu, 8 Oct 2009 09:35:42 +0200
-        date_as_date = False
-        convertor = {'+':1, '-':-1}
-        try:
-            time_as_string = time_as_string.replace(',', '')
-            date_list = time_as_string.split(' ')
-            date_temp_str = ' '.join(date_list[1:5])
-            if len(date_list) >= 6:
-                sign = convertor.get(date_list[5][0], False)
-            else:
-                sign = False
-            try:
-                dt = datetime.datetime.strptime(
-                                            date_temp_str,
-                                            "%d %b %Y %H:%M:%S")
-            except:
-                try:
-                    dt = datetime.datetime.strptime(
-                                            date_temp_str,
-                                            "%d %b %Y %H:%M")
-                except:
-                    return False
-            if sign:
-                try:
-                    offset = datetime.timedelta(
-                                hours=sign * int(
-                                             date_list[5][1:3]
-                                                ),
-                                             minutes=sign * int(
-                                                            date_list[5][3:5]
-                                                                )
-                                                )
-                except Exception, e2:
-                    """Looks like UT or GMT, just forget decoding"""
-                    return False
-            else:
-                offset = datetime.timedelta(hours=0)
-            dt = dt + offset
-            date_as_date = dt.strftime('%Y-%m-%d %H:%M:%S')
-        except Exception, e:
-            logger.notifyChannel(
-                    _("Email Template"),
-                    netsvc.LOG_WARNING,
-                    _(
-                      "Datetime Extraction failed.Date:%s \
-                      \tError:%s") % (
-                                    time_as_string,
-                                    e)
-                      )
-        return date_as_date
-        
-    def send_receive(self, cr, uid, ids, context=None):
-        for id in ids:
-            ctx = context.copy()
-            ctx['filters'] = [('account_id', '=', id)]
-            self.pool.get('email_template.mailbox').send_all_mail(cr, uid, [], context=ctx)
-        return True
- 
-    def decode_header_text(self, text):
-        """ Decode internationalized headers RFC2822.
-            To, CC, BCC, Subject fields can contain 
-            text slices with different encodes, like:
-                =?iso-8859-1?Q?Enric_Mart=ED?= <enricmarti@xxxxxxxxxxx>, 
-                =?Windows-1252?Q?David_G=F3mez?= <david@xxxxxxxxxxx>
-            Sometimes they include extra " character at the beginning/
-            end of the contact name, like:
-                "=?iso-8859-1?Q?Enric_Mart=ED?=" <enricmarti@xxxxxxxxxxx>
-            and decode_header() does not work well, so we use regular 
-            expressions (?=   ? ?   ?=) to split the text slices
-        """
-        if not text:
-            return text        
-        p = re.compile("(=\?.*?\?.\?.*?\?=)")
-        text2 = ''
-        try:
-            for t2 in p.split(text):
-                text2 += ''.join(
-                            [s.decode(
-                                      t or 'ascii'
-                                    ) for (s, t) in decode_header(t2)]
-                                ).encode('utf-8')
-        except:
-            return text
-        return text2
-
-email_template_account()
-
-# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== removed file 'email_template/email_template_account_view.xml'
--- email_template/email_template_account_view.xml	2011-02-07 11:32:52 +0000
+++ email_template/email_template_account_view.xml	1970-01-01 00:00:00 +0000
@@ -1,124 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<openerp>
-    <data>
-
-        <menuitem name="Marketing" icon="terp-crm" id="base.marketing_menu" sequence="17"
-            groups="marketing.group_marketing_user,marketing.group_marketing_manager"/>
-        <menuitem name="Emails" id="base.menu_emails" parent="base.marketing_menu" sequence="5"/>
-        <menuitem name="Email Template" id="menu_email_template" parent="base.menu_emails"/>
-
-        <record model="ir.ui.view" id="email_template_account_form">
-            <field name="name">email_template.account.form</field>
-            <field name="model">email_template.account</field>
-            <field name="type">form</field>
-            <field name="arch" type="xml">
-                <form string="Email Account Configuration">
-                    <group colspan="2">
-                        <field name="name" select="1" />
-                    </group>
-                    <notebook colspan="4">
-                        <page string="Outgoing">
-                            <separator string="Server Information" colspan="4" />
-                            <group colspan="4" col="4">
-                                <field name="smtpserver" select="1"/>
-                                <button name="check_outgoing_connection" type="object" string="Test Outgoing Connection" icon="gtk-network" colspan="2"/>
-                                <field name="smtpport" select="2" />
-                                <field name="smtpssl" select="2" />
-                                <field name="smtptls" select="2" />
-                                <field name="auto_delete" />
-                            </group>
-                            <separator string="User Information" colspan="4" />
-                            <group col="2" colspan="2">
-                                <field name="email_id" select="1" colspan="2" />
-                                <field name="smtppass" password="True" colspan="2" />
-                                <field name="company" select="2" colspan="2" />
-                            </group>
-                            <group col="2" colspan="2">
-                                <field name="smtpuname" select="1" colspan="2" />
-                                <field name="user" select="2" colspan="2" />
-                                <field name="send_pref" colspan="2" />
-                            </group>
-                        </page>
-                    </notebook>
-                    <group colspan="4" col="10">
-                        <field name="state" select="1"/>
-                        <button string="Approve Account" name="button_approval" states="draft" type="workflow" icon="terp-camera_test"/>
-                        <button string="Suspend Account" name="button_suspended" states="approved" type="workflow" icon="gtk-cancel"/>
-                        <button string="Request Re-activation" name="get_reapprove" states="suspended" type="workflow" icon="gtk-convert"/>
-                        <button string="Send/Receive" name="send_receive" states="approved" type="object" icon="terp-check"/>
-                    </group>
-                </form>
-            </field>
-        </record>
-
-        <record model="ir.ui.view" id="email_template_account_tree">
-            <field name="name">email_template.account.tree</field>
-            <field name="model">email_template.account</field>
-            <field name="type">tree</field>
-            <field name="arch" type="xml">
-                <tree colors="blue:state in ('draft');black:state in ('suspended','approved')" string="Email Accounts">
-                    <field name="name" />
-                    <field name="email_id" />
-                    <field name="smtpuname" />
-                    <field name="user" />
-                    <field name="smtpserver" />
-                    <field name="smtpport" />
-                    <field name="auto_delete" />
-                    <field name="state" />
-                </tree>
-            </field>
-        </record>
-
-       <record id="view_email_template_account_search" model="ir.ui.view">
-           <field name="name">email_template.account.search</field>
-           <field name="model">email_template.account</field>
-           <field name="type">search</field>
-           <field name="arch" type="xml">
-               <search string="Email Accounts">
-                    <filter icon="terp-document-new" string="Draft" name="draft" domain="[('state','=','draft')]"/>
-                    <filter icon="terp-camera_test" string="Approved" domain="[('state','=','approved')]"/>
-                    <filter icon="terp-emblem-important" string="Suspended" domain="[('state','=','suspended')]"/>
-                    <separator orientation="vertical"/>
-                    <filter icon="terp-go-home" string="Company Accounts" domain="[('company','=','yes')]"/>
-                    <separator orientation="vertical"/>
-                    <field name="user" select="1">
-                        <filter icon="terp-personal" help="My Accounts" name="my" domain="[('user','=',uid)]"/>
-                    </field>
-                    <field name="name" select="1"/>
-                    <field name="email_id" select="1"/>
-               </search>
-           </field>
-        </record>
-
-        <record model="ir.actions.act_window" id="action_email_template_account_tree_all">
-            <field name="name">Accounts</field>
-            <field name="res_model">email_template.account</field>
-            <field name="view_type">form</field>
-            <field name="view_mode">form,tree</field>
-            <field name="view_id" ref="email_template_account_tree" />
-            <field name="context">{'search_default_draft': 1, 'search_default_my': 1}</field>
-            <field name="search_view_id" ref="view_email_template_account_search"/>
-        </record>
-
-        <menuitem name="Configuration" parent="base.marketing_menu"
-            id="base.menu_marketing_config_root" sequence="20" groups="base.group_system"/>
-
-        <menuitem name="Email Template" id="menu_email_template_configuration" parent="base.menu_marketing_config_root" />
-
-        <menuitem name="Email Accounts" id="menu_email_template_account_all" parent="menu_email_template_configuration" action="action_email_template_account_tree_all"/>
-
-        <menuitem name="Configuration" parent="base.menu_tools"
-              id="base.menu_lunch_survey_root" sequence="20" />
-
-        <menuitem name="Email Template" id="menu_email_template_config_tools"
-            parent="base.menu_lunch_survey_root" />
-
-        <menuitem name="Email Accounts" id="menu_email_account_all_tools"
-            parent="menu_email_template_config_tools" action="action_email_template_account_tree_all" />
-
-    </data>
-</openerp>
-
-
-
-

=== removed file 'email_template/email_template_mailbox.py'
--- email_template/email_template_mailbox.py	2011-01-24 16:13:46 +0000
+++ email_template/email_template_mailbox.py	1970-01-01 00:00:00 +0000
@@ -1,213 +0,0 @@
-# -*- coding: utf-8 -*-
-##############################################################################
-#
-#    OpenERP, Open Source Management Solution
-#    Copyright (C) 2009 Sharoon Thomas
-#    Copyright (C) 2004-2010 OpenERP SA (<http://www.openerp.com>)
-#
-#    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
-#
-#    This program is distributed in the hope that it will be useful,
-#    but WITHOUT ANY WARRANTY; without even the implied warranty of
-#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU General Public License for more details.
-#
-#    You should have received a copy of the GNU General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>
-#
-##############################################################################
-
-from osv import osv, fields
-import time
-import netsvc
-from tools.translate import _
-import tools
-
-LOGGER = netsvc.Logger()
-
-class email_template_mailbox(osv.osv):
-    _name = "email_template.mailbox"
-    _description = 'Email Mailbox'
-    _rec_name = "subject"
-    _order = "date_mail desc"
-    
-    def run_mail_scheduler(self, cursor, user, context=None):
-        """
-        This method is called by OpenERP Scheduler
-        to periodically send emails
-        """
-        try:
-            self.send_all_mail(cursor, user, context=context)
-        except Exception, e:
-            LOGGER.notifyChannel(
-                                 "Email Template",
-                                 netsvc.LOG_ERROR,
-                                 _("Error sending mail: %s") % e)
-        
-    def send_all_mail(self, cr, uid, ids=None, context=None):
-        if ids is None:
-            ids = []
-        if context is None:
-            context = {}
-        filters = [('folder', '=', 'outbox'), ('state', '!=', 'sending')]
-        if 'filters' in context.keys():
-            for each_filter in context['filters']:
-                filters.append(each_filter)
-        ids = self.search(cr, uid, filters, context=context)
-        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')
-                values = self.read(cr, uid, id, [], context) 
-                payload = {}
-                if values['attachments_ids']:
-                    for attid in values['attachments_ids']:
-                        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]],
-                              {'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)
-                if result == True:
-                    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)
-                    
-            except Exception, error:
-                logger = netsvc.Logger()
-                logger.notifyChannel("email-template", netsvc.LOG_ERROR, _("Sending of Mail %s failed. Probable Reason:Could not login to server\nError: %s") % (id, error))
-                self.historise(cr, uid, [id], error, context)
-            self.write(cr, uid, id, {'state':'na'}, context)
-        return result
-    
-    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)
-    
-    _columns = {
-            'email_from':fields.char(
-                            'From', 
-                            size=64),
-            'email_to':fields.char(
-                            'Recipient (To)', 
-                            size=250,),
-            'email_cc':fields.char(
-                            'CC', 
-                            size=250),
-            'email_bcc':fields.char(
-                            'BCC', 
-                            size=250),
-            'reply_to':fields.char(
-                            'Reply-To', 
-                            size=250),
-            'message_id':fields.char(
-                            'Message-ID', 
-                            size=250),
-            'subject':fields.char(
-                            'Subject', 
-                            size=200,),
-            'body_text':fields.text(
-                            'Standard Body (Text)'),
-            'body_html':fields.text(
-                            'Body (Rich Text Clients Only)'),
-            'attachments_ids':fields.many2many(
-                            'ir.attachment', 
-                            'mail_attachments_rel', 
-                            'mail_id', 
-                            'att_id', 
-                            'Attachments'),
-            'account_id' :fields.many2one(
-                            'email_template.account',
-                            'User account', 
-                            required=True),
-            'user':fields.related(
-                            'account_id', 
-                            'user', 
-                            type="many2one", 
-                            relation="res.users", 
-                            string="User"),
-            'server_ref':fields.integer(
-                            'Server Reference of mail', 
-                            help="Applicable for inward items only"),
-            'mail_type':fields.selection([
-                            ('multipart/mixed', 
-                             'Has Attachments'),
-                            ('multipart/alternative', 
-                             'Plain Text & HTML with no attachments'),
-                            ('multipart/related', 
-                             'Intermixed content'),
-                            ('text/plain', 
-                             'Plain Text'),
-                            ('text/html', 
-                             'HTML Body'),
-                            ], 'Mail Contents'),
-            #I like GMAIL which allows putting same mail in many folders
-            #Lets plan it for 0.9
-            'folder':fields.selection([
-                            ('drafts', 'Drafts'),
-                            ('outbox', 'Outbox'),
-                            ('trash', 'Trash'),
-                            ('sent', 'Sent Items'),
-                            ], 'Folder', required=True),
-            'state':fields.selection([
-                            ('na', 'Not Applicable'),
-                            ('sending', 'Sending'),
-                            ], 'Status', required=True),
-            'date_mail':fields.datetime('Rec/Sent Date', help="Date on which Email Sent or Received"),
-            'history':fields.text(
-                            'History', 
-                            readonly=True, 
-                            store=True)
-        }
-
-    _defaults = {
-        'state': lambda * a: 'na',
-        '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:

=== removed file 'email_template/email_template_mailbox_view.xml'
--- email_template/email_template_mailbox_view.xml	2011-02-07 11:32:52 +0000
+++ email_template/email_template_mailbox_view.xml	1970-01-01 00:00:00 +0000
@@ -1,134 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<openerp>
-    <data>
-        <!-- Email Template-->
-        <record model="ir.ui.view" id="email_template_mailbox_form">
-            <field name="name">email_template.mailbox.form</field>
-            <field name="model">email_template.mailbox</field>
-            <field name="type">form</field>
-            <field name="arch" type="xml">
-                <form string="Emails">
-                    <group col="4" colspan="4" name="headers">
-                        <field name="email_from" select="1"/>
-                        <field name="email_to" required="1" select="1" />
-                        <field name="reply_to" select="2"/>
-                        <field name="email_cc" select="1"/>
-                        <field name="email_bcc" select="2"/>
-                        <field name="date_mail" select="2"/>
-                        <field name="subject" colspan="4" select="1"/>
-                    </group>
-                    <notebook colspan="4">
-                        <page string="Standard Body">
-                            <separator colspan="4" string="Standard Body" />
-                            <notebook colspan="4">
-                                <page string="Standard Body (Text)">
-                                    <field name="body_text" nolabel="1" colspan="4" select="1"/>
-                                </page>
-                                <page string="Body (HTML-Web Client Only)">
-                                    <field name="body_html" nolabel="1" colspan="4" />
-                                </page>
-                            </notebook>
-                            <separator colspan="4" string="" />
-                            <group col="4" colspan="4">
-                                <field name="state" readonly="1" string="State"/>
-                                <button name="send_this_mail" type="object" string="Send Mail" icon="terp-mail-message-new"/>
-                            </group>
-                        </page>
-
-                        <page string="Attachments">
-                            <separator colspan="4" string="Attachments" />
-                            <field name="attachments_ids" colspan="4" nolabel="1" />
-                        </page>
-                        <page string="Advanced">
-                            <field name="account_id" colspan="2" />
-                            <field name="server_ref" colspan="2" />
-                            <field name="mail_type" colspan="2" />
-                            <field name="folder" colspan="2" select="2"/>
-                            <field name="message_id" select="2"/>
-                            <separator string="History" colspan="4" />
-                            <field name="history" nolabel="1" colspan="4"/>
-                        </page>
-                    </notebook>
-                </form>
-            </field>
-        </record>
-
-        <!--============================================= TREE VIEWS =============================================-->
-         <record id="view_email_template_mailbox_tree" model="ir.ui.view">
-            <field name="name">email_template.mailbox.tree</field>
-            <field name="model">email_template.mailbox</field>
-            <field name="type">tree</field>
-            <field name="arch" type="xml">
-                <tree string="Emails" colors="blue:folder=='drafts';grey:folder=='trash'">
-                    <field name="subject" select="1" />
-                    <field name="user" />
-                    <field name="email_from" select="1" />
-                    <field name="email_to"/>
-                    <field name="date_mail" select="2" />
-                    <field name="attachments_ids" select="2" />
-                    <field name="folder" invisible="1"/>
-                </tree>
-            </field>
-        </record>
-
-        <record id="view_email_template_mailbox_search" model="ir.ui.view">
-           <field name="name">email_template.mailbox.search</field>
-           <field name="model">email_template.mailbox</field>
-           <field name="type">search</field>
-           <field name="arch" type="xml">
-               <search string="Emails">
-                    <filter icon="terp-document-new" string="Drafts" name="draft" domain="[('folder','=','drafts')]"/>
-                    <filter icon="terp-mail-" string="Outbox" name="outbox" domain="[('folder','=','outbox')]"/>
-                    <filter icon="terp-camera_test" string="Sent" domain="[('folder','=','sent')]"/>
-                    <filter icon="terp-mail_delete" string="Trash" domain="[('folder','=','trash')]"/>
-                    <separator orientation="vertical"/>
-                    <filter icon="terp-personal+" string="Personal Emails" name="personal" domain="[('account_id.company','=','no')]"/>
-                    <filter icon="terp-go-home" string="Company Emails" name="company" domain="[('account_id.company','=','yes')]"/>
-                    <separator orientation="vertical"/>
-                    <field name="subject"/>
-                    <field name="email_from"/>
-                    <field name="user">
-                        <filter icon="terp-personal"
-                            string="My Emails" help="My Emails" name="myemails"
-                            domain="[('account_id.user','=', uid)]" />
-                    </field>
-                    <newline/>
-                    <field name="email_to"/>
-                    <field name="date_mail"/>
-               </search>
-           </field>
-        </record>
-
-        <record model="ir.actions.act_window" id="action_email_template_mailbox">
-            <field name="name">Emails</field>
-            <field name="res_model">email_template.mailbox</field>
-            <field name="view_type">form</field>
-            <field name="view_mode">tree,form</field>
-            <field name="view_id" ref="view_email_template_mailbox_tree" />
-            <field name="context">{'search_default_outbox': 1}</field>
-            <field name="help">An email template is an email document that will be sent as part of a marketing campaign. You can personalize it according to specific customer profile fields, so that a partner name or other partner related information may be inserted automatically.</field>
-            <field name="search_view_id" ref="view_email_template_mailbox_search"/>
-        </record>
-
-        <!--======================================== MENUS ========================================-->
-
-        <menuitem name="Emails"
-            id="menu_email_template_personal_mails"
-            parent="menu_email_template"
-            action="action_email_template_mailbox" />
-
-        <!-- Mailbox menu in Tools -->
-        <menuitem name="Email Template" id="menu_email_template_tools"
-            parent="base.menu_tools" />
-
-        <menuitem name="Emails"
-            id="menu_email_template_mails_tools"
-            parent="menu_email_template_tools"
-            action="action_email_template_mailbox" />
-
-    </data>
-</openerp>
-
-
-
-

=== removed file 'email_template/email_template_scheduler_data.xml'
--- email_template/email_template_scheduler_data.xml	2011-01-14 00:11:01 +0000
+++ email_template/email_template_scheduler_data.xml	1970-01-01 00:00:00 +0000
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<openerp>
-    <data noupdate="1">
-        <record forcecreate="True" id="ir_cron_mail_scheduler_action" model="ir.cron">
-            <field name="name">Email Template scheduler</field>
-            <field name="user_id" ref="base.user_root"/>
-            <field name="interval_number">1</field>
-            <field name="interval_type">hours</field>
-            <field name="numbercall">-1</field>
-            <field eval="False" name="doall"/>
-            <field eval="'email_template.mailbox'" name="model"/>
-            <field eval="'run_mail_scheduler'" name="function"/>
-            <field eval="'()'" name="args"/>
-        </record>
-    </data>
-</openerp>

=== modified file 'email_template/email_template_view.xml'
--- email_template/email_template_view.xml	2011-01-14 00:11:01 +0000
+++ email_template/email_template_view.xml	2011-04-11 12:37:32 +0000
@@ -1,115 +1,66 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <openerp>
     <data>
-    
-        <!-- Email Template Preview -->
-        <record model="ir.ui.view" id="email_template_preview_form">
-            <field name="name">email_template.preview.form</field>
-            <field name="model">email_template.preview</field>
-            <field name="type">form</field>
-            <field name="arch" type="xml">
-                <form string="Email Preview">
-                    <group col="4" colspan="4">
-                        <field name="rel_model" />
-                        <field name="rel_model_ref" on_change="on_change_ref(rel_model_ref, context)"/>
-                    </group>
-                    <group col="8" colspan="4">
-                        <field name="to" />
-                        <field name="cc" />
-                        <field name="bcc" />
-                        <field name="reply_to" />
-                        <field name="message_id" attrs="{'invisible':[('message_id','=',False)]}" groups="base.group_extended"/>
-                        <field name="subject" colspan="8"/>
-                    </group>
-                    <group col="4" colspan="4">
-                        <separator string= "Body(Text)" colspan="2"/>   
-                        <separator string= "Body(Html)" colspan="2"/>
-                        <newline/>
-                        <field name="body_text" nolabel="1" colspan="2"/>
-                        <field name="body_html" nolabel="1" colspan="2"/>
-                    </group>
-                    <field name="report" colspan="2"/>
-                    <button icon="gtk-ok" special="cancel" string="OK" colspan="1"/>
-                </form>
-            </field>
-        </record>
-        
-        <record id="wizard_email_template_preview" model="ir.actions.act_window">
-            <field name="name">Template Preview</field>
-            <field name="res_model">email_template.preview</field>
-            <field name="src_model">email_template.preview</field>
-            <field name="type">ir.actions.act_window</field>
-            <field name="view_type">form</field>
-            <field name="view_mode">form</field>
-            <field name="auto_refresh" eval="1" />
-            <field name="target">new</field>
-            <field name="context">{'template_id':active_id}</field>
-        </record>
-        <!--EMail client Form view  -->
 
         <record model="ir.ui.view" id="email_template_form">
             <field name="name">email.template.form</field>
             <field name="model">email.template</field>
             <field name="type">form</field>
             <field name="arch" type="xml">
-                <form string="Email Templates">
-                    <field name="name" />
-                    <field name="object_name" required="1"
-                        on_change="change_model(object_name)" />
-                    <field name="model_int_name" invisible="1" />
+                <form string="Templates">
+                    <field name="name" required="1"/>
+                    <field name="model_id" required="1"
+                        on_change="onchange_model_id(model_id)"/>
+                    <field name="model" invisible="1"/>
                     <notebook colspan="4">
-                        <page string="Mail Details">
+                        <page string="Email Details">
                             <group col="2" colspan="2">
                                 <separator string="Addresses" colspan="2"/>
-                                <field name="from_account" required="1"/>
-                                <field name="def_to" required="1"/>
-                                <field name="def_cc"/>
-                                <field name="def_bcc"/>
+                                <field name="smtp_server_id"/>
+                                <field name="email_to" required="1"/>
+                                <field name="email_cc"/>
+                                <field name="email_bcc"/>
                                 <field name="reply_to"/>
                             </group>
                             <group col="2" colspan="2">
                                 <separator string="Options" colspan="2"/>
                                 <field name="lang" colspan="4" />
-                                <field name="use_sign" colspan="4" />
+                                <field name="user_signature" colspan="4" />
                                 <field name="track_campaign_item" colspan="4"/>
                             </group>
                             <group col="2" colspan="2">
-                                <separator colspan="2" string="Email Content  " />
-                                <field name="def_subject" colspan="4" required="1" />
+                                <separator colspan="2" string="Email Content"/>
+                                <field name="subject" colspan="4" required="1"/>
                                 <notebook>
-                                    <page string="Body (Text)">
-                                        <field name="def_body_text" colspan="4" nolabel="1" />
-                                    </page>
-                                    <page string="Body (Raw HTML)">
-                                        <field name="def_body_html" colspan="4" nolabel="1" />
-                                        <label string="Note: This is Raw HTML." colspan="4" />
-                                    </page>
+                                    <page string="Body">
+                                        <field name="body" colspan="4" nolabel="1"/>
+                                    </page>
+                                    <!-- <page string="Body (Raw HTML)">
+                                        <field name="def_body_html" colspan="4" nolabel="1"/>
+                                        <label string="Note: This is Raw HTML." colspan="4"/>
+                                    </page> -->
                                 </notebook>
                             </group>
                             <group col="4" colspan="2">
-                                <separator colspan="4" string="Expression Builder" />
-                                <field name="template_language"
-                                    on_change="onchange_null_value(model_object_field,sub_model_object_field,null_value,template_language,context)" />
                                 <notebook>
                                     <page string="Insert Simple Field">
-
                                         <field name="model_object_field"
-                                            domain="[('model_id','=',object_name),('ttype','!=','one2many'),('ttype','!=','many2many')]"
-                                            on_change="onchange_model_object_field(model_object_field, template_language,context)"
-                                            colspan="4" />
-                                        <field name="sub_object" readonly="1" colspan="4" />
+                                            domain="[('model_id','=',model_id),('ttype','!=','one2many'),('ttype','!=','many2many')]"
+                                            on_change="onchange_sub_model_object_value_field(model_object_field)"
+                                            colspan="4"/>
+                                        <field name="sub_object" readonly="1" colspan="4"/>
                                         <field name="sub_model_object_field"
                                             domain="[('model_id','=',sub_object),('ttype','!=','one2many'),('ttype','!=','many2many')]"
                                             colspan="4"
                                             attrs="{'readonly':[('sub_object','=',False)],'required':[('sub_object','!=',False)]}"
-                                            on_change="onchange_sub_model_object_field(model_object_field,sub_model_object_field,template_language,context)" />
+                                            on_change="onchange_sub_model_object_value_field(model_object_field,sub_model_object_field)"/>
                                         <field name="null_value" colspan="4"
-                                            on_change="onchange_null_value(model_object_field,sub_model_object_field,null_value,template_language,context)" />
-                                        <field name="copyvalue" colspan="4" />
+                                            on_change="onchange_sub_model_object_value_field(model_object_field,sub_model_object_field,null_value)" />
+                                        <field name="copyvalue" colspan="4"/>
                                     </page>
                                 </notebook>
                                 <button name="%(wizard_email_template_preview)d" string="Preview Template"
-                                    type="action" colspan="4" target="new" icon="gtk-zoom-fit"/>
+                                    type="action" colspan="4" target="new" icon="gtk-zoom-fit" context="{'template_id':active_id}"/>
                             </group>
                         </page>
                         <page string="Advanced">
@@ -119,11 +70,12 @@
                                     <button name="create_action" string="Create Action" type="object" icon="gtk-execute"  colspan="2" attrs="{'invisible':[('ref_ir_act_window','!=',False), ('ref_ir_value','!=',False)]}"/>
                                     <field name="ref_ir_act_window"/>
                                     <field name="ref_ir_value"/>
-                                    <button name="delete_action" string="Delete Action" type="object" icon="gtk-delete" colspan="2" attrs="{'invisible':[('ref_ir_act_window','=',False), ('ref_ir_value','=',False)]}"/>
+                                    <button name="unlink_action" string="Delete Action" type="object" icon="gtk-delete" colspan="2" attrs="{'invisible':[('ref_ir_act_window','=',False), ('ref_ir_value','=',False)]}"/>
                                 </group>
                                 <group colspan="2" col="2">
                                     <separator string="Advanced Options" colspan="2"/>
                                     <field name="message_id"/>
+                                    <field name="auto_delete"/>
                                 </group>
                             </group>
                             <group colspan="2" col="2">
@@ -134,8 +86,8 @@
                                     </page>
                                     <page string="Report">
                                         <field name="report_template" colspan="4"
-                                            domain="[('model','=',model_int_name)]" />
-                                        <field name="file_name" colspan="4" />
+                                            domain="[('model','=',model)]"/>
+                                        <field name="report_name" colspan="4" />
                                     </page>
                                 </notebook>
                             </group>
@@ -150,16 +102,16 @@
             <field name="model">email.template</field>
             <field name="type">tree</field>
             <field name="arch" type="xml">
-                <tree string="Email Templates">
+                <tree string="Templates">
                     <field name="name"/>
-                    <field name="from_account"/>
-                    <field name="object_name"/>
-                    <field name="def_to"/>
-                    <field name="def_cc"/>
-                    <field name="def_bcc"/>
-                    <field name="def_subject"/>
-                    <field name="use_sign"/>
-                    <field name="file_name"/>
+                    <field name="smtp_server_id"/>
+                    <field name="model_id"/>
+                    <field name="email_to"/>
+                    <field name="email_cc"/>
+                    <field name="email_bcc"/>
+                    <field name="subject"/>
+                    <field name="user_signature"/>
+                    <field name="report_name"/>
                     <button name="%(wizard_email_template_preview)d" string="Preview Template"
                             type="action" target="new" icon="gtk-zoom-fit"/>
                 </tree>
@@ -173,26 +125,26 @@
            <field name="arch" type="xml">
                <search string="Templates">
                     <group col="13" colspan="4">
-                        <field name="name" select="1"/>
-                        <field name="object_name" select="1"/>
-                        <field name="def_to" select="1"/>
+                        <field name="name"/>
+                        <field name="model_id"/>
+                        <field name="email_to"/>
                         <separator orientation="vertical"/>
-                        <field name="lang" select="1"/>
-                        <field name="def_subject" select="1"/>
-                        <field name="file_name" select="1"/>
+                        <field name="lang"/>
+                        <field name="subject"/>
+                        <field name="report_name"/>
                     </group>
                     <newline/>
                     <group expand="0" string="Group by..." colspan="4" col="10">
-                        <filter string="Account" domain="[]" context="{'group_by':'from_account'}" icon="terp-folder-orange"/>
+                        <filter string="SMTP Server" domain="[]" context="{'group_by':'smtp_server_id'}" icon="terp-folder-orange"/>
                         <separator orientation="vertical"/>
-                        <filter string="Resource" domain="[]" context="{'group_by':'object_name'}" icon="terp-accessories-archiver"/>
+                        <filter string="Model" domain="[]" context="{'group_by':'model_id'}" icon="terp-accessories-archiver"/>
                     </group>
                </search>
            </field>
         </record>
 
         <record model="ir.actions.act_window" id="action_email_template_tree_all">
-            <field name="name">Email Templates</field>
+            <field name="name">Templates</field>
             <field name="res_model">email.template</field>
             <field name="view_type">form</field>
             <field name="view_mode">form,tree</field>
@@ -200,16 +152,8 @@
             <field name="search_view_id" ref="view_email_template_search"/>
         </record>
 
-        <menuitem name="Email Templates" id="menu_email_template_all"
-            parent="menu_email_template_configuration" action="action_email_template_tree_all" />
+        <menuitem name="Templates" id="menu_email_template_all_tools"
+            parent="mail.menu_email_message_tools" action="action_email_template_tree_all" />
 
-        <!-- Email Template menu in Tools -->
-        <menuitem name="Email Templates" id="menu_email_template_all_tools"
-            parent="menu_email_template_config_tools" action="action_email_template_tree_all" />
     </data>
 </openerp>
-
-
-
-
-

=== removed file 'email_template/email_template_workflow.xml'
--- email_template/email_template_workflow.xml	2011-01-14 00:11:01 +0000
+++ email_template/email_template_workflow.xml	1970-01-01 00:00:00 +0000
@@ -1,64 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<openerp>
-    <data>
-
-        <record id="wkf_email_template_setting" model="workflow">
-            <field name="name">Email Template Workflow</field>
-            <field name="osv">email_template.account</field>
-            <field name="on_create">True</field>
-        </record>
-
-        <!--Activity -->
-
-         <record id="act_draft" model="workflow.activity">
-            <field name="wkf_id" ref="wkf_email_template_setting"/>
-            <field name="flow_start">True</field>
-            <field name="name">draft</field>
-        <field name="kind">function</field>
-            <field name="action">write({'state':'draft'})</field>
-        </record>
-
-        <record id="act_approved" model="workflow.activity">
-            <field name="name">approval</field>
-            <field name="wkf_id" ref="wkf_email_template_setting"/>
-            <field name="kind">function</field>
-            <field name="action">do_approval()</field>
-        </record>
-
-        <record id="act_suspended" model="workflow.activity">
-            <field name="name">suspended</field>
-            <field name="wkf_id" ref="wkf_email_template_setting"/>
-            <field name="kind">function</field>
-            <field name="action">write({'state':'suspended'})</field>
-        </record>
-        <record id="act_dummy" model="workflow.activity">
-            <field name="name">dummy</field>
-            <field name="wkf_id" ref="wkf_email_template_setting"/>
-            <field name="flow_stop">True</field>
-        </record>
-
-        <!-- Transition -->
-
-        <record id="trans_awaiting_approved" model="workflow.transition">
-            <field name="act_from" ref="act_draft"/>
-            <field name="act_to" ref="act_approved"/>
-            <field name="signal">button_approval</field>
-        </record>
-
-        <record id="trans_approved_suspended" model="workflow.transition">
-            <field name="act_from" ref="act_approved"/>
-            <field name="act_to" ref="act_suspended"/>
-            <field name="signal">button_suspended</field>
-        </record>
-        <record id="trans_suspended_reapproved" model="workflow.transition">
-            <field name="act_from" ref="act_suspended"/>
-            <field name="act_to" ref="act_draft"/>
-            <field name="signal">get_reapprove</field>
-        </record>
-        <record id="trans_suspended_dummy" model="workflow.transition">
-            <field name="act_from" ref="act_suspended"/>
-            <field name="act_to" ref="act_dummy"/>
-            <field name="signal">get_never</field>
-        </record>
-    </data>
-</openerp>

=== added file 'email_template/mako_template.py'
--- email_template/mako_template.py	1970-01-01 00:00:00 +0000
+++ email_template/mako_template.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,59 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2009 Sharoon Thomas
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+import tools
+from tools.translate import _
+import pooler
+import logging
+
+try:
+    from mako.template import Template as MakoTemplate
+except ImportError:
+    logging.getLogger('init').warning("module email_template: Mako templates not installed")
+
+def get_value(cr, uid, message=None, model=None, record_id=False, context=None):
+    """
+    returns Messages in Mako Template
+    """
+    pool = pooler.get_pool(cr.dbname)
+    if message is None:
+        message = {}
+    #Returns the computed expression
+    if message:
+        try:
+            message = tools.ustr(message)
+            record = pool.get(model).browse(cr, uid, record_id, context=context)
+            env = {
+                'user': pool.get('res.users').browse(cr, uid, uid, context=context),
+                'db': cr.dbname
+               }
+            templ = MakoTemplate(message, input_encoding='utf-8')
+            reply = MakoTemplate(message).render_unicode(object=record, peobject=record, env=env, format_exceptions=True)
+            return reply or False
+        except Exception:
+            logging.exception("can't render %r", message)
+            return u""
+    else:
+        return message
+
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
+

=== removed file 'email_template/security/email_template_security.xml'
--- email_template/security/email_template_security.xml	2011-02-25 10:23:30 +0000
+++ email_template/security/email_template_security.xml	1970-01-01 00:00:00 +0000
@@ -1,6 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<openerp>
-    <data noupdate="0">
-
-    </data>
-</openerp>

=== modified file 'email_template/security/ir.model.access.csv'
--- email_template/security/ir.model.access.csv	2011-01-14 00:11:01 +0000
+++ email_template/security/ir.model.access.csv	2011-04-11 12:37:32 +0000
@@ -1,10 +1,4 @@
 "id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
-"access_email_template_account","email_template.account","model_email_template_account","marketing.group_marketing_user",1,0,0,0
-"access_email_template","email.template","model_email_template","marketing.group_marketing_user",1,0,0,0
-"access_email_template_mailbox","email_template.mailbox","model_email_template_mailbox","marketing.group_marketing_user",1,1,1,1
-"access_email_template_account_system","email_template.account system","model_email_template_account","base.group_system",1,1,1,1
+"access_email_template","email.template","model_email_template",,1,0,0,0
 "access_email_template_system","email.template system","model_email_template","base.group_system",1,1,1,1
-"access_email_template_mailbox_system","email_template.mailbox system","model_email_template_mailbox","base.group_system",1,0,0,0
-"access_email_template_account_manager","email_template.account","model_email_template_account","marketing.group_marketing_manager",1,1,1,1
-"access_email_template_manager","email.template","model_email_template","marketing.group_marketing_manager",1,1,1,1
-"access_email_template_mailbox_manager","email_template.mailbox","model_email_template_mailbox","marketing.group_marketing_manager",1,1,1,1
+"access_email_template_manager","email.template","model_email_template",,1,1,1,1

=== modified file 'email_template/wizard/__init__.py'
--- email_template/wizard/__init__.py	2011-01-14 00:11:01 +0000
+++ email_template/wizard/__init__.py	2011-04-11 12:37:32 +0000
@@ -3,7 +3,7 @@
 #
 #    OpenERP, Open Source Management Solution
 #    Copyright (C) 2009 Sharoon Thomas
-#    Copyright (C) 2010-2010 OpenERP SA (<http://www.openerp.com>)
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
 #
 #    This program is free software: you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
@@ -19,5 +19,7 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
 #
 ##############################################################################
+import email_template_preview
+import email_template_send_wizard
+import email_compose_message
 
-import email_template_send_wizard

=== added file 'email_template/wizard/email_compose_message.py'
--- email_template/wizard/email_compose_message.py	1970-01-01 00:00:00 +0000
+++ email_template/wizard/email_compose_message.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,145 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+from osv import osv
+from osv import fields
+import tools
+
+class email_compose_message(osv.osv_memory):
+    _name = 'email.compose.message'
+    _inherit = 'email.compose.message'
+
+    def get_template_data(self, cr, uid, res_id, template_id, context=None):
+        if context is None:
+            context = {}
+        result = {}
+        template_pool = self.pool.get('email.template')
+        if template_id:
+            template = template_pool.get_email_template(cr, uid, template_id=template_id, context=context)
+            def _get_template_value(field):
+                if not template:
+                    return False
+                if len(context.get('src_rec_ids',[])) > 1: # Multiple Mail: Gets original template values for multiple email change
+                    return getattr(template, field)
+                else: # Simple Mail: Gets computed template values
+                    return template_pool.get_template_value(cr, uid, getattr(template, field), template.model, context.get('email_res_id'), context)
+            result.update({
+                    'template_id' : template.id,
+                    'smtp_server_id' : template.smtp_server_id.id,
+                    'body' : _get_template_value('body') or False,
+                    'subject' : _get_template_value('subject') or False,
+                    'attachment_ids' : template_pool.read(cr, uid, template.id, ['attachment_ids'])['attachment_ids'] or [],
+                    'res_id' : res_id or False,
+                    'email_to' : _get_template_value('email_to') or False,
+                    'email_cc' : _get_template_value('email_cc') or False,
+                    'email_bcc' : _get_template_value('email_bcc') or False,
+                    'reply_to' : _get_template_value('reply_to') or False,
+                    'model' : template.model or False,
+                })
+        return result
+
+    def default_get(self, cr, uid, fields, context=None):
+        if context is None:
+            context = {}
+        result = super(email_compose_message, self).default_get(cr, uid, fields, context=context)
+        template_id = context.get('template_id', False)
+        vals = {}
+        if template_id and context.get('active_model') and context.get('active_id'):
+            vals = self.get_template_data(cr, uid, context.get('active_id'), template_id, context)
+
+        if not vals:
+            return result
+
+        if 'template_id' in fields:
+            result.update({'template_id' : vals.get('template_id', False)})
+
+        if 'smtp_server_id' in fields:
+            result.update({'smtp_server_id' : vals.get('smtp_server_id', False)})
+
+        if 'attachment_ids' in fields:
+            result.update({'attachment_ids' : vals.get('attachment_ids', False)})
+
+        if 'model' in fields:
+            result.update({'model' : vals.get('model', False)})
+
+        if 'res_id' in fields:
+            result.update({'res_id' : vals.get('res_id', False)})
+
+        if 'email_to' in fields:
+            result.update({'email_to' : vals.get('email_to', False)})
+
+        if 'email_cc' in fields:
+            result.update({'email_cc' : vals.get('email_cc', False)})
+
+        if 'email_bcc' in fields:
+            result.update({'email_bcc' : vals.get('email_bcc', False)})
+
+        if 'subject' in fields:
+            result.update({'subject' : vals.get('name', False)})
+
+        if 'body' in fields:
+            result.update({'body' : vals.get('body', False)})
+
+        if 'reply_to' in fields:
+            result.update({'reply_to' : vals.get('reply_to', False)})
+
+        return result
+
+    def _get_templates(self, cr, uid, context=None):
+        """
+        Return Email Template of particular  Model.
+        """
+        if context is None:
+            context = {}
+        record_ids = []
+        email_temp_pool = self.pool.get('email.template')
+        model = False
+        if context.get('message_id'):
+            message_pool = self.pool.get('email.message')
+            message_data = message_pool.browse(cr, uid, int(context.get('message_id')), context)
+            model = message_data.model
+        elif context.get('active_model',False):
+            model =  context.get('active_model')
+        if model:
+            record_ids = email_temp_pool.search(cr, uid, [('model','=',model)])
+            return email_temp_pool.name_get(cr, uid, record_ids, context) + [(False,'')]
+        return []
+
+    _columns = {
+        'template_id': fields.selection(_get_templates, 'Template'),
+    }
+
+    def on_change_template(self, cr, uid, ids, model, template_id, context=None):
+        if context is None:
+            context = {}
+        if context.get('mail') == 'reply':
+            return {'value':{}}
+        vals = {}
+        resource_id = context.get('active_id', False)
+        if template_id and resource_id:
+            vals.update(self.get_template_data(cr, uid, resource_id, template_id, context))
+        else:
+            vals.update({'attachment_ids' : []})
+        return {'value': vals}
+
+email_compose_message()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'email_template/wizard/email_compose_message_view.xml'
--- email_template/wizard/email_compose_message_view.xml	1970-01-01 00:00:00 +0000
+++ email_template/wizard/email_compose_message_view.xml	2011-04-11 12:37:32 +0000
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+	<data>
+
+        <record model="ir.ui.view" id="email_compose_message_wizard_inherit_form">
+            <field name="name">email.compose.message.form</field>
+            <field name="model">email.compose.message</field>
+            <field name="type">form</field>
+            <field name="inherit_id" ref="mail.email_compose_message_wizard_form"/>
+            <field name="arch" type="xml">
+	            <field name="model" position="after">
+	                <field name="template_id" colspan="4"
+	                    on_change="on_change_template(model, template_id, context)"/>
+	            </field>
+            </field>
+        </record>
+	</data>
+</openerp>

=== added file 'email_template/wizard/email_template_preview.py'
--- email_template/wizard/email_template_preview.py	1970-01-01 00:00:00 +0000
+++ email_template/wizard/email_template_preview.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,108 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2009 Sharoon Thomas
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+from osv import osv, fields
+from tools.translate import _
+import tools
+
+
+class email_template_preview(osv.osv_memory):
+    _inherit = "email.template"
+    _name = "email_template.preview"
+    _description = "Email Template Preview"
+    _rec_name = "subject"
+
+    def _get_records(self, cr, uid, context=None):
+        """
+        Return Records of particular Email Template's Model
+        """
+        if context is None:
+            context = {}
+
+        template_id = context.get('template_id', False)
+        if not template_id:
+            return []
+        template_pool = self.pool.get('email.template')
+        model_pool = self.pool.get('ir.model')
+        template = template_pool.browse(cr, uid, int(template_id), context=context)
+        template_object = template.model_id
+        model =  self.pool.get(template_object.model)
+        record_ids = model.search(cr, uid, [], 0, 20, 'id', context=context)
+        default_id = context.get('default_res_id')
+        if default_id and default_id not in record_ids:
+            record_ids.insert(0, default_id)
+
+        return model.name_get(cr, uid, record_ids, context)
+
+
+    def default_get(self, cr, uid, fields, context=None):
+        if context is None:
+            context = {}
+        result = super(email_template_preview, self).default_get(cr, uid, fields, context=context)
+
+        template_pool = self.pool.get('email.template')
+        template_id = context.get('template_id',False)
+        if 'res_id' in fields:
+            records = self._get_records(cr, uid, context=context)
+            result['res_id'] = records and records[0][0] or False # select first record as a Default
+        if template_id and 'model_id' in fields:
+            result['model_id'] = template_pool.read(cr, uid, int(template_id), ['model_id'], context).get('model_id', False)
+        return result
+
+    _columns = {
+        'res_id':fields.selection(_get_records, 'Referred Document'),
+    }
+    _defaults = {
+    }
+    def on_change_ref(self, cr, uid, ids, res_id, context=None):
+        if context is None:
+            context = {}
+        if not res_id:
+            return {}
+        vals = {}
+        if context == {}:
+            context = self.context
+
+        template_pool = self.pool.get('email.template')
+        template_id = context.get('template_id', False)
+        template = template_pool.get_email_template(cr, uid, template_id=template_id, record_id=res_id, context=context)
+        model = template.model
+        vals['email_to'] = self.get_template_value(cr, uid, template.email_to, model, res_id, context)
+        vals['email_cc'] = self.get_template_value(cr, uid, template.email_cc, model, res_id, context)
+        vals['email_bcc'] = self.get_template_value(cr, uid, template.email_bcc, model, res_id, context)
+        vals['reply_to'] = self.get_template_value(cr, uid, template.reply_to, model, res_id, context)
+        if template.message_id:
+            vals['message_id'] = self.get_template_value(cr, uid, message_id, model, res_id, context)
+        elif template.track_campaign_item:
+            vals['message_id'] = tools.generate_tracking_message_id(res_id)
+        vals['subject'] = self.get_template_value(cr, uid, template.subject, model, res_id, context)
+        description = self.get_template_value(cr, uid, template.body, model, res_id, context) or ''
+        if template.user_signature:
+            signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature
+            description += '\n' + signature
+        vals['body'] = description
+        vals['report_name'] = self.get_template_value(cr, uid, template.report_name, model, res_id, context)
+        return {'value':vals}
+
+email_template_preview()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'email_template/wizard/email_template_preview_view.xml'
--- email_template/wizard/email_template_preview_view.xml	1970-01-01 00:00:00 +0000
+++ email_template/wizard/email_template_preview_view.xml	2011-04-11 12:37:32 +0000
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+	<data>
+        <!-- Email Template Preview -->
+        <record model="ir.ui.view" id="email_template_preview_form">
+            <field name="name">email_template.preview.form</field>
+            <field name="model">email_template.preview</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Email Preview">
+                    <group col="4" colspan="4">
+                        <field name="model_id" readonly="1"/>
+                        <field name="res_id" on_change="on_change_ref(res_id, context)"/>
+                    </group>
+                    <group col="8" colspan="4">
+                        <field name="email_to" readonly="1"/>
+                        <field name="email_cc" readonly="1"/>
+                        <field name="email_bcc" readonly="1"/>
+                        <field name="reply_to" readonly="1"/>
+                        <field name="message_id" readonly="1" attrs="{'invisible':[('message_id','=',False)]}" groups="base.group_extended"/>
+                        <field name="subject" colspan="8" readonly="1"/>
+                    </group>
+                    <group col="4" colspan="4">
+                        <separator string= "Body" colspan="2" />
+                        <!--<separator string= "Body(Html)" colspan="2" />-->
+                        <newline/>
+                        <field name="body" nolabel="1" colspan="2" readonly="1"/>
+                        <!-- <field name="body_html" nolabel="1" colspan="2" readonly="1"/>-->
+                    </group>
+                    <field name="report_name" colspan="2" readonly="1"/>
+                    <button icon="gtk-ok" special="cancel" string="OK" colspan="1"/>
+                </form>
+            </field>
+        </record>
+
+        <record id="wizard_email_template_preview" model="ir.actions.act_window">
+            <field name="name">Template Preview</field>
+            <field name="res_model">email_template.preview</field>
+            <field name="src_model">email_template.preview</field>
+            <field name="type">ir.actions.act_window</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">form</field>
+            <field name="auto_refresh" eval="1" />
+            <field name="target">new</field>
+            <field name="context">{'template_id':active_id}</field>
+        </record>
+
+	</data>
+</openerp>

=== modified file 'email_template/wizard/email_template_send_wizard.py'
--- email_template/wizard/email_template_send_wizard.py	2011-02-15 13:27:07 +0000
+++ email_template/wizard/email_template_send_wizard.py	2011-04-11 12:37:32 +0000
@@ -3,7 +3,7 @@
 #
 #    OpenERP, Open Source Management Solution
 #    Copyright (C) 2009 Sharoon Thomas
-#    Copyright (C) 2010-2010 OpenERP SA (<http://www.openerp.com>)
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
 #
 #    This program is free software: you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
@@ -21,165 +21,118 @@
 ##############################################################################
 
 from osv import osv, fields
-from mako.template import Template
-from mako import exceptions
 import netsvc
-import base64
 from tools.translate import _
-import tools
-from email_template.email_template import get_value
-
-
-## FIXME: this wizard duplicates a lot of features of the email template preview,
-##        one of the 2 should inherit from the other!
 
 class email_template_send_wizard(osv.osv_memory):
     _name = 'email_template.send.wizard'
+    _inherit = 'email.template'
     _description = 'This is the wizard for sending mail'
-    _rec_name = "subject"
-
-    def _get_accounts(self, cr, uid, context=None):
-        if context is None:
-            context = {}
-
-        template = self._get_template(cr, uid, context)
-        if not template:
-            return []
-
-        logger = netsvc.Logger()
-
-        if template.from_account:
-            return [(template.from_account.id, '%s (%s)' % (template.from_account.name, template.from_account.email_id))]
-        else:
-            account_id = self.pool.get('email_template.account').search(cr,uid,[('company','=','no'),('user','=',uid)], context=context)
-            if account_id:
-                account = self.pool.get('email_template.account').browse(cr,uid,account_id, context)
-                return [(r.id,r.name + " (" + r.email_id + ")") for r in account]
-            else:
-                logger.notifyChannel(_("email-template"), netsvc.LOG_ERROR, _("No personal email accounts are configured for you. \nEither ask admin to enforce an account for this template or get yourself a personal email account."))
-                raise osv.except_osv(_("Missing mail account"),_("No personal email accounts are configured for you. \nEither ask admin to enforce an account for this template or get yourself a personal email account."))
-
-    def get_value(self, cursor, user, template, message, context=None, id=None):
-        """Gets the value of the message parsed with the content of object id (or the first 'src_rec_ids' if id is not given)"""
-        if not message:
-            return ''
-        if not id:
-            id = context['src_rec_ids'][0]
-        return get_value(cursor, user, id, message, template, context)
-    
-    def _get_template(self, cr, uid, context=None):
-        if context is None:
-            context = {}
-        if not 'template' in context and not 'template_id' in context:
-            return None
-        if 'template_id' in context.keys():
-            template_ids = self.pool.get('email.template').search(cr, uid, [('id','=',context['template_id'])], context=context)
-        elif 'template' in context.keys():
-            # Old versions of email_template used the name of the template. This caused
-            # problems when the user changed the name of the template, but we keep the code
-            # for compatibility with those versions.
-            template_ids = self.pool.get('email.template').search(cr, uid, [('name','=',context['template'])], context=context)
-        if not template_ids:
-            return None
-
-        template = self.pool.get('email.template').browse(cr, uid, template_ids[0], context)
-
-        lang = self.get_value(cr, uid, template, template.lang, context)
-        if lang:
-            # Use translated template if necessary
-            ctx = context.copy()
-            ctx['lang'] = lang
-            template = self.pool.get('email.template').browse(cr, uid, template.id, ctx)
-        return template
-
-    def _get_template_value(self, cr, uid, field, context=None):
-        if context is None:
-            context = {}
-        template = self._get_template(cr, uid, context)
-        if not template:
-            return False
-        if len(context['src_rec_ids']) > 1: # Multiple Mail: Gets original template values for multiple email change
-            return getattr(template, field)
-        else: # Simple Mail: Gets computed template values
-            return self.get_value(cr, uid, template, getattr(template, field), context)
+
+    def default_get(self, cr, uid, fields, context=None):
+        if context is None:
+            context = {}
+        result = super(email_template_send_wizard, self).default_get(cr, uid, fields, context=context)
+
+        template_pool = self.pool.get('email.template')
+        model_pool = self.pool.get('ir.model')
+        template_id=context.get('template_id', False)
+        template = template_pool.get_email_template(cr, uid, template_id=template_id, context=context)
+        def _get_template_value(field):
+            if not template:
+                return False
+            if len(context['src_rec_ids']) > 1: # Multiple Mail: Gets original template values for multiple email change
+                return getattr(template, field)
+            else: # Simple Mail: Gets computed template values
+                return self.get_template_value(cr, uid, getattr(template, field), template.model, context.get('active_id'), context)
+
+        if 'user_signature' in fields:
+            result['user_signature'] = template.user_signature
+
+        if 'report_template' in fields:
+            result['report_template'] = template.report_template and template.report_template.id or False
+
+        if 'template_id' in fields:
+            result['template_id'] = template.id
+
+        if 'smtp_server_id' in fields:
+            result['smtp_server_id'] = template.smtp_server_id.id
+
+        if 'message_id' in fields:
+            result['message_id'] = template.message_id
+
+        if 'track_campaign_item' in fields:
+            result['track_campaign_item'] = template.track_campaign_item
+
+        if 'attachment_ids' in fields:
+            result['attachment_ids'] = template_pool.read(cr, uid, template.id, ['attachment_ids'])['attachment_ids']
+
+        if 'requested' in fields:
+            result['requested'] = len(context.get('src_rec_ids',''))
+
+        if 'state' in fields:
+            result['state'] =  len(context.get('src_rec_ids','')) > 1 and 'multi' or 'single'
+
+        if 'model_id' in fields:
+            result['model_id'] = model_pool.search(cr, uid, [('model','=',context.get('src_model'))],context=context)[0]
+
+        if 'res_id' in fields:
+            result['res_id'] = context['active_id']
+
+        if 'email_to' in fields:
+            result['email_to'] = _get_template_value('email_to')
+
+        if 'email_cc' in fields:
+            result['email_cc'] = _get_template_value('email_cc')
+
+        if 'email_bcc' in fields:
+            result['email_bcc'] = _get_template_value('email_bcc')
+
+        if 'subject' in fields:
+            result['subject'] = _get_template_value('subject')
+
+        if 'body' in fields:
+            result['body'] = _get_template_value('description')
+
+        #if 'body_html' in fields:
+        #    result['body_html'] = _get_template_value('body_html')
+
+        if 'reply_to' in fields:
+            result['reply_to'] = _get_template_value('reply_to')
+
+        if 'report_name' in fields:
+            result['report_name'] = _get_template_value('report_name')
+
+        return result
 
     _columns = {
+        'requested':fields.integer('No of requested Mails',readonly=True),
+        'generated':fields.integer('No of generated Mails',readonly=True),
+        'full_success':fields.boolean('Complete Success',readonly=True),
+        'attachment_ids': fields.many2many('ir.attachment','send_wizard_attachment_rel', 'wizard_id', 'attachment_id', 'Attachments'),
         'state':fields.selection([
                         ('single','Simple Mail Wizard Step 1'),
                         ('multi','Multiple Mail Wizard Step 1'),
                         ('done','Wizard Complete')
-                                  ],'Status',readonly=True),
-        'ref_template':fields.many2one('email.template','Template',readonly=True),
-        'rel_model':fields.many2one('ir.model','Model',readonly=True),
-        'rel_model_ref':fields.integer('Referred Document',readonly=True),
-        'from':fields.selection(_get_accounts,'From Account',select=True),
-        'to':fields.char('To',size=250,required=True),
-        'cc':fields.char('CC',size=250,),
-        'bcc':fields.char('BCC',size=250,),
-        'reply_to':fields.char('Reply-To', 
-                    size=250, 
-                    help="The address recipients should reply to,"
-                         " if different from the From address."
-                         " Placeholders can be used here."),
-        'message_id':fields.char('Message-ID', 
-                    size=250, 
-                    help="The Message-ID header value, if you need to"
-                         "specify it, for example to automatically recognize the replies later."
-                        " Placeholders can be used here."),
-        'subject':fields.char('Subject',size=200),
-        'body_text':fields.text('Body',),
-        'body_html':fields.text('Body',),
-        'report':fields.char('Report File Name',size=100,),
-        'signature':fields.boolean('Attach my signature to mail'),
-        #'filename':fields.text('File Name'),
-        'requested':fields.integer('No of requested Mails',readonly=True),
-        'generated':fields.integer('No of generated Mails',readonly=True), 
-        'full_success':fields.boolean('Complete Success',readonly=True),
-        'attachment_ids': fields.many2many('ir.attachment','send_wizard_attachment_rel', 'wizard_id', 'attachment_id', 'Attachments'),
+                                  ],'State',readonly=True),
     }
 
-    #FIXME: probably better by overriding default_get directly 
     _defaults = {
-        'state': lambda self,cr,uid,ctx: len(ctx['src_rec_ids']) > 1 and 'multi' or 'single',
-        'rel_model': lambda self,cr,uid,ctx: self.pool.get('ir.model').search(cr,uid,[('model','=',ctx['src_model'])],context=ctx)[0],
-        'rel_model_ref': lambda self,cr,uid,ctx: ctx['active_id'],
-        'to': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_to', ctx),
-        'cc': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_cc', ctx),
-        'bcc': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_bcc', ctx),
-        'subject':lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_subject', ctx),
-        'body_text':lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_body_text', ctx),
-        'body_html':lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_body_html', ctx),
-        'report': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'file_name', ctx),
-        'signature': lambda self,cr,uid,ctx: self._get_template(cr, uid, ctx).use_sign,
-        'ref_template':lambda self,cr,uid,ctx: self._get_template(cr, uid, ctx).id,
-        'reply_to': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'reply_to', ctx),
-        'reply_to': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'reply_to', ctx),
-        'requested':lambda self,cr,uid,ctx: len(ctx['src_rec_ids']),
-        'full_success': False,
-        'attachment_ids': [], 
     }
 
-    def fields_get(self, cr, uid, fields=None, context=None, write_access=True):
-        if context is None:
-            context = {}
-        result = super(email_template_send_wizard, self).fields_get(cr, uid, fields, context, write_access)
-        if 'attachment_ids' in result and 'src_model' in context:
-            result['attachment_ids']['domain'] = [('res_model','=',context['src_model']),('res_id','=',context['active_id'])]
-        return result
-
-    def sav_to_drafts(self, cr, uid, ids, context=None):
-        if context is None:
-            context = {}
-        mailid = self.save_to_mailbox(cr, uid, ids, context=context)
-        if self.pool.get('email_template.mailbox').write(cr, uid, mailid, {'folder':'drafts'}, context):
-            return {'type':'ir.actions.act_window_close' }
+    #def fields_get(self, cr, uid, fields=None, context=None, write_access=True):
+    #    if context is None:
+    #        context = {}
+    #    result = super(email_template_send_wizard, self).fields_get(cr, uid, fields, context, write_access)
+    #    if 'attachment_ids' in result and 'src_model' in context:
+    #        result['attachment_ids']['domain'] = [('res_model','=',context['src_model']),('res_id','=',context['active_id'])]
+    #    return result
 
     def send_mail(self, cr, uid, ids, context=None):
         if context is None:
             context = {}
         mailid = self.save_to_mailbox(cr, uid, ids, context)
-        if self.pool.get('email_template.mailbox').write(cr, uid, mailid, {'folder':'outbox'}, context):
-            return {'type':'ir.actions.act_window_close' }
+        return {'type': 'ir.actions.act_window_close'}
 
     def get_generated(self, cr, uid, ids=None, context=None):
         if ids is None:
@@ -187,11 +140,10 @@
         if context is None:
             context = {}
         logger = netsvc.Logger()
-        if context['src_rec_ids'] and len(context['src_rec_ids'])>1:
+        if context.get('src_rec_ids'):
             #Means there are multiple items selected for email.
             mail_ids = self.save_to_mailbox(cr, uid, ids, context)
             if mail_ids:
-                self.pool.get('email_template.mailbox').write(cr, uid, mail_ids, {'folder':'outbox'}, context)
                 logger.notifyChannel("email-template", netsvc.LOG_INFO, _("Emails for multiple items saved in outbox."))
                 self.write(cr, uid, ids, {
                     'generated':len(mail_ids),
@@ -200,80 +152,21 @@
             else:
                 raise osv.except_osv(_("Email Template"),_("Email sending failed for one or more objects."))
         return True
-     
+
     def save_to_mailbox(self, cr, uid, ids, context=None):
-        def get_end_value(id, value):
-            if len(context['src_rec_ids']) > 1: # Multiple Mail: Gets value from the template
-                return self.get_value(cr, uid, template, value, context, id)
-            else:
-                return value
-
-        if context is None:
-            context = {}
-        mail_ids = []
-        template = self._get_template(cr, uid, context)
-        for id in context['src_rec_ids']:
-            screen_vals = self.read(cr, uid, ids[0], [],context)
-            account = self.pool.get('email_template.account').read(cr, uid, screen_vals['from'], context=context)
-            vals = {
-                'email_from': tools.ustr(account['name']) + "<" + tools.ustr(account['email_id']) + ">",
-                'email_to': get_end_value(id, screen_vals['to']),
-                'email_cc': get_end_value(id, screen_vals['cc']),
-                'email_bcc': get_end_value(id, screen_vals['bcc']),
-                'subject': get_end_value(id, screen_vals['subject']),
-                'body_text': get_end_value(id, screen_vals['body_text']),
-                'body_html': get_end_value(id, screen_vals['body_html']),
-                'account_id': screen_vals['from'],
-                'state':'na',
-                'mail_type':'multipart/alternative' #Options:'multipart/mixed','multipart/alternative','text/plain','text/html'
-            }
-            if screen_vals['signature']:
-                signature = self.pool.get('res.users').read(cr, uid, uid, ['signature'], context)['signature']
-                if signature:
-                    vals['body_text'] = tools.ustr(vals['body_text'] or '') + signature
-                    vals['body_html'] = tools.ustr(vals['body_html'] or '') + signature
-
-            attachment_ids = []
-
-            #Create partly the mail and later update attachments
-            mail_id = self.pool.get('email_template.mailbox').create(cr, uid, vals, context)
-            mail_ids.append(mail_id)
-            if template.report_template:
-                reportname = 'report.' + self.pool.get('ir.actions.report.xml').read(cr, uid, template.report_template.id, ['report_name'], context)['report_name']
-                data = {}
-                data['model'] = self.pool.get('ir.model').browse(cr, uid, screen_vals['rel_model'], context).model
-
-                # Ensure report is rendered using template's language
-                ctx = context.copy()
-                if template.lang:
-                    ctx['lang'] = self.get_value(cr, uid, template, template.lang, context, id)
-                service = netsvc.LocalService(reportname)
-                (result, format) = service.create(cr, uid, [id], data, ctx)
-                attachment_id = self.pool.get('ir.attachment').create(cr, uid, {
-                    'name': _('%s (Email Attachment)') % tools.ustr(vals['subject']),
-                    'datas': base64.b64encode(result),
-                    'datas_fname': tools.ustr(get_end_value(id, screen_vals['report']) or _('Report')) + "." + format,
-                    'description': vals['body_text'] or _("No Description"),
-                    'res_model': 'email_template.mailbox',
-                    'res_id': mail_id
-                }, context)
-                attachment_ids.append( attachment_id )
-
-            # Add document attachments
-            for attachment_id in screen_vals.get('attachment_ids',[]):
-                new_id = self.pool.get('ir.attachment').copy(cr, uid, attachment_id, {
-                    'res_model': 'email_template.mailbox',
-                    'res_id': mail_id,
-                }, context)
-                attachment_ids.append( new_id )
-
-            if attachment_ids:
-                self.pool.get('email_template.mailbox').write(cr, uid, mail_id, {
-                    'attachments_ids': [[6, 0, attachment_ids]],
-                    'mail_type': 'multipart/mixed'
-                }, context)
-
-        return mail_ids
+        #def get_end_value(id, value):
+        #    if len(context['src_rec_ids']) > 1: # Multiple Mail: Gets value from the template
+        #        return self.get_value(cr, uid, template, value, context, id)
+        #    else:
+        #        return value
+
+        email_ids = []
+        for template in self.browse(cr, uid, ids, context=context):
+            for record_id in context.get('src_rec_ids',[]):
+                email_id = self.generate_email(cr, uid, template.id, record_id, context)
+                email_ids.append(email_id)
+        return email_ids
+
 email_template_send_wizard()
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'email_template/wizard/email_template_send_wizard_view.xml'
--- email_template/wizard/email_template_send_wizard_view.xml	2011-01-14 00:11:01 +0000
+++ email_template/wizard/email_template_send_wizard_view.xml	2011-04-11 12:37:32 +0000
@@ -2,62 +2,61 @@
 <openerp>
 	<data>
 
-		<record model="ir.ui.view" id="email_template_send_wizard_form">
-			<field name="name">email_template.send.wizard.form</field>
-			<field name="model">email_template.send.wizard</field>
-			<field name="type">form</field>
-			<field name="arch" type="xml">
-				<form string="Send mail Wizard">
-					<group col="4" colspan="4">
-						<field name="rel_model" colspan="2" />
-						<field name="from" required="1" colspan="2" />
-					</group>
-					<group col="4" colspan="4">
-						<group col="6" colspan="4">
-							<field name="to" select="1" colspan="4" />
-							<newline />
-							<field name="cc" select="2" colspan="4" />
-							<newline />
-							<field name="bcc" select="2" colspan="4" />
-							<newline />
-							<field name="subject" select="2" colspan="4" attrs="{'required':[('state','=','single')]}" />
-							<newline />
-							<field name="report" colspan="4" />
-						</group>
-						<separator string="" colspan="4" />
-						<notebook colspan="4">
-							<page string="Body (Plain Text)">
-								<field name="body_text" select="2" colspan="4" nolabel="1" />
-							</page>
-							<page string="Body (HTML)">
-								<field name="body_html" select="2" colspan="4" nolabel="1" />
-							</page>
-							<page string="Attachments">
-								<label string="Add here all attachments of the current document you want to include in the Email." colspan="4"/>
-								<field name="attachment_ids" colspan="4" nolabel="1"/>
-							</page>
-						</notebook>
-						<field name="signature" colspan="4" />
-					</group>
-					<group col="4" colspan="4" attrs="{'invisible':[('state','!=','single')]}">
-						<button icon="gtk-apply" name="sav_to_drafts" string="Save in Drafts" type="object" />
-						<button icon="gtk-ok" name="send_mail" string="Send now" type="object" />
-						<button icon="gtk-cancel" special="cancel" string="Discard Mail" />
-					</group>
-					<group col="4" colspan="4" attrs="{'invisible':[('state','=','single')]}">
-					    <label string="Tip: Multiple emails are sent in the same language (the first one is proposed). We suggest you send emails in groups according to language." colspan="4"/>
-						<field name="requested" />
-						<field name="generated" />
-						<button icon="gtk-ok" name="get_generated" string="Send all mails" type="object" states="multi" colspan="2" />
-						<button icon="gtk-cancel" special="cancel" string="Discard Mail" colspan="2" states="multi" />
-					</group>
-					<button icon="gtk-ok" special="cancel" string="Close" colspan="4" states="done" />
-					<field name="state" />
-					<newline />
-					<label string="After clicking send all mails, mails will be sent to outbox and cleared in next Send/Recieve" colspan="4"/>
-				</form>
-			</field>
-		</record>
+        <record model="ir.ui.view" id="email_template_send_wizard_form">
+            <field name="name">email_template.send.wizard.form</field>
+            <field name="model">email_template.send.wizard</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Send Email Wizard">
+                    <group col="4" colspan="4">
+                        <field name="smtp_server_id" widget="selection" required="1" colspan="4"/>
+                    </group>
+                    <group col="4" colspan="4">
+                        <group col="6" colspan="4">
+                            <field name="email_to" colspan="4"/>
+                            <field name="email_cc" colspan="4"/>
+                            <field name="email_bcc" colspan="4"/>
+                            <field name="subject" colspan="4" attrs="{'required':[('state','=','single')]}"/>
+                            <field name="report_name" colspan="4"/>
+                            <field name="user_signature" colspan="4"/>
+                        </group>
+                        <separator string="" colspan="4"/>
+                        <notebook colspan="4">
+                            <page string="Body">
+                                <field name="description" colspan="4" nolabel="1"/>
+                            </page>
+                            <!-- <page string="Body (HTML)">
+                                <field name="body_html" colspan="4" nolabel="1"/>
+                            </page> -->
+                            <page string="Attachments">
+                                <label string="Add here all attachments of the current document you want to include in the Email." colspan="4"/>
+                                <field name="attachment_ids" colspan="4" nolabel="1"/>
+                            </page>
+                        </notebook>
+
+                    </group>
+                    <field name="state" invisible="1"/>
+                    <group col="4" colspan="4" attrs="{'invisible':[('state','=','single')]}">
+                        <field name="requested"/>
+                        <field name="generated"/>
+                        <newline/>
+                        <label string="After clicking Send All, emails will be sent to outbox and cleared in next Send/Recieve" colspan="4"/>
+                        <label string="Tip: Multiple emails are sent in the same language (the first one is proposed). We suggest you send emails in groups according to language." colspan="4"/>
+                        <newline/>
+                    </group>
+                    <label string="" colspan="2"/>
+                    <group col="4" colspan="2" attrs="{'invisible':[('state','=','single')]}">
+                        <button icon="gtk-cancel" special="cancel" string="Discard"  states="multi"/>
+                        <button icon="gtk-ok" name="get_generated" string="Send All" type="object" states="multi"/>
+                        <button icon="gtk-ok" special="cancel" string="Close" states="done"/>
+                    </group>
+                    <group col="4" colspan="2" attrs="{'invisible':[('state','!=','single')]}">
+                        <button icon="gtk-cancel" special="cancel" string="Discard"/>
+                        <button icon="gtk-ok" name="send_mail" string="Send now" type="object"/>
+                    </group>
+                </form>
+            </field>
+        </record>
+
 	</data>
 </openerp>
-

=== modified file 'event/__openerp__.py'
--- event/__openerp__.py	2011-03-18 14:06:18 +0000
+++ event/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -38,7 +38,7 @@
         Association / Configuration / Types of Events
 """,
     'author': 'OpenERP SA',
-    'depends': ['crm', 'base_contact', 'account', 'marketing'],
+    'depends': ['crm', 'base_contact', 'account', 'marketing', 'mail'],
     'init_xml': [],
     'update_xml': [
         'security/event_security.xml',

=== modified file 'event/event.py'
--- event/event.py	2011-03-02 17:49:20 +0000
+++ event/event.py	2011-04-11 12:37:32 +0000
@@ -24,8 +24,9 @@
 from crm import crm
 from osv import fields, osv
 from tools.translate import _
-import tools
 import decimal_precision as dp
+from crm import wizard
+
 
 class event_type(osv.osv):
     """ Event Type """
@@ -280,7 +281,7 @@
     """Event Registration"""
     _name= 'event.registration'
     _description = __doc__
-    _inherit = 'mailgate.thread'
+    _inherit = 'email.thread'
 
     def _amount_line(self, cr, uid, ids, field_name, arg, context=None):
         cur_obj = self.pool.get('res.currency')
@@ -313,8 +314,8 @@
         'create_date': fields.datetime('Creation Date', readonly=True),
         'write_date': fields.datetime('Write Date', readonly=True),
         'description': fields.text('Description', states={'done': [('readonly', True)]}),
-        'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
-        'log_ids': fields.one2many('mailgate.message', 'res_id', 'Logs', domain=[('history', '=', False),('model','=',_name)]),
+        'message_ids': fields.one2many('email.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
+        'log_ids': fields.one2many('email.message', 'res_id', 'Logs', domain=[('history', '=', False),('model','=',_name)]),
         'date_deadline': fields.related('event_id','date_end', type='datetime', string="End Date", readonly=True),
         'date': fields.related('event_id', 'date_begin', type='datetime', string="Start Date", readonly=True),
         'user_id': fields.many2one('res.users', 'Responsible', states={'done': [('readonly', True)]}),
@@ -521,7 +522,7 @@
         """
         Send email to user
         """
-
+        email_message_obj = self.pool.get('email.message')
         for regestration in self.browse(cr, uid, ids, context=context):
             src = regestration.event_id.reply_to or False
             email_to = []
@@ -545,7 +546,7 @@
                     subject = _('Auto Confirmation: [%s] %s') %(regestration.id, regestration.name)
                     body = regestration.event_id.mail_confirm
             if subject or body:
-                tools.email_send(src, email_to, subject, body, email_cc=email_cc, openobject_id=regestration.id)
+                email_message_obj.schedule_with_attach(cr, uid, src, email_to, subject, body, model='event.registration', email_cc=email_cc, openobject_id=regestration.id)
                 self.history(cr, uid, [regestration], subject, history = True, \
                         email=email_to, details=body, \
                         subject=subject, email_from=src, \

=== modified file 'event/event_view.xml'
--- event/event_view.xml	2011-01-27 09:49:39 +0000
+++ event/event_view.xml	2011-04-11 12:37:32 +0000
@@ -391,11 +391,11 @@
                                 <tree string="History">
                                     <field name="display_text" string="History Information"/>
                                     <field name="history" invisible="1"/>
-                                    <button
-                                        string="Reply" attrs="{'invisible': [('history', '!=', True)]}"
-                                        name="%(crm.action_crm_send_mail)d"
-                                        context="{'mail':'reply', 'model': 'event.registration', 'include_original' : True}"
-                                        icon="terp-mail-replied" type="action" />
+		                            <button
+		                                string="Reply" attrs="{'invisible': [('history', '!=', True)]}"
+		                                name="%(mail.action_email_compose_message_wizard)d"
+		                                context="{'mail':'reply', 'message_id':active_id}"
+		                                icon="terp-mail-replied" type="action" />
                                 </tree>
                                 <form string="History">
                                     <group col="4" colspan="4">
@@ -403,18 +403,17 @@
                                         <field name="date"/>
                                         <field name="email_to" widget="char" size="512"/>
                                         <field name="email_cc" widget="char" size="512"/>
-                                        <field name="name" colspan="4" widget="char" size="512"/>
+                                        <field name="subject" colspan="4" widget="char" size="512"/>
                                         <field name="history" invisible="1"/>
                                     </group>
                                     <notebook colspan="4">
                                         <page string="Details">
                                             <group attrs="{'invisible': [('history', '!=', True)]}">
-                                                <field name="description" colspan="4" nolabel="1" height="250"/>
-                                                <button colspan="4"
-                                                    string="Reply"
-                                                    name="%(crm.action_crm_send_mail)d"
-                                                    context="{'mail':'reply', 'model': 'event.registration', 'include_original' : True}"
-                                                    icon="terp-mail-replied" type="action" />
+                                                <field name="body" colspan="4" nolabel="1" height="250"/>
+		                                        <button colspan="4" string="Reply"
+		                                            name="%(mail.action_email_compose_message_wizard)d"
+		                                            context="{'mail':'reply', 'message_id':active_id}"
+		                                            icon="terp-mail-replied" type="action"/>
                                             </group>
                                             <group attrs="{'invisible': [('history', '=', True)]}">
                                                 <field name="display_text" colspan="4" nolabel="1"  height="250"/>
@@ -426,14 +425,14 @@
                                     </notebook>
                                 </form>
                             </field>
-                             <button string="Add Internal Note"
+                            <button string="Add Internal Note"
                                 name="%(crm.action_crm_add_note)d"
                                 context="{'model': 'crm.lead' }"
                                 icon="terp-document-new" type="action" />
-                              <button string="Send New Email"
-                                name="%(crm.action_crm_send_mail)d"
-                                context="{'mail':'new', 'model': 'event.registration'}"
-                                icon="terp-mail-message-new" type="action" />
+		                    <button string="Send New Email"
+		                        name="%(mail.action_email_compose_message_wizard)d"
+		                        context="{'email_model':'event.registration', 'email_res_id': active_id}"
+		                        icon="terp-mail-message-new" type="action"/>
                            </page>
                     </notebook>
                 </form>

=== modified file 'event/wizard/__init__.py'
--- event/wizard/__init__.py	2011-01-14 00:11:01 +0000
+++ event/wizard/__init__.py	2011-04-11 12:37:32 +0000
@@ -23,4 +23,5 @@
 import event_confirm_registration
 import event_confirm
 import partner_event_registration
+import email_compose_message
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'event/wizard/email_compose_message.py'
--- event/wizard/email_compose_message.py	1970-01-01 00:00:00 +0000
+++ event/wizard/email_compose_message.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,49 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+from osv import osv
+from osv import fields
+import tools
+
+class email_compose_message(osv.osv_memory):
+    _inherit = 'email.compose.message'
+
+    def get_value(self, cr, uid, model, resource_id, context=None):
+        if context is None:
+            context = {}
+        result = super(email_compose_message, self).get_value(cr, uid,  model, resource_id, context=context)
+        if model == 'event.registration' and resource_id:
+            model_obj = self.pool.get(model)
+            data = model_obj.browse(cr, uid , resource_id, context)
+            result.update({
+                    'subject' : data.event_id.name or False,
+                    'email_to' : data.email_from or False,
+                    'email_from' : data.user_id and data.user_id.address_id and data.user_id.address_id.email or False,
+                    'body' : '\n' + (tools.ustr(data.user_id.signature or '')),
+                    'email_cc' : tools.ustr(data.email_cc or ''),
+                    'model': model  or False,
+                    'res_id': resource_id  or False,
+                })
+        return result
+
+email_compose_message()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'fetchmail/__openerp__.py'
--- fetchmail/__openerp__.py	2011-03-18 14:06:18 +0000
+++ fetchmail/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -24,7 +24,7 @@
 {
     "name" : "Fetchmail Server",
     "version" : "1.0",
-    "depends" : ["base", 'mail_gateway'],
+    "depends" : ["base", 'mail'],
     "author" : "OpenERP SA",
     "category": 'Tools',
     "description": """

=== modified file 'fetchmail/fetchmail.py'
--- fetchmail/fetchmail.py	2011-02-15 12:50:01 +0000
+++ fetchmail/fetchmail.py	2011-04-11 12:37:32 +0000
@@ -62,7 +62,7 @@
         'object_id': fields.many2one('ir.model', "Model", required=True, help="OpenObject Model. Generates a record of this model.\nSelect Object with message_new attrbutes."),
         'priority': fields.integer('Server Priority', readonly=True, states={'draft':[('readonly', False)]}, help="Priority between 0 to 10, select define the order of Processing"),
         'user_id':fields.many2one('res.users', 'User', required=False),
-        'message_ids': fields.one2many('mailgate.message', 'server_id', 'Messages', readonly=True),
+        'message_ids': fields.one2many('email.message', 'server_id', 'Messages', readonly=True),
     }
     _defaults = {
         'state': lambda *a: "draft",
@@ -82,21 +82,9 @@
                 return False
         return True
 
-    def check_model(self, cr, uid, ids, context = None):
-        if context is None:
-            context = {}
-        current_rec = self.read(cr, uid, ids, context)
-        if current_rec:
-            current_rec = current_rec[0]
-            model_name = self.pool.get('ir.model').browse(cr, uid, current_rec.get('object_id')[0]).model
-            model = self.pool.get(model_name)
-            if hasattr(model, 'message_new'):
-                return True
-        return False
-
     _constraints = [
         (check_duplicate, 'Warning! Can\'t have duplicate server configuration!', ['user', 'password']),
-        (check_model, 'Warning! Record for selected Model can not be created\nPlease choose valid Model', ['object_id'])
+        
     ]
 
     def onchange_server_type(self, cr, uid, ids, server_type=False, ssl=False):
@@ -111,13 +99,13 @@
     def set_draft(self, cr, uid, ids, context=None):
         self.write(cr, uid, ids , {'state':'draft'})
         return True
-    
+
     def button_confirm_login(self, cr, uid, ids, context=None):
         if context is None:
             context = {}
         for server in self.browse(cr, uid, ids, context=context):
-            logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail start checking for new emails on %s' % (server.name))
-            context.update({'server_id': server.id, 'server_type': server.type})
+            logger.notifyChannel(server.type, netsvc.LOG_INFO, 'fetchmail start checking for new emails on %s' % (server.name))
+            context.update({'server_id': server.id})
             try:
                 if server.type == 'imap':
                     imap_server = None
@@ -128,7 +116,7 @@
 
                     imap_server.login(server.user, server.password)
                     ret_server = imap_server
-                    
+
                 elif server.type == 'pop':
                     pop_server = None
                     if server.is_ssl:
@@ -141,12 +129,12 @@
                     pop_server.user(server.user)
                     pop_server.pass_(server.password)
                     ret_server = pop_server
-                    
+
                 self.write(cr, uid, [server.id], {'state':'done'})
                 if context.get('get_server',False):
                     return ret_server
             except Exception, e:
-                logger.notifyChannel(server.type, netsvc.LOG_WARNING, '%s' % (e))
+                logger.notifyChannel(server.type, netsvc.LOG_ERROR, '%s' % (e))
         return True
 
     def button_fetch_mail(self, cr, uid, ids, context=None):
@@ -161,90 +149,82 @@
     def fetch_mail(self, cr, uid, ids, context=None):
         if context is None:
             context = {}
-        email_tool = self.pool.get('email.server.tools')
+        thread_pool = self.pool.get('email.thread')
         action_pool = self.pool.get('ir.actions.server')
         context.update({'get_server': True})
         for server in self.browse(cr, uid, ids, context=context):
             count = 0
             user = server.user_id.id or uid
-            try:
-                if server.type == 'imap':
+            if server.type == 'imap':
+                try:
                     imap_server = self.button_confirm_login(cr, uid, [server.id], context=context)
                     imap_server.select()
                     result, data = imap_server.search(None, '(UNSEEN)')
                     for num in data[0].split():
                         result, data = imap_server.fetch(num, '(RFC822)')
-                        res_id = email_tool.process_email(cr, user, server.object_id.model, data[0][1], attach=server.attach, context=context)
+                        res_id = thread_pool.process_email(cr, user, server.object_id.model, data[0][1], attach=server.attach, context=context)
                         if res_id and server.action_id:
                             action_pool.run(cr, user, [server.action_id.id], {'active_id': res_id, 'active_ids':[res_id]})
 
                             imap_server.store(num, '+FLAGS', '\\Seen')
                         count += 1
-                    logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail fetch/process %s email(s) from %s' % (count, server.name))
+                    logger.notifyChannel(server.type, netsvc.LOG_INFO, 'fetchmail fetch/process %s email(s) from %s' % (count, server.name))
 
-                    imap_server.close()
-                    imap_server.logout()
-                elif server.type == 'pop':
+                except Exception, e:
+                    logger.notifyChannel(server.type, netsvc.LOG_ERROR, '%s' % (tools.ustr(e)))
+                finally:
+                    if imap_server:
+                        imap_server.close()
+                        imap_server.logout()
+            elif server.type == 'pop':
+                try:
                     pop_server = self.button_confirm_login(cr, uid, [server.id], context=context)
+                    (numMsgs, totalSize) = pop_server.stat()
                     pop_server.list()
-                    (numMsgs, totalSize) = pop_server.stat()
                     for num in range(1, numMsgs + 1):
                         (header, msges, octets) = pop_server.retr(num)
                         msg = '\n'.join(msges)
-                        res_id = email_tool.process_email(cr, user, server.object_id.model, msg, attach=server.attach, context=context)
+                        res_id = thread_pool.process_email(cr, user, server.object_id.model, msg, attach=server.attach, context=context)
                         if res_id and server.action_id:
                             action_pool.run(cr, user, [server.action_id.id], {'active_id': res_id, 'active_ids':[res_id]})
 
                         pop_server.dele(num)
-
-                    pop_server.quit()
-
-                    logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail fetch %s email(s) from %s' % (numMsgs, server.name))
-
-            except Exception, e:
-                logger.notifyChannel(server.type, netsvc.LOG_WARNING, '%s' % (tools.ustr(e)))
-
+                    logger.notifyChannel(server.type, netsvc.LOG_INFO, 'fetchmail fetch %s email(s) from %s' % (numMsgs, server.name))
+                except Exception, e:
+                    logger.notifyChannel(server.type, netsvc.LOG_ERROR, '%s' % (tools.ustr(e)))
+                finally:
+                    if pop_server:
+                        pop_server.quit()
         return True
 
 email_server()
 
-class mailgate_message(osv.osv):
+class email_message(osv.osv):
 
-    _inherit = "mailgate.message"
+    _inherit = "email.message"
 
     _columns = {
         'server_id': fields.many2one('email.server', "Mail Server", readonly=True, select=True),
-        'server_type':fields.selection([
-            ('pop', 'POP Server'),
-            ('imap', 'IMAP Server'),
-        ], 'Server Type', select=True, readonly=True),
     }
-    _order = 'id desc'
 
     def create(self, cr, uid, values, context=None):
         if context is None:
             context={}
         server_id = context.get('server_id',False)
-        server_type = context.get('server_type',False)
         if server_id:
             values['server_id'] = server_id
-        if server_type:
-            values['server_type'] = server_type
-        res = super(mailgate_message,self).create(cr, uid, values, context=context)
+        res = super(email_message,self).create(cr, uid, values, context=context)
         return res
 
     def write(self, cr, uid, ids, values, context=None):
         if context is None:
             context={}
         server_id = context.get('server_id',False)
-        server_type = context.get('server_type',False)
         if server_id:
             values['server_id'] = server_id
-        if server_type:
-            values['server_type'] = server_type
-        res = super(mailgate_message,self).write(cr, uid, ids, values, context=context)
+        res = super(email_message,self).write(cr, uid, ids, values, context=context)
         return res
 
-mailgate_message()
+email_message()
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'fetchmail/fetchmail_view.xml'
--- fetchmail/fetchmail_view.xml	2011-01-27 09:49:39 +0000
+++ fetchmail/fetchmail_view.xml	2011-04-11 12:37:32 +0000
@@ -1,7 +1,7 @@
 <?xml version="1.0"?>
 <openerp>
     <data>
-    
+
         <record model="ir.ui.view" id="view_email_server_tree">
             <field name="name">email.server.tree</field>
             <field name="model">email.server</field>
@@ -18,7 +18,7 @@
                 </tree>
             </field>
         </record>
-        
+
         <record model="ir.ui.view" id="view_email_server_form">
             <field name="name">email.server.form</field>
             <field name="model">email.server</field>
@@ -68,7 +68,7 @@
                 </form>
             </field>
         </record>
-        
+
         <record model="ir.ui.view" id="view_email_server_search">
             <field name="name">email.server.search</field>
             <field name="model">email.server</field>
@@ -91,9 +91,9 @@
                 </search>
             </field>
         </record>
-        
+
         <record model="ir.actions.act_window" id="action_email_server_tree">
-            <field name="name">Email Servers</field>
+            <field name="name">Incomming Servers</field>
             <field name="res_model">email.server</field>
             <field name="view_type">form</field>
             <field name="view_mode">tree,form</field>
@@ -101,58 +101,50 @@
             <field name="search_view_id" ref="view_email_server_search"/>
         </record>
 
-        <menuitem 
-            parent="base.menu_mail_gateway" 
+        <menuitem
+            parent="mail.menu_config_email"
             id="menu_action_fetchmail_server_tree"
-            action="action_email_server_tree" 
-            name="Email Servers"
+            action="action_email_server_tree"
+            name="Incomming Servers"
         />
-        
-      <record model="ir.ui.view" id="mailgate_message_tree_view">
-	      <field name="name">mailgate.message.tree</field>
-	      <field name="model">mailgate.message</field>
+
+      <record model="ir.ui.view" id="email_message_tree_view">
+	      <field name="name">email.message.tree</field>
+	      <field name="model">email.message</field>
 	      <field name="type">tree</field>
-	      <field name="inherit_id" ref="mail_gateway.view_mailgate_message_tree"/>
+	      <field name="inherit_id" ref="mail.view_email_message_tree"/>
 	      <field name="arch" type="xml">
 	          <field name="user_id" position="after">
 	              <field name="server_id" select="1"/>
-	              <field name="ref_id"/>
 	          </field>
 	      </field>
       </record>
-      
-      <record model="ir.ui.view" id="mailgate_message_search_view">
-	      <field name="name">mailgate.message.inherit.search</field>
-	      <field name="model">mailgate.message</field>
+
+      <record model="ir.ui.view" id="email_message_search_view">
+	      <field name="name">email.message.inherit.search</field>
+	      <field name="model">email.message</field>
 	      <field name="type">search</field>
-	      <field name="inherit_id" ref="mail_gateway.view_mailgate_message_search"/>
+	      <field name="inherit_id" ref="mail.view_email_message_search"/>
 	      <field name="arch" type="xml">
-			<xpath expr="/search/field[@name='name']" position="before">
-			     <filter string="Emails" name="emails" domain="[('server_id','!=',False)]" icon="terp-mail-message-new"/>
-			     <separator orientation="vertical"/>
-			</xpath>
 			<xpath expr="/search/group/filter[@string='Thread']" position="before">
                 <filter string="Mail Server" icon="terp-accessories-archiver" domain="[]" context="{'group_by':'server_id'}"/>
             </xpath>
 	      </field>
       </record>
-      
+
       <record id="action_view_mail_message_emails" model="ir.actions.act_window">
             <field name="name">Messages</field>
-            <field name="res_model">mailgate.message</field>
+            <field name="res_model">email.message</field>
             <field name="view_type">form</field>
             <field name="view_mode">tree,form</field>
             <field name="context">{'search_default_emails': 1}</field>
-            <field name="search_view_id" ref="mailgate_message_search_view"/>
+            <field name="search_view_id" ref="email_message_search_view"/>
       </record>
-        
-      <menuitem id="base.menu_email_gateway_form"
-               parent="base.menu_mail_gateway" action="action_view_mail_message_emails" />
 
-      <act_window 
+      <act_window
             context="{'search_default_server_id': [active_id], 'default_server_id': active_id}"
-            id="act_server_history" name="Emails"
-            res_model="mailgate.message" src_model="email.server"/>
-            
+            id="act_server_history" name="Messages" domain="[('history', '=', True), ('server_id', '=', active_id)]"
+            res_model="email.message" src_model="email.server"/>
+
     </data>
 </openerp>

=== modified file 'hr_evaluation/__openerp__.py'
--- hr_evaluation/__openerp__.py	2011-03-18 14:06:18 +0000
+++ hr_evaluation/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -44,7 +44,7 @@
     "update_xml": [
         "security/ir.model.access.csv",
         "security/hr_evaluation_security.xml",
-        "wizard/hr_evaluation_mail_view.xml",
+#        "wizard/hr_evaluation_mail_view.xml",
         "hr_evaluation_view.xml",
         "report/hr_evaluation_report_view.xml",
         "board_hr_evaluation_view.xml"

=== modified file 'hr_evaluation/hr_evaluation.py'
--- hr_evaluation/hr_evaluation.py	2011-02-17 05:26:05 +0000
+++ hr_evaluation/hr_evaluation.py	2011-04-11 12:37:32 +0000
@@ -24,7 +24,6 @@
 from dateutil.relativedelta import relativedelta
 from dateutil import parser
 from osv import fields, osv
-import tools
 from tools.translate import _
 
 class hr_evaluation_plan(osv.osv):
@@ -193,6 +192,7 @@
         return {'value': {'plan_id':evaluation_plan_id}}
 
     def button_plan_in_progress(self, cr, uid, ids, context=None):
+        email_message_obj = self.pool.get('email.message')
         hr_eval_inter_obj = self.pool.get('hr.evaluation.interview')
         if context is None:
             context = {}
@@ -229,7 +229,7 @@
                         sub = phase.email_subject
                         dest = [child.work_email]
                         if dest:
-                           tools.email_send(evaluation.employee_id.work_email, dest, sub, body)
+                           email_message_obj.schedule_with_attach(cr, uid, evaluation.employee_id.work_email, dest, sub, body, model='hr_evaluation.evaluation')
 
         self.write(cr, uid, ids, {'state':'wait'}, context=context)
         return True

=== modified file 'hr_evaluation/hr_evaluation_view.xml'
--- hr_evaluation/hr_evaluation_view.xml	2011-03-09 13:03:45 +0000
+++ hr_evaluation/hr_evaluation_view.xml	2011-04-11 12:37:32 +0000
@@ -401,10 +401,23 @@
             <field name="act_window_id" ref="action_hr_evaluation_interview_tree"/>
         </record>
 
+
+        <!-- Email Compose message Action-->
+        <record id="action_email_compose_message_menu_wizard" model="ir.actions.act_window">
+            <field name="name">Send E-mail</field>
+            <field name="res_model">email.compose.message</field>
+            <field name="src_model">email.compose.message</field>
+            <field name="type">ir.actions.act_window</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">form</field>
+            <field name="context">{'email_model':'hr.evaluation.interview'}</field>
+            <field name="target">new</field>
+        </record>
+
         <menuitem name="Interview Requests" parent="menu_eval_hr" id="menu_open_hr_evaluation_interview_requests"
             action="action_hr_evaluation_interview_tree"/>
         <menuitem name="Evaluation Reminders" parent="menu_eval_hr" id="menu_eval_send_mail"
-           action="action_hr_evaluation_send_mail" sequence="45" groups="base.group_hr_manager"/>
+           action="action_email_compose_message_menu_wizard" sequence="45" groups="base.group_hr_manager"/>
 
       <!-- Evaluation Interviews Button on Employee Form -->
       <act_window

=== modified file 'hr_evaluation/wizard/__init__.py'
--- hr_evaluation/wizard/__init__.py	2011-01-14 00:11:01 +0000
+++ hr_evaluation/wizard/__init__.py	2011-04-11 12:37:32 +0000
@@ -19,6 +19,7 @@
 #
 ##############################################################################
 
-import hr_evaluation_mail
+#import hr_evaluation_mail
+import email_compose_message
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'hr_evaluation/wizard/email_compose_message.py'
--- hr_evaluation/wizard/email_compose_message.py	1970-01-01 00:00:00 +0000
+++ hr_evaluation/wizard/email_compose_message.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,53 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+from osv import osv
+from osv import fields
+import tools
+from tools.translate import _
+
+class email_compose_message(osv.osv_memory):
+    _inherit = 'email.compose.message'
+
+    def get_value(self, cr, uid, model, resource_id, context=None):
+        if context is None:
+            context = {}
+        result = super(email_compose_message, self).get_value(cr, uid,  model, resource_id, context=context)
+        if model == 'hr.evaluation.interview' and resource_id:
+            model_pool = self.pool.get(model)
+            record_data = model_pool.browse(cr, uid, resource_id, context)
+            if record_data.state == "waiting_answer":
+                msg = _("Hello %s, \n\n Kindly post your response for '%s' survey interview. \n\n Thanks,")  %(record_data.user_to_review_id.name, record_data.survey_id.title)
+                result.update({
+                        'email_from': tools.config.get('email_from',''),
+                        'email_to': record_data.user_to_review_id.work_email or False,
+                        'subject': _("Reminder to fill up Survey"),
+                        'body': msg,
+                        'res_id': resource_id,
+                        'email_cc': False,
+                        'email_bcc': False,
+                        'reply_to': False,
+                    })
+        return result
+
+email_compose_message()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'hr_evaluation/wizard/hr_evaluation_mail.py'
--- hr_evaluation/wizard/hr_evaluation_mail.py	2011-01-14 00:11:01 +0000
+++ hr_evaluation/wizard/hr_evaluation_mail.py	2011-04-11 12:37:32 +0000
@@ -29,13 +29,14 @@
     }
 
     def send_mail(self, cr, uid, ids, context=None):
+        email_message_obj = self.pool.get('email.message')
         hr_evaluation_interview_obj = self.pool.get('hr.evaluation.interview')
         evaluation_data = self.read(cr, uid, ids, context=context)[0]
         current_interview = hr_evaluation_interview_obj.browse(cr, uid, evaluation_data.get('evaluation_id'))
         if current_interview.state == "waiting_answer" and current_interview.user_to_review_id.work_email :
             msg = " Hello %s, \n\n Kindly post your response for '%s' survey interview. \n\n Thanks,"  %(current_interview.user_to_review_id.name, current_interview.survey_id.title)
-            tools.email_send(tools.config['email_from'], [current_interview.user_to_review_id.work_email],\
-                                          'Reminder to fill up Survey', msg)
+            email_message_obj.schedule_with_attach(cr, uid, tools.config['email_from'], [current_interview.user_to_review_id.work_email],\
+                                          'Reminder to fill up Survey', msg, model='hr.evaluation.reminder')
         return {'type': 'ir.actions.act_window_close'}
 
 hr_evaluation_reminder()

=== modified file 'hr_recruitment/hr_recruitment.py'
--- hr_recruitment/hr_recruitment.py	2011-03-11 17:20:00 +0000
+++ hr_recruitment/hr_recruitment.py	2011-04-11 12:37:32 +0000
@@ -29,6 +29,7 @@
 import binascii
 import tools
 from tools.translate import _
+from crm import wizard
 
 AVAILABLE_STATES = [
     ('draft', 'New'),
@@ -79,7 +80,7 @@
     _name = "hr.applicant"
     _description = "Applicant"
     _order = "id desc"
-    _inherit = ['mailgate.thread']
+    _inherit = ['email.thread']
 
     def _compute_day(self, cr, uid, ids, fields, args, context=None):
         """
@@ -117,7 +118,7 @@
 
     _columns = {
         'name': fields.char('Name', size=128, required=True),
-        'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
+        'message_ids': fields.one2many('email.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
         'active': fields.boolean('Active', help="If the active field is set to false, it will allow you to hide the case without removing it."),
         'description': fields.text('Description'),
         'email_from': fields.char('Email', size=128, help="These people will receive email."),
@@ -305,9 +306,7 @@
         @param cr: the current row, from the database cursor,
         @param uid: the current user’s ID for security checks
         """
-        mailgate_pool = self.pool.get('email.server.tools')
-        attach_obj = self.pool.get('ir.attachment')
-
+        thread_pool = self.pool.get('email.thread')
         subject = msg.get('subject')
         body = msg.get('body')
         msg_from = msg.get('from')
@@ -323,26 +322,12 @@
         if msg.get('priority', False):
             vals['priority'] = priority
 
-        res = mailgate_pool.get_partner(cr, uid, msg.get('from'))
+        res = thread_pool.get_partner(cr, uid, msg.get('from'))
         if res:
             vals.update(res)
-        res = self.create(cr, uid, vals, context=context)
-
-        attachents = msg.get('attachments', [])
-        for attactment in attachents or []:
-            data_attach = {
-                'name': attactment,
-                'datas':binascii.b2a_base64(str(attachents.get(attactment))),
-                'datas_fname': attactment,
-                'description': 'Mail attachment',
-                'res_model': self._name,
-                'res_id': res,
-            }
-            attach_obj.create(cr, uid, data_attach, context=context)
-
-        return res
-
-    def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context=None):
+        return self.create(cr, uid, vals, context=context)
+
+    def message_update(self, cr, uid, ids, msg, vals={}, default_act='pending', context=None):
         """
         @param self: The object pointer
         @param cr: the current row, from the database cursor,
@@ -377,17 +362,6 @@
         res = self.write(cr, uid, ids, vals, context=context)
         return res
 
-    def msg_send(self, cr, uid, id, *args, **argv):
-        """ Send The Message
-            @param self: The object pointer
-            @param cr: the current row, from the database cursor,
-            @param uid: the current user’s ID for security checks,
-            @param ids: List of email’s IDs
-            @param *args: Return Tuple Value
-            @param **args: Return Dictionary of Keyword Value
-        """
-        return True
-
     def case_open(self, cr, uid, ids, *args):
         """
         @param self: The object pointer

=== modified file 'hr_recruitment/hr_recruitment_view.xml'
--- hr_recruitment/hr_recruitment_view.xml	2011-03-11 17:29:52 +0000
+++ hr_recruitment/hr_recruitment_view.xml	2011-04-11 12:37:32 +0000
@@ -145,11 +145,11 @@
                             <tree string="History">
                                 <field name="display_text" string="History Information"/>
                                 <field name="history" invisible="1"/>
-                                <button
-                                    string="Reply" attrs="{'invisible': [('history', '!=', True)]}"
-                                    name="%(crm.action_crm_send_mail)d"
-                                    context="{'mail':'reply', 'model': 'hr.applicant', 'include_original' : True}"
-                                    icon="terp-mail-replied" type="action" />
+	                            <button
+	                                string="Reply" attrs="{'invisible': [('history', '!=', True)]}"
+	                                name="%(mail.action_email_compose_message_wizard)d"
+	                                context="{'mail':'reply', 'message_id':active_id}"
+	                                icon="terp-mail-replied" type="action" />
                             </tree>
                             <form string="History">
                                 <group col="4" colspan="4">
@@ -157,18 +157,17 @@
                                     <field name="date"/>
                                     <field name="email_to" widget="char" size="512"/>
                                     <field name="email_cc" widget="char" size="512"/>
-                                    <field name="name" colspan="4" widget="char" size="512"/>
+                                    <field name="subject" colspan="4" widget="char" size="512"/>
                                     <field name="history" invisible="1"/>
                                 </group>
                                 <notebook colspan="4">
                                     <page string="Details">
                                         <group attrs="{'invisible': [('history', '!=', True)]}">
-                                            <field name="description" colspan="4" nolabel="1" height="250"/>
-                                            <button colspan="4"
-                                                string="Reply"
-                                                name="%(crm.action_crm_send_mail)d"
-                                                context="{'mail':'reply', 'model': 'hr.applicant', 'include_original' : True}"
-                                                icon="terp-mail-replied" type="action" />
+                                            <field name="body" colspan="4" nolabel="1" height="250"/>
+	                                        <button colspan="4" string="Reply"
+	                                            name="%(mail.action_email_compose_message_wizard)d"
+	                                            context="{'mail':'reply', 'message_id':active_id}"
+	                                            icon="terp-mail-replied" type="action"/>
                                         </group>
                                         <group attrs="{'invisible': [('history', '=', True)]}">
                                             <field name="display_text" colspan="4" nolabel="1"  height="250"/>
@@ -180,14 +179,14 @@
                                 </notebook>
                             </form>
                         </field>
-                         <button string="Add Internal Note"
+                        <button string="Add Internal Note"
                                 name="%(crm.action_crm_add_note)d"
                                 context="{'model': 'crm.lead' }"
                                 icon="terp-document-new" type="action" />
-                        <button string="Send New Email"
-                           name="%(crm.action_crm_send_mail)d"
-                           context="{'mail':'new', 'model': 'hr.applicant'}"
-                           icon="terp-mail-message-new" type="action" />
+	                    <button string="Send New Email"
+	                        name="%(mail.action_email_compose_message_wizard)d"
+	                        context="{'email_model': 'hr.applicant', 'email_res_id': active_id}"
+	                        icon="terp-mail-message-new" type="action"/>
                  </page>
                  <page string="Notes">
                      <field name="description" nolabel="1" colspan="4"/>

=== modified file 'hr_recruitment/security/ir.model.access.csv'
--- hr_recruitment/security/ir.model.access.csv	2011-01-14 00:11:01 +0000
+++ hr_recruitment/security/ir.model.access.csv	2011-04-11 12:37:32 +0000
@@ -3,7 +3,7 @@
 "access_hr_recruitment_report","hr.recruitment.report","model_hr_recruitment_report","base.group_hr_manager",1,1,1,1
 "access_hr_recruitment_stage_user","hr.recruitment.stage.user","model_hr_recruitment_stage","base.group_hr_user",1,1,1,1
 "access_hr_recruitment_degree","hr.recruitment.degree","model_hr_recruitment_degree","base.group_hr_user",1,1,1,1
-"access_mailgate_message_user","mailgate.message.user","mail_gateway.model_mailgate_message","base.group_hr_user",1,1,1,1
+"access_email_message_user","email.message.user","mail.model_email_message","base.group_hr_user",1,1,1,1
 "access_res_partner_hr_user","res.partner.user","base.model_res_partner","base.group_hr_user",1,1,1,1
 "access_res_partner_address_hr_user","res.partner.address.user","base.model_res_partner_address","base.group_hr_user",1,1,1,1
 "access_survey_hr_user","survey.hr.user","survey.model_survey","base.group_hr_user",1,1,1,0

=== modified file 'hr_recruitment/wizard/__init__.py'
--- hr_recruitment/wizard/__init__.py	2011-02-16 12:04:50 +0000
+++ hr_recruitment/wizard/__init__.py	2011-04-11 12:37:32 +0000
@@ -23,6 +23,7 @@
 import hr_recruitment_phonecall
 import hr_recruitment_create_partner_job
 import hr_recruitment_employee_hired
+import email_compose_message
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 

=== added file 'hr_recruitment/wizard/email_compose_message.py'
--- hr_recruitment/wizard/email_compose_message.py	1970-01-01 00:00:00 +0000
+++ hr_recruitment/wizard/email_compose_message.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+from osv import osv
+from osv import fields
+import tools
+
+
+class email_compose_message(osv.osv_memory):
+    _inherit = 'email.compose.message'
+
+    def get_value(self, cr, uid, model, resource_id, context=None):
+        if context is None:
+            context = {}
+        result = super(email_compose_message, self).get_value(cr, uid,  model, resource_id, context=context)
+        if model == 'hr.applicant' and resource_id:
+            model_obj = self.pool.get(model)
+            data = model_obj.browse(cr, uid , resource_id, context)
+            result.update({
+                    'subject' : data.name or False,
+                    'email_to' : data.email_from or False,
+                    'email_from' : data.user_id and data.user_id.address_id and data.user_id.address_id.email or False,
+                    'body' : '\n' + (tools.ustr(data.user_id.signature or '')),
+                    'email_cc' : tools.ustr(data.email_cc or ''),
+                    'model': model  or False,
+                    'res_id': resource_id  or False,
+                })
+        return result
+
+email_compose_message()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'hr_recruitment/wizard/hr_recruitment_phonecall.py'
--- hr_recruitment/wizard/hr_recruitment_phonecall.py	2011-03-14 10:27:42 +0000
+++ hr_recruitment/wizard/hr_recruitment_phonecall.py	2011-04-11 12:37:32 +0000
@@ -32,7 +32,7 @@
         'deadline': fields.datetime('Planned Date'),
         'note': fields.text('Goals'),
         'category_id': fields.many2one('crm.case.categ', 'Category', required=True),
-                }
+    }
 
     def _date_user(self, cr, uid, context=None):
         case_obj = self.pool.get('hr.applicant')
@@ -50,7 +50,7 @@
         return categ_id and categ_id[0] or case.categ_id and case.categ_id.id or False
 
     def _get_note(self, cr, uid, context=None):
-        msg_obj = self.pool.get('mailgate.message')
+        msg_obj = self.pool.get('email.message')
         if context is None:
             context = {}
         if context.get('active_id'):

=== renamed directory 'mail_gateway' => 'mail'
=== modified file 'mail/__init__.py'
--- mail_gateway/__init__.py	2011-01-14 00:11:01 +0000
+++ mail/__init__.py	2011-04-11 12:37:32 +0000
@@ -19,8 +19,10 @@
 #
 ##############################################################################
 
-import mail_gateway
+import email_message
+import email_thread
 import res_partner
+import wizard
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 

=== modified file 'mail/__openerp__.py'
--- mail_gateway/__openerp__.py	2011-03-18 14:06:18 +0000
+++ mail/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -20,24 +20,27 @@
 ##############################################################################
 
 {
-    'name': 'Email Gateway System',
+    'name': 'Email System',
     'version': '1.0',
     'category': 'Tools',
     'description': """
-The generic email gateway system allows to send and receive emails.
+The generic email system allows to send and receive emails.
 ===================================================================
 
-    * History of emails
-    * Easy integration with any module""",
+    * SMTP Server Configuration
+    * Provide API for Sending Messages
+    * Store all emails releated messages""",
     'author': 'OpenERP SA',
     'website': 'http://www.openerp.com',
-    'depends': ['base'],
+    'depends': ['base', 'base_tools'],
     'init_xml': [],
     'update_xml': [
-        "mail_gateway_view.xml",
+        "wizard/email_compose_message_view.xml",
+        "email_view.xml",
+        "email_thread_view.xml",
         "res_partner_view.xml",
-        'security/ir.model.access.csv'
-
+        'security/ir.model.access.csv',
+        'email_data.xml',
     ],
     'demo_xml': [],
     'installable': True,

=== added file 'mail/email_data.xml'
--- mail/email_data.xml	1970-01-01 00:00:00 +0000
+++ mail/email_data.xml	2011-04-11 12:37:32 +0000
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+    <data noupdate="1">
+            <record forcecreate="True" id="ir_cron_mail_scheduler_action" model="ir.cron">
+            <field name="name">Email scheduler</field>
+            <field name="user_id" ref="base.user_root"/>
+            <field name="interval_number">1</field>
+            <field name="interval_type">hours</field>
+            <field name="numbercall">-1</field>
+            <field eval="False" name="doall"/>
+            <field eval="'email.message'" name="model"/>
+            <field eval="'process_email_queue'" name="function"/>
+            <field eval="'()'" name="args"/>
+        </record>
+    </data>
+</openerp>

=== renamed file 'mail_gateway/mail_gateway.py' => 'mail/email_message.py'
--- mail_gateway/mail_gateway.py	2011-03-17 17:33:16 +0000
+++ mail/email_message.py	2011-04-11 12:37:32 +0000
@@ -19,164 +19,49 @@
 #
 ##############################################################################
 
-from osv import osv, fields
+from osv import osv
+from osv import fields
+from tools.translate import _
+import tools
+import netsvc
+import base64
 import time
-import tools
-import binascii
+import logging
+import re
 import email
 from email.header import decode_header
-from email.utils import parsedate
-import base64
-import re
-from tools.translate import _
-import logging
-import xmlrpclib
-
-_logger = logging.getLogger('mailgate')
-
-class mailgate_thread(osv.osv):
-    '''
-    Mailgateway Thread
-    '''
-    _name = 'mailgate.thread'
-    _description = 'Mailgateway Thread'
-
-    _columns = {
-        'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', readonly=True),
-    }
-
-    def copy(self, cr, uid, id, default=None, context=None):
-        """
-        Overrides orm copy method.
-        @param self: the object pointer
-        @param cr: the current row, from the database cursor,
-        @param uid: the current user’s ID for security checks,
-        @param id: Id of mailgate thread
-        @param default: Dictionary of default values for copy.
-        @param context: A standard dictionary for contextual values
-        """
-        if default is None:
-            default = {}
-
-        default.update({
-            'message_ids': [],
-            'date_closed': False,
-            'date_open': False
-        })
-        return super(mailgate_thread, self).copy(cr, uid, id, default, context=context)
-
-    def message_new(self, cr, uid, msg, context):
-        raise Exception, _('Method is not implemented')
-
-    def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context=None):
-        raise Exception, _('Method is not implemented')
-
-    def message_followers(self, cr, uid, ids, context=None):
-        """ Get a list of emails of the people following this thread
-        """
-        res = {}
-        if isinstance(ids, (str, int, long)):
-            ids = [long(ids)]
-        for thread in self.browse(cr, uid, ids, context=context):
-            l=[]
-            for message in thread.message_ids:
-                l.append((message.user_id and message.user_id.email) or '')
-                l.append(message.email_from or '')
-                l.append(message.email_cc or '')
-            res[thread.id] = l
-        return res
-
-    def msg_send(self, cr, uid, id, *args, **argv):
-        raise Exception, _('Method is not implemented')
-
-    def history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, \
-                    email_from=False, message_id=False, references=None, attach=None, email_cc=None, \
-                    email_bcc=None, email_date=None, context=None):
-        """
-        @param self: The object pointer
-        @param cr: the current row, from the database cursor,
-        @param uid: the current user’s ID for security checks,
-        @param cases: a browse record list
-        @param keyword: Subject of the history item
-        @param history: Value True/False, If True it makes entry in case History otherwise in Case Log
-        @param email: Email-To / Recipient address
-        @param email_from: Email From / Sender address if any
-        @param email_cc: Comma-Separated list of Carbon Copy Emails To addresse if any
-        @param email_bcc: Comma-Separated list of Blind Carbon Copy Emails To addresses if any
-        @param email_date: Email Date string if different from now, in server Timezone
-        @param details: Description, Details of case history if any
-        @param atach: Attachment sent in email
-        @param context: A standard dictionary for contextual values"""
-        if context is None:
-            context = {}
-        if attach is None:
-            attach = []
-
-        if email_date:
-            edate = parsedate(email_date)
-            if edate is not None:
-                email_date = time.strftime('%Y-%m-%d %H:%M:%S', edate)
-
-        # The mailgate sends the ids of the cases and not the object list
-
-        if all(isinstance(case_id, (int, long)) for case_id in cases):
-            cases = self.browse(cr, uid, cases, context=context)
-
-        att_obj = self.pool.get('ir.attachment')
-        obj = self.pool.get('mailgate.message')
-
-        for case in cases:
-            attachments = []
-            for att in attach:
-                att_ids = att_obj.search(cr, uid, [('name','=',att[0]), ('res_id', '=', case.id)])
-                if att_ids:
-                    attachments.append(att_ids[0])
-                else:
-                    attachments.append(att_obj.create(cr, uid, {'res_model':case._name,'res_id':case.id, 'name': att[0], 'datas': base64.encodestring(att[1])}))
-
-            partner_id = hasattr(case, 'partner_id') and (case.partner_id and case.partner_id.id or False) or False
-            if not partner_id and case._name == 'res.partner':
-                partner_id = case.id
-            data = {
-                'name': keyword,
-                'user_id': uid,
-                'model' : case._name,
-                'partner_id': partner_id,
-                'res_id': case.id,
-                'date': time.strftime('%Y-%m-%d %H:%M:%S'),
-                'message_id': message_id,
-                'description': details,
-                'attachment_ids': [(6, 0, attachments)]
-            }
-
-            if history:
-                for param in (email, email_cc, email_bcc):
-                    if isinstance(param, list):
-                        param = ", ".join(param)
-
-                data = {
-                    'name': subject or _('History'),
-                    'history': True,
-                    'user_id': uid,
-                    'model' : case._name,
-                    'res_id': case.id,
-                    'date': email_date or time.strftime('%Y-%m-%d %H:%M:%S'),
-                    'description': details or (hasattr(case, 'description') and case.description or False),
-                    'email_to': email,
-                    'email_from': email_from or \
-                        (hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
-                         case.user_id.address_id.email),
-                    'email_cc': email_cc,
-                    'email_bcc': email_bcc,
-                    'partner_id': partner_id,
-                    'references': references,
-                    'message_id': message_id,
-                    'attachment_ids': [(6, 0, attachments)]
-                }
-
-            obj.create(cr, uid, data, context=context)
-        return True
-mailgate_thread()
+#import binascii
+#import email
+#from email.header import decode_header
+#from email.utils import parsedate
+#import base64
+#import re
+#import logging
+#import xmlrpclib
+
+#import re
+#import smtplib
+#import base64
+#from email import Encoders
+#from email.mime.base import MIMEBase
+#from email.mime.multipart import MIMEMultipart
+#from email.mime.text import MIMEText
+#from email.header import decode_header, Header
+#from email.utils import formatdate
+#import netsvc
+#import datetime
+#import tools
+#import logging
+#email_content_types = [
+#    'multipart/mixed',
+#    'multipart/alternative',
+#    'multipart/related',
+#    'text/plain',
+#    'text/html'
+#]
+
+LOGGER = netsvc.Logger()
+_logger = logging.getLogger('mail')
 
 def format_date_tz(date, tz=None):
     if not date:
@@ -184,10 +69,42 @@
     format = tools.DEFAULT_SERVER_DATETIME_FORMAT
     return tools.server_to_local_timestamp(date, format, format, tz)
 
-class mailgate_message(osv.osv):
-    '''
-    Mailgateway Message
-    '''
+class email_message_common(osv.osv_memory):
+    _name = 'email.message.common'
+    _columns = {
+        'subject':fields.text('Subject', translate=True),
+        'model': fields.char('Object Name', size=128, select=1),
+        'res_id': fields.integer('Resource ID', select=1),
+        'date': fields.datetime('Date'),
+        'user_id': fields.many2one('res.users', 'User Responsible'),
+        'email_from': fields.char('From', size=128, help="Email From"),
+        'email_to': fields.char('To', help="Email Recipients", size=256),
+        'email_cc': fields.char('Cc', help="Carbon Copy Email Recipients", size=256),
+        'email_bcc': fields.char('Bcc', help='Blind Carbon Copy Email Recipients', size=256),
+        'message_id': fields.char('Message Id', size=1024, help="Message Id on Email.", select=1),
+        'references': fields.text('References', help="References emails."),
+        'reply_to':fields.char('Reply-To', size=250),
+        'sub_type': fields.char('Sub Type', size=32),
+        'headers': fields.text('x_headers'),
+        'priority':fields.integer('Priority'),
+        'body': fields.text('Description', translate=True),
+        'body_html': fields.text('HTML', help="Contains HTML version of email"),
+        'smtp_server_id':fields.many2one('ir.mail_server', 'SMTP Server'),
+    }
+    _rec_name = 'subject'
+
+    _sql_constraints = []
+email_message_common()
+
+class email_message(osv.osv):
+    '''
+    Email Message
+    '''
+    _inherit = 'email.message.common'
+    _name = 'email.message'
+    _description = 'Email Message'
+    _order = 'date desc'
+
     def open_document(self, cr, uid, ids, context=None):
         """ To Open Document
         @param self: The object pointer.
@@ -251,53 +168,116 @@
         for message in self.browse(cr, uid, ids, context=context):
             msg_txt = ''
             if message.history:
-                msg_txt += _('%s wrote on %s:\n\t') % (message.email_from or '/', format_date_tz(message.date, tz))
-                if message.description:
-                    msg_txt += self.truncate_data(cr, uid, message.description, context=context)
+                msg_txt += (message.email_from or '/') + _(' wrote on ') + format_date_tz(message.date, tz) + ':\n\t'
+                if message.body:
+                    msg_txt += self.truncate_data(cr, uid, message.body, context=context)
             else:
-                msg_txt = _('%s on %s:\n\t') % (message.user_id.name or '/', format_date_tz(message.date, tz))
-                msg_txt += message.name
+                msg_txt = (message.user_id.name or '/') + _(' on ') + format_date_tz(message.date, tz) + ':\n\t'
+                msg_txt += message.subject
             result[message.id] = msg_txt
         return result
 
-    _name = 'mailgate.message'
-    _description = 'Mailgateway Message'
-    _order = 'date desc'
     _columns = {
-        'name':fields.text('Subject', readonly=True),
-        'model': fields.char('Object Name', size=128, select=1, readonly=True),
-        'res_id': fields.integer('Resource ID', select=1, readonly=True),
-        'ref_id': fields.char('Reference Id', size=256, readonly=True, help="Message Id in Email Server.", select=True),
-        'date': fields.datetime('Date', readonly=True),
-        'history': fields.boolean('Is History?', readonly=True),
-        'user_id': fields.many2one('res.users', 'User Responsible', readonly=True),
-        'message': fields.text('Description', readonly=True),
-        'email_from': fields.char('From', size=128, help="Email From", readonly=True),
-        'email_to': fields.char('To', help="Email Recipients", size=256, readonly=True),
-        'email_cc': fields.char('Cc', help="Carbon Copy Email Recipients", size=256, readonly=True),
-        'email_bcc': fields.char('Bcc', help='Blind Carbon Copy Email Recipients', size=256, readonly=True),
-        'message_id': fields.char('Message Id', size=1024, readonly=True, help="Message Id on Email.", select=True),
-        'references': fields.text('References', readonly=True, help="References emails."),
-        'description': fields.text('Description', readonly=True),
-        'partner_id': fields.many2one('res.partner', 'Partner', required=False),
-        'attachment_ids': fields.many2many('ir.attachment', 'message_attachment_rel', 'message_id', 'attachment_id', 'Attachments', readonly=True),
+        'partner_id': fields.many2one('res.partner', 'Partner'),
+        'attachment_ids': fields.many2many('ir.attachment', 'message_attachment_rel', 'message_id', 'attachment_id', 'Attachments'),
         'display_text': fields.function(_get_display_text, method=True, type='text', size="512", string='Display Text'),
+        'debug':fields.boolean('Debug', readonly=True),
+        'history': fields.boolean('History', readonly=True),
+        'state':fields.selection([
+                        ('outgoing', 'Outgoing'),
+                        ('sent', 'Sent'),
+                        ('received', 'Received'),
+                        ('exception', 'Exception'),
+                        ('cancel', 'Cancelled'),
+                        ], 'State', readonly=True),
+        'auto_delete': fields.boolean('Auto Delete', help="Permanently delete emails after sending"),
+    }
+
+    _defaults = {
     }
 
     def init(self, cr):
         cr.execute("""SELECT indexname
                       FROM pg_indexes
-                      WHERE indexname = 'mailgate_message_res_id_model_idx'""")
+                      WHERE indexname = 'email_message_res_id_model_idx'""")
         if not cr.fetchone():
-            cr.execute("""CREATE INDEX mailgate_message_res_id_model_idx
-                          ON mailgate_message (model, res_id)""")
-
-mailgate_message()
-
-class mailgate_tool(osv.osv_memory):
-
-    _name = 'email.server.tools'
-    _description = "Email Server Tools"
+            cr.execute("""CREATE INDEX email_message_res_id_model_idx
+                          ON email_message (model, res_id)""")
+
+    def schedule_with_attach(self, cr, uid, email_from, email_to, subject, body, model=False, email_cc=None, email_bcc=None, reply_to=False, attach=None,
+            message_id=False, references=False, openobject_id=False, debug=False, subtype='plain', x_headers={}, priority='3', smtp_server_id=False, context=None, auto_delete=False):
+        attachment_obj = self.pool.get('ir.attachment')
+        if email_to and type(email_to) != list:
+            email_to = [email_to]
+        if email_cc and type(email_cc) != list:
+            email_cc = [email_cc]
+        if email_bcc and type(email_bcc) != list:
+            email_bcc = [email_bcc]
+
+        msg_vals = {
+                'subject': subject,
+                'model': model or '',
+                'date': time.strftime('%Y-%m-%d %H:%M:%S'),
+                'user_id': uid,
+                'body': body,
+                'email_from': email_from,
+                'email_to': email_to and ','.join(email_to) or '',
+                'email_cc': email_cc and ','.join(email_cc) or '',
+                'email_bcc': email_bcc and ','.join(email_bcc) or '',
+                'reply_to': reply_to,
+                'res_id': openobject_id,
+                'message_id': message_id,
+                'references': references or '',
+                'sub_type': subtype or '',
+                'headers': x_headers or False,
+                'priority': priority,
+                'debug': debug,
+                'history': True,
+                'smtp_server_id': smtp_server_id,
+                'state': 'outgoing',
+                'auto_delete': auto_delete
+            }
+        email_msg_id = self.create(cr, uid, msg_vals, context)
+        if attach:
+            attachment_ids = []
+            for attachment in attach:
+                attachment_data = {
+                        'name': attachment[0],
+                        'subject':  (subject or '') + _(' (Email Attachment)'),
+                        'datas': attachment[1],
+                        'datas_fname': attachment[0],
+                        'body': subject or _('No Description'),
+                        'res_model':'email.message',
+                        'res_id': email_msg_id,
+                    }
+                if context.has_key('default_type'):
+                    del context['default_type']
+                attachment_ids.append(attachment_obj.create(cr, uid, attachment_data, context))
+            self.write(cr, uid, email_msg_id,
+                              { 'attachment_ids': [[6, 0, attachment_ids]] }, context)
+        return email_msg_id
+
+    def process_retry(self, cr, uid, ids, context=None):
+        return self.write(cr, uid, ids, {'state':'outgoing'}, context)
+
+    def process_email_queue(self, cr, uid, ids=None, context=None):
+        if ids is None:
+            ids = []
+        if context is None:
+            context = {}
+        if not ids:
+            filters = [('state', '=', 'outgoing')]
+            if 'filters' in context:
+                filters.extend(context['filters'])
+            ids = self.search(cr, uid, filters, context=context)
+        try:
+            res = self.send_email(cr, uid, ids, auto_commit=True, context=context)
+        except Exception, error:
+            logger = netsvc.Logger()
+            msg = _("Sending of Mail failed. Error: %s") % (error)
+            logger.notifyChannel("email", netsvc.LOG_ERROR, msg)
+            return False
+        return res
 
     def _decode_header(self, text):
         """Returns unicode() string conversion of the the given encoded smtp header"""
@@ -308,138 +288,20 @@
     def to_email(self,text):
         return re.findall(r'([^ ,<@]+@[^> ,]+)',text)
 
-    def history(self, cr, uid, model, res_ids, msg, attach, context=None):
-        """This function creates history for mails fetched
-        @param self: The object pointer
-        @param cr: the current row, from the database cursor,
-        @param uid: the current user’s ID for security checks,
-        @param model: OpenObject Model
-        @param res_ids: Ids of the record of OpenObject model created
-        @param msg: Email details
-        @param attach: Email attachments
-        """
-        if isinstance(res_ids, (int, long)):
-            res_ids = [res_ids]
-
-        msg_pool = self.pool.get('mailgate.message')
-        for res_id in res_ids:
-            case = self.pool.get(model).browse(cr, uid, res_id, context=context)
-            partner_id = hasattr(case, 'partner_id') and (case.partner_id and case.partner_id.id or False) or False
-            if not partner_id and model == 'res.partner':
-                partner_id = res_id
-            msg_data = {
-                'name': msg.get('subject', 'No subject'),
-                'date': msg.get('date'),
-                'description': msg.get('body', msg.get('from')),
-                'history': True,
-                'partner_id': partner_id,
-                'model': model,
-                'email_cc': msg.get('cc'),
-                'email_from': msg.get('from'),
-                'email_to': msg.get('to'),
-                'message_id': msg.get('message-id'),
-                'references': msg.get('references') or msg.get('in-reply-to'),
-                'res_id': res_id,
-                'user_id': uid,
-                'attachment_ids': [(6, 0, attach)]
-            }
-            msg_pool.create(cr, uid, msg_data, context=context)
-        return True
-
-    def email_forward(self, cr, uid, model, res_ids, msg, email_error=False, context=None):
-        """Sends an email to all people following the thread
-        @param res_id: Id of the record of OpenObject model created from the email message
-        @param msg: email.message.Message to forward
-        @param email_error: Default Email address in case of any Problem
-        """
-        model_pool = self.pool.get(model)
-
-        for res in model_pool.browse(cr, uid, res_ids, context=context):
-            message_followers = model_pool.message_followers(cr, uid, [res.id])[res.id]
-            message_followers_emails = self.to_email(','.join(filter(None, message_followers)))
-            message_recipients = self.to_email(','.join(filter(None,
-                                                         [self._decode_header(msg['from']),
-                                                         self._decode_header(msg['to']),
-                                                         self._decode_header(msg['cc'])])))
-            message_forward = [i for i in message_followers_emails if (i and (i not in message_recipients))]
-
-            if message_forward:
-                # TODO: we need an interface for this for all types of objects, not just leads
-                if hasattr(res, 'section_id'):
-                    del msg['reply-to']
-                    msg['reply-to'] = res.section_id.reply_to
-
-                smtp_from = self.to_email(msg['from'])
-                if not tools.misc._email_send(smtp_from, message_forward, msg, openobject_id=res.id) and email_error:
-                    subj = msg['subject']
-                    del msg['subject'], msg['to'], msg['cc'], msg['bcc']
-                    msg['subject'] = '[OpenERP-Forward-Failed] %s' % subj
-                    msg['to'] = email_error
-                    tools.misc._email_send(smtp_from, self.to_email(email_error), msg, openobject_id=res.id)
-
-    def process_email(self, cr, uid, model, message, custom_values=None, attach=True, context=None):
-        """This function Processes email and create record for given OpenERP model
-        @param self: The object pointer
-        @param cr: the current row, from the database cursor,
-        @param uid: the current user’s ID for security checks,
-        @param model: OpenObject Model
-        @param message: Email details, passed as a string or an xmlrpclib.Binary
-        @param attach: Email attachments
-        @param context: A standard dictionary for contextual values"""
-
-        # extract message bytes, we are forced to pass the message as binary because
-        # we don't know its encoding until we parse its headers and hence can't
-        # convert it to utf-8 for transport between the mailgate script and here.
-        if isinstance(message, xmlrpclib.Binary):
-            message = str(message.data)
-
-        if context is None:
-            context = {}
-
-        if custom_values is None or not isinstance(custom_values, dict):
-            custom_values = {}
-
-        model_pool = self.pool.get(model)
-        res_id = False
-
-        # Create New Record into particular model
-        def create_record(msg):
-            att_ids = []
-            if hasattr(model_pool, 'message_new'):
-                res_id = model_pool.message_new(cr, uid, msg, context=context)
-                if custom_values:
-                    model_pool.write(cr, uid, [res_id], custom_values, context=context)
-            else:
-                data = {
-                    'name': msg.get('subject'),
-                    'email_from': msg.get('from'),
-                    'email_cc': msg.get('cc'),
-                    'user_id': False,
-                    'description': msg.get('body'),
-                    'state' : 'draft',
-                }
-                data.update(self.get_partner(cr, uid, msg.get('from'), context=context))
-                res_id = model_pool.create(cr, uid, data, context=context)
-
-                if attach:
-                    for attachment in msg.get('attachments', []):
-                        data_attach = {
-                            'name': attachment,
-                            'datas': binascii.b2a_base64(str(attachments.get(attachment))),
-                            'datas_fname': attachment,
-                            'description': 'Mail attachment',
-                            'res_model': model,
-                            'res_id': res_id,
-                        }
-                        att_ids.append(self.pool.get('ir.attachment').create(cr, uid, data_attach))
-
-            return res_id, att_ids
+    def parse_message(self, message):
+        """Return Dictionary Object after parse EML Message String
+        @param message: email.message.Message object or string or unicode object
+        """
+        if isinstance(message, str):
+            msg_txt = email.message_from_string(message)
 
         # Warning: message_from_string doesn't always work correctly on unicode,
         # we must use utf-8 strings here :-(
         if isinstance(message, unicode):
             message = message.encode('utf-8')
-        msg_txt = email.message_from_string(message)
+            msg_txt = email.message_from_string(message)
+
+        msg_txt = message
         message_id = msg_txt.get('message-id', False)
         msg = {}
 
@@ -447,8 +309,9 @@
             # Very unusual situation, be we should be fault-tolerant here
             message_id = time.time()
             msg_txt['message-id'] = message_id
-            _logger.info('Message without message-id, generating a random one: %s', message_id)
+            _logger.info('Parsing Message without message-id, generating a random one: %s', message_id)
 
+       
         fields = msg_txt.keys()
         msg['id'] = message_id
         msg['message-id'] = message_id
@@ -460,7 +323,7 @@
             msg['content-type'] = msg_txt.get('Content-Type')
 
         if 'From' in fields:
-            msg['from'] = self._decode_header(msg_txt.get('From'))
+            msg['from'] = self._decode_header(msg_txt.get('From') or msg_txt.get_unixfrom())
 
         if 'Delivered-To' in fields:
             msg['to'] = self._decode_header(msg_txt.get('Delivered-To'))
@@ -484,12 +347,12 @@
             msg['in-reply-to'] = msg_txt.get('In-Reply-To')
 
         if 'X-Priority' in fields:
-            msg['priority'] = msg_txt.get('X-Priority', '3 (Normal)').split(' ')[0]
+            msg['priority'] = msg_txt.get('X-Priority', '3 (Normal)').split(' ')[0] #TOFIX:
 
-        if not msg_txt.is_multipart() or 'text/plain' in msg.get('Content-Type', ''):
+        if not msg_txt.is_multipart() or 'text/plain' in msg.get('content-type', ''):
             encoding = msg_txt.get_content_charset()
             body = msg_txt.get_payload(decode=True)
-            if 'text/html' in msg_txt.get('Content-Type', ''):
+            if 'text/html' in msg.get('content-type', ''):
                 body = tools.html2plaintext(body)
             msg['body'] = tools.ustr(body, encoding)
 
@@ -527,78 +390,135 @@
 
             msg['body'] = body
             msg['attachments'] = attachments
-        res_ids = []
-        attachment_ids = []
-        new_res_id = False
-        if msg.get('references') or msg.get('in-reply-to'):
-            references = msg.get('references') or msg.get('in-reply-to')
-            if '\r\n' in references:
-                references = references.split('\r\n')
-            else:
-                references = references.split(' ')
-            for ref in references:
-                ref = ref.strip()
-                res_id = tools.misc.reference_re.search(ref)
-                if res_id:
-                    res_id = res_id.group(1)
+        return msg
+
+    def send_email(self, cr, uid, ids, auto_commit=False, context=None):
+        """
+        send email message
+        """
+        if context is None:
+            context = {}
+        smtp_server_obj = self.pool.get('ir.mail_server')
+        attachment_pool = self.pool.get('ir.attachment')
+        self.write(cr, uid, ids, {'state':'outgoing'}, context)
+        for message in self.browse(cr, uid, ids, context):
+            try:
+                smtp_server = message.smtp_server_id
+                if not smtp_server:
+                    smtp_ids = smtp_server_obj.search(cr, uid, [])
+                    if smtp_ids:
+                        smtp_server = smtp_server_obj.browse(cr, uid, smtp_ids, context)[0]
+                attachments = []
+                for attach in message.attachment_ids:
+                    attachments.append((attach.datas_fname, base64.b64decode(attach.datas)))
+                if message.state in ['outgoing', 'exception']:
+                    msg = smtp_server_obj.pack_message(cr, uid, message.email_from,
+                        message.email_to and message.email_to.split(',') or [], message.subject, message.body,
+                        email_cc=message.email_cc and message.email_cc.split(',') or [],
+                        email_bcc=message.email_bcc and message.email_bcc.split(',') or [],
+                        reply_to=message.reply_to,
+                        attach=attachments, message_id=message.message_id, references = message.references,
+                        openobject_id=message.res_id,
+                        subtype=message.sub_type,
+                        x_headers=message.headers and eval(message.headers) or {},
+                        priority=message.priority)
+                    res = smtp_server_obj.send_email(cr, uid, 
+                        msg,
+                        mail_server_id = message.smtp_server_id.id or None,
+                        smtp_server=smtp_server and smtp_server.smtp_host or None,
+                        smtp_port=smtp_server and smtp_server.smtp_port or None,
+                        smtp_user=smtp_server and smtp_server.smtp_user or None,
+                        smtp_password=smtp_server and smtp_server.smtp_pass or None,
+                        ssl=smtp_server and smtp_server.smtp_ssl or False,
+                        tls=smtp_server and smtp_server.smtp_tls,
+                        debug=message.debug)
+                    if res:
+                        self.write(cr, uid, [message.id], {'state':'sent', 'message_id': res}, context)
+                    else:
+                        self.write(cr, uid, [message.id], {'state':'exception'}, context)
                 else:
-                    res_id = tools.misc.res_re.search(msg['subject'])
-                    if res_id:
-                        res_id = res_id.group(1)
-                if res_id:
-                    res_id = int(res_id)
-                    model_pool = self.pool.get(model)
-                    if model_pool.exists(cr, uid, res_id):
-                        res_ids.append(res_id)
-                        if hasattr(model_pool, 'message_update'):
-                            model_pool.message_update(cr, uid, [res_id], {}, msg, context=context)
-                        else:
-                            raise NotImplementedError('model %s does not support updating records, mailgate API method message_update() is missing'%model)
-
-        if not len(res_ids):
-            new_res_id, attachment_ids = create_record(msg)
-            res_ids = [new_res_id]
-
-        # Store messages
-        context.update({'model' : model})
-        if hasattr(model_pool, 'history'):
-            model_pool.history(cr, uid, res_ids, _('receive'), history=True,
-                            subject = msg.get('subject'),
-                            email = msg.get('to'),
-                            details = msg.get('body'),
-                            email_from = msg.get('from'),
-                            email_cc = msg.get('cc'),
-                            message_id = msg.get('message-id'),
-                            references = msg.get('references', False) or msg.get('in-reply-to', False),
-                            attach = attachments.items(),
-                            email_date = msg.get('date'),
-                            context = context)
-        else:
-            self.history(cr, uid, model, res_ids, msg, attachment_ids, context=context)
-        self.email_forward(cr, uid, model, res_ids, msg_txt)
-        return new_res_id
-
-    def get_partner(self, cr, uid, from_email, context=None):
-        """This function returns partner Id based on email passed
-        @param self: The object pointer
-        @param cr: the current row, from the database cursor,
-        @param uid: the current user’s ID for security checks
-        @param from_email: email address based on that function will search for the correct
-        """
-        address_pool = self.pool.get('res.partner.address')
-        res = {
-            'partner_address_id': False,
-            'partner_id': False
-        }
-        from_email = self.to_email(from_email)[0]
-        address_ids = address_pool.search(cr, uid, [('email', 'like', from_email)])
-        if address_ids:
-            address = address_pool.browse(cr, uid, address_ids[0])
-            res['partner_address_id'] = address_ids[0]
-            res['partner_id'] = address.partner_id.id
-
-        return res
-
-mailgate_tool()
+                    raise osv.except_osv(_('Error !'), _('No messages in outgoing or exception state!'))
+
+                #if auto_delete=True then delete that sent messages as well as attachments
+                message_data = self.read(cr, uid, message.id, ['state', 'auto_delete', 'attachment_ids'])
+                if message_data['state'] == 'sent' and message_data['auto_delete'] == True:
+                    self.unlink(cr, uid, [message.id], context=context)
+                    if message_data['attachment_ids']:
+                        attachment_pool.unlink(cr, uid, message_data['attachment_ids'], context=context)
+
+                if auto_commit == True:
+                    cr.commit()
+
+            except Exception, error:
+                logger = netsvc.Logger()
+                logger.notifyChannel("email-template", netsvc.LOG_ERROR, _("Sending of Mail %s failed. Probable Reason:Could not login to server\nError: %s") % (message.id, error))
+                self.write(cr, uid, [message.id], {'state':'exception'}, context)
+                return False
+        return True
+
+    def do_cancel(self, cr, uid, ids, context=None):
+        '''
+        Cancel the email to be send
+        '''
+        self.write(cr, uid, ids, {'state':'cancel'}, context)
+        return True
+# OLD Code.
+#    def send_all_mail(self, cr, uid, ids=None, context=None):
+#        if ids is None:
+#            ids = []
+#        if context is None:
+#            context = {}
+#        filters = [('folder', '=', 'outbox'), ('state', '!=', 'sending')]
+#        if 'filters' in context.keys():
+#            for each_filter in context['filters']:
+#                filters.append(each_filter)
+#        ids = self.search(cr, uid, filters, context=context)
+#        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.smtp_server')
+#                values = self.read(cr, uid, id, [], context)
+#                payload = {}
+#                if values['attachments_ids']:
+#                    for attid in values['attachments_ids']:
+#                        attachment = attachment_pool.browse(cr, uid, attid, context)#,['datas_fname','datas'])
+#                        payload[attachment.datas_fname] = attachment.datas
+#                result = account_obj.send_email(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)
+#                if result == True:
+#                    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)
+#                else:
+#                    error = result['error_msg']
+#
+#            except Exception, error:
+#                logger = netsvc.Logger()
+#                logger.notifyChannel("email-template", netsvc.LOG_ERROR, _("Sending of Mail %s failed. Probable Reason:Could not login to server\nError: %s") % (id, error))
+#            self.write(cr, uid, id, {'state':'na'}, context)
+#        return result
+
+email_message()
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'mail/email_thread.py'
--- mail/email_thread.py	1970-01-01 00:00:00 +0000
+++ mail/email_thread.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,375 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details
+#
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+from osv import osv, fields
+import time
+import tools
+import binascii
+import email
+
+from email.utils import parsedate
+import base64
+
+from tools.translate import _
+import logging
+import xmlrpclib
+
+_logger = logging.getLogger('mail')
+
+class email_thread(osv.osv):
+    '''
+    Email Thread
+    '''
+    _name = 'email.thread'
+    _description = 'Email Thread'
+
+    _columns = {
+        'message_ids': fields.one2many('email.message', 'res_id', 'Messages', readonly=True),
+    }
+
+    def copy(self, cr, uid, id, default=None, context=None):
+        """
+        Overrides orm copy method.
+        @param self: the object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param id: Id of mailgate thread
+        @param default: Dictionary of default values for copy.
+        @param context: A standard dictionary for contextual values
+        """
+        if default is None:
+            default = {}
+
+        default.update({
+            'message_ids': [],
+        })
+        return super(email_thread, self).copy(cr, uid, id, default, context=context)
+
+    def message_new(self, cr, uid, msg, context=None):
+        """
+        Called by process_email() to create a new record
+        corresponding to an incoming message for a new thread.
+        @param msg: Dictionary Object to contain email message data
+        """
+        if context is None:
+            context = {}
+        model = context.get('thread_model', False)
+        if not model:
+            model = self._name
+        model_pool = self.pool.get(model)
+        if hasattr(model_pool, 'message_new'):
+            res_id = model_pool.message_new(cr, uid, msg, context)
+        else:
+            fields = model_pool.fields_get(cr, uid, context=context)
+            data = model_pool.default_get(cr, uid, fields, context=context)
+            if 'name' in fields and not data.get('name',False):
+                data['name'] = msg.get('from','')
+            res_id = model_pool.create(cr, uid, data, context=context)
+
+        attachments = msg.get('attachments', {})
+        self.history(cr, uid, [res_id], _('receive'), history=True,
+                            subject = msg.get('subject'),
+                            email = msg.get('to'),
+                            details = msg.get('body'),
+                            email_from = msg.get('from'),
+                            email_cc = msg.get('cc'),
+                            message_id = msg.get('message-id'),
+                            references = msg.get('references', False) or msg.get('in-reply-to', False),
+                            attach = attachments.items(),
+                            email_date = msg.get('date'),
+                            context = context)
+        return res_id
+
+    def message_update(self, cr, uid, ids, msg, vals={}, default_act=None, context=None):
+        """
+        Called by process_email() to add a new incoming message for an existing thread
+        @param msg: Dictionary Object to contain email message data
+        """
+        if context is None:
+            context = {}
+        model = context.get('thread_model', False)
+        if not model:
+            model = self._name
+        model_pool = self.pool.get(model)
+        if hasattr(model_pool, 'message_update'):
+            model_pool.message_update(cr, uid, ids, msg, vals=vals, default_act=default_act, context=context)
+        attachments = msg.get('attachments', {})
+        self.history(cr, uid, ids, _('receive'), history=True,
+                            subject = msg.get('subject'),
+                            email = msg.get('to'),
+                            details = msg.get('body'),
+                            email_from = msg.get('from'),
+                            email_cc = msg.get('cc'),
+                            message_id = msg.get('message-id'),
+                            references = msg.get('references', False) or msg.get('in-reply-to', False),
+                            attach = attachments.items(),
+                            email_date = msg.get('date'),
+                            context = context)
+        return True
+
+    def thread_followers(self, cr, uid, ids, context=None):
+        """ Get a list of emails of the people following this thread
+        """
+        res = {}
+        if isinstance(ids, (str, int, long)):
+            ids = [long(ids)]
+        for thread in self.browse(cr, uid, ids, context=context):
+            l=[]
+            for message in thread.message_ids:
+                l.append((message.user_id and message.user_id.email) or '')
+                l.append(message.email_from or '')
+                l.append(message.email_cc or '')
+            res[thread.id] = l
+        return res
+
+    def history(self, cr, uid, threads, keyword, history=False, subject=None, email=False, details=None, \
+                    email_from=False, message_id=False, references=None, attach=None, email_cc=None, \
+                    email_bcc=None, email_date=None, context=None):
+        """
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param threads: a browse record list
+        @param keyword: Thread action keyword e.g.: If thread is closed "Close" keyword is used
+        @param history: Value True/False, If True it makes entry as a Emails Messages otherwise Log Messages
+        @param email: Email-To / Recipient address
+        @param email_from: Email From / Sender address if any
+        @param email_cc: Comma-Separated list of Carbon Copy Emails To addresse if any
+        @param email_bcc: Comma-Separated list of Blind Carbon Copy Emails To addresses if any
+        @param email_date: Email Date string if different from now, in server Timezone
+        @param details: Description, Details of thread history if any
+        @param attach: Attachment sent in email
+        @param context: A standard dictionary for contextual values"""
+        if context is None:
+            context = {}
+        if attach is None:
+            attach = []
+
+        model = context.get('thread_model', False)
+        if not model:
+            model = self._name
+        model_pool = self.pool.get(model)
+
+        if email_date:
+            edate = parsedate(email_date)
+            if edate is not None:
+                email_date = time.strftime('%Y-%m-%d %H:%M:%S', edate)
+
+        # The script sends the ids of the threads and not the object list
+
+        if all(isinstance(thread_id, (int, long)) for thread_id in threads):
+            threads = model_pool.browse(cr, uid, threads, context=context)
+
+        att_obj = self.pool.get('ir.attachment')
+        obj = self.pool.get('email.message')
+
+        for thread in threads:
+            attachments = []
+            for attachment in attach:
+                data_attach = {
+                    'name': attachment[0],
+                    'datas': binascii.b2a_base64(str(attachment[1])),
+                    'datas_fname': attachment[0],
+                    'description': _('Mail attachment'),
+                    'res_model': thread._name,
+                    'res_id': thread.id,
+                }
+                attachments.append(att_obj.create(cr, uid, data_attach))
+
+            partner_id = hasattr(thread, 'partner_id') and (thread.partner_id and thread.partner_id.id or False) or False
+            if not partner_id and thread._name == 'res.partner':
+                partner_id = thread.id
+            data = {
+                'subject': keyword,
+                'user_id': uid,
+                'model' : thread._name,
+                'partner_id': partner_id,
+                'res_id': thread.id,
+                'date': time.strftime('%Y-%m-%d %H:%M:%S'),
+                'message_id': message_id,
+                'body': details or (hasattr(thread, 'description') and thread.description or False),
+                'attachment_ids': [(6, 0, attachments)]
+            }
+
+            if history:
+                for param in (email, email_cc, email_bcc):
+                    if isinstance(param, list):
+                        param = ", ".join(param)
+
+                data = {
+                    'subject': subject or _('History'),
+                    'history': True,
+                    'user_id': uid,
+                    'model' : thread._name,
+                    'res_id': thread.id,
+                    'date': email_date or time.strftime('%Y-%m-%d %H:%M:%S'),
+                    'body': details,
+                    'email_to': email,
+                    'email_from': email_from or \
+                        (hasattr(thread, 'user_id') and thread.user_id and thread.user_id.address_id and \
+                         thread.user_id.address_id.email),
+                    'email_cc': email_cc,
+                    'email_bcc': email_bcc,
+                    'partner_id': partner_id,
+                    'references': references,
+                    'message_id': message_id,
+                    'attachment_ids': [(6, 0, attachments)],
+                    'state' : 'received',
+                }
+            obj.create(cr, uid, data, context=context)
+        return True
+
+    def email_forward(self, cr, uid, model, res_ids, msg, email_error=False, context=None):
+        """Sends an email to all people following the thread
+        @param res_id: Id of the record of OpenObject model created from the email message
+        @param msg: email.message.Message object to forward
+        @param email_error: Default Email address in case of any Problem
+        """
+        model_pool = self.pool.get(model)
+        smtp_server_obj = self.pool.get('ir.mail_server')
+        email_message_obj = self.pool.get('email.message')
+        _decode_header = email_message_obj._decode_header
+        for res in model_pool.browse(cr, uid, res_ids, context=context):
+            if hasattr(model_pool, 'thread_followers'):
+                self.thread_followers = model_pool.thread_followers
+            thread_followers = self.thread_followers(cr, uid, [res.id])[res.id]
+            message_followers_emails = email_message_obj.to_email(','.join(filter(None, thread_followers)))
+            message_recipients = email_message_obj.to_email(','.join(filter(None,
+                                                         [_decode_header(msg['from']),
+                                                         _decode_header(msg['to']),
+                                                         _decode_header(msg['cc'])])))
+            message_forward = [i for i in message_followers_emails if (i and (i not in message_recipients))]
+
+            if message_forward:
+                # TODO: we need an interface for this for all types of objects, not just leads
+                if hasattr(res, 'section_id'):
+                    del msg['reply-to']
+                    msg['reply-to'] = res.section_id.reply_to
+
+                smtp_from = email_message_obj.to_email(msg['from'])
+                msg['from'] = smtp_from
+                msg['to'] =  message_forward
+                msg['message-id'] = tools.generate_tracking_message_id(res.id)
+                if not smtp_server_obj.send_email(cr, uid, msg) and email_error:
+                    subj = msg['subject']
+                    del msg['subject'], msg['to'], msg['cc'], msg['bcc']
+                    msg['subject'] = _('[OpenERP-Forward-Failed] %s') % subj
+                    msg['to'] = email_error
+                    smtp_server_obj.send_email(cr, uid, msg)
+        return True
+
+    def process_email(self, cr, uid, model, message, custom_values=None, attach=True, context=None):
+        """This function Processes email and create record for given OpenERP model
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param model: OpenObject Model
+        @param message: Email details, passed as a string or an xmlrpclib.Binary
+        @param attach: Email attachments
+        @param context: A standard dictionary for contextual values"""
+
+        # extract message bytes, we are forced to pass the message as binary because
+        # we don't know its encoding until we parse its headers and hence can't
+        # convert it to utf-8 for transport between the mailgate script and here.
+        if isinstance(message, xmlrpclib.Binary):
+            message = str(message.data)
+
+        if context is None:
+            context = {}
+
+        if custom_values is None or not isinstance(custom_values, dict):
+            custom_values = {}
+
+        model_pool = self.pool.get(model)
+        if self._name != model:
+            context.update({'thread_model':model})
+
+        email_message_pool = self.pool.get('email.message')
+        res_id = False
+
+
+        # Parse Message
+        # Warning: message_from_string doesn't always work correctly on unicode,
+        # we must use utf-8 strings here :-(
+        if isinstance(message, unicode):
+            message = message.encode('utf-8')
+        msg_txt = email.message_from_string(message)
+        msg = email_message_pool.parse_message(msg_txt)
+
+        # Create New Record into particular model
+        def create_record(msg):
+            new_res_id = self.message_new(cr, uid, msg, context=context)
+            if custom_values:
+                model_pool.write(cr, uid, [res_id], custom_values, context=context)
+            return new_res_id
+
+        res_id = False
+        if msg.get('references') or msg.get('in-reply-to'):
+            references = msg.get('references') or msg.get('in-reply-to')
+            if '\r\n' in references:
+                references = references.split('\r\n')
+            else:
+                references = references.split(' ')
+            for ref in references:
+                ref = ref.strip()
+                res_id = tools.reference_re.search(ref)
+                if res_id:
+                    res_id = res_id.group(1)
+                else:
+                    res_id = tools.res_re.search(msg['subject'])
+                    if res_id:
+                        res_id = res_id.group(1)
+                if res_id:
+                    res_id = int(res_id)
+                    if model_pool.exists(cr, uid, res_id):
+                        self.message_update(cr, uid, [res_id], msg, {}, context=context)
+
+        if not res_id:
+            res_id = create_record(msg)
+
+        #To forward the email to other followers
+        self.email_forward(cr, uid, model, [res_id], msg_txt)
+        return res_id
+
+    def get_partner(self, cr, uid, from_email, context=None):
+        """This function returns partner Id based on email passed
+        @param self: The object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks
+        @param from_email: email address based on that function will search for the correct
+        """
+        address_pool = self.pool.get('res.partner.address')
+        email_message_pool = self.pool.get('email.message')
+        res = {
+            'partner_address_id': False,
+            'partner_id': False
+        }
+        from_email = email_message_pool.to_email(from_email)[0]
+        address_ids = address_pool.search(cr, uid, [('email', 'like', from_email)])
+        if address_ids:
+            address = address_pool.browse(cr, uid, address_ids[0])
+            res['partner_address_id'] = address_ids[0]
+            res['partner_id'] = address.partner_id.id
+
+        return res
+
+email_thread()
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'mail/email_thread_view.xml'
--- mail/email_thread_view.xml	1970-01-01 00:00:00 +0000
+++ mail/email_thread_view.xml	2011-04-11 12:37:32 +0000
@@ -0,0 +1,90 @@
+<?xml version="1.0"?>
+<openerp>
+    <data>
+
+        <record model="ir.ui.view" id="view_mailgate_thread_form">
+            <field name="name">email.thread.form</field>
+            <field name="model">email.thread</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Mailgateway Thread">
+                    <separator string="History" colspan="4"/>
+                    <field name="message_ids" nolabel="1" colspan="4" mode="tree,form">
+                        <tree string="Mailgateway History">
+                            <field name="display_text"/>
+                        </tree>
+                        <form string="Mailgate History">
+                            <field name="subject" widget="char"/>
+                            <field name="date"/>
+                            <field name="user_id"/>
+                            <field name="message_id"/>
+                            <field name="history"/>
+                            <notebook colspan="4">
+                                <page string="Email Details">
+                                    <group col="4" colspan="4">
+                                        <separator string="Email Details" colspan="4"/>
+                                        <field name="email_from"/>
+                                        <field name="email_to"/>
+                                        <field name="email_cc"/>
+                                        <field name="email_bcc"/>
+                                   </group>
+                                </page>
+                                <page string="Attachments">
+                                    <separator string="Attachments" colspan="4"/>
+                                    <field name="attachment_ids" nolabel="1" colspan="4" />
+                                </page>
+                            </notebook>
+                        </form>
+                    </field>
+                </form>
+            </field>
+        </record>
+
+        <record model="ir.ui.view" id="view_mailgate_thread_tree">
+            <field name="name">email.thread.tree</field>
+            <field name="model">email.thread</field>
+            <field name="type">tree</field>
+            <field name="arch" type="xml">
+                <tree string="Mailgateway Thread">
+                    <field name="message_ids" />
+                </tree>
+            </field>
+        </record>
+
+         <!--        Emails action-->
+        <record model="ir.actions.act_window" id="action_view_mailgate_thread">
+            <field name="name">Mailgateway Threads</field>
+            <field name="res_model">email.thread</field>
+            <field name="view_mode">tree,form</field>
+            <field name="view_type">form</field>
+            <field name="view_id" ref="view_mailgate_thread_tree"/>
+        </record>
+
+        <record model="ir.actions.act_window.view" id="action_view_mailgate_thread_view1">
+            <field name="sequence" eval="1"/>
+            <field name="view_mode">tree</field>
+            <field name="view_id" ref="view_mailgate_thread_tree"/>
+            <field name="act_window_id" ref="action_view_mailgate_thread"/>
+        </record>
+        <record model="ir.actions.act_window.view" id="action_view_mailgate_thread_view2">
+            <field name="sequence" eval="2"/>
+            <field name="view_mode">form</field>
+            <field name="view_id" ref="view_mailgate_thread_form"/>
+            <field name="act_window_id" ref="action_view_mailgate_thread"/>
+        </record>
+
+        <record model="ir.actions.act_window.view" id="action_view_mailgate_thread_view1">
+            <field name="sequence" eval="1"/>
+            <field name="view_mode">tree</field>
+            <field name="view_id" ref="view_mailgate_thread_tree"/>
+            <field name="act_window_id" ref="action_view_mailgate_thread"/>
+        </record>
+        <record model="ir.actions.act_window.view" id="action_view_mailgate_thread_view2">
+            <field name="sequence" eval="2"/>
+            <field name="view_mode">form</field>
+            <field name="view_id" ref="view_mailgate_thread_form"/>
+            <field name="act_window_id" ref="action_view_mailgate_thread"/>
+        </record>
+
+    </data>
+</openerp>

=== renamed file 'mail_gateway/mail_gateway_view.xml' => 'mail/email_view.xml'
--- mail_gateway/mail_gateway_view.xml	2011-01-14 00:11:01 +0000
+++ mail/email_view.xml	2011-04-11 12:37:32 +0000
@@ -2,219 +2,150 @@
 <openerp>
     <data>
 
-        <menuitem id="base.menu_mail_gateway" name="Emails"
-            parent="base.menu_base_config" sequence="1" />
+        <menuitem name="Configuration" parent="base.menu_tools"
+              id="base.menu_lunch_survey_root" sequence="20"/>
 
-        <record model="ir.ui.view" id="view_mailgate_message_form">
-          <field name="name">mailgate.message.form</field>
-            <field name="model">mailgate.message</field>
-            <field name="type">form</field>
-            <field name="arch" type="xml">
-             <form string="mailgate message">
+        <record model="ir.ui.view" id="view_email_message_form">
+            <field name="name">email.message.form</field>
+			<field name="model">email.message</field>
+			<field name="type">form</field>
+			<field name="arch" type="xml">
+                <form string="Email message">
                     <group colspan="4" col="6">
-                        <field name="name" string="Subject" required="1" select="1"  widget="char" size="512"/>
-                        <field name="date" required="1" select="1"/>
-                        <field name="user_id" string="Owner" select="1"/>
-                        <field name="partner_id" readonly="1" />
+                       <field name="subject" widget="char" size="512"/>
+                       <field name="date"/>
+                       <field name="user_id" string="User"/>
+                       <field name="partner_id" readonly="1" />
+                       <field name="priority"/>
                     </group>
                     <notebook colspan="4">
                         <page string="Details">
                             <group col="2" colspan="2">
                                 <separator string="Email Followers" colspan="4"/>
-                                <field name="email_from" />
-                                <field name="email_to" widget="char" size="512"/>
-                                <field name="email_cc" widget="char" size="512"/>
-                                <field name="email_bcc" widget="char" size="512" groups="base.group_extended"/>
-                           </group>
-                           <group col="2" colspan="2">
+                                <field name="email_from"/>
+                                <field name="email_to"/>
+                                <field name="email_cc"/>
+                                <field name="email_bcc" groups="base.group_extended"/>
+                                <field name="reply_to"/>
+                            </group>
+                            <group col="2" colspan="2">
                                 <separator string="Message Details" colspan="4"/>
                                 <field name="model" readonly="1"/>
                                 <group col="3" colspan="2">
-                                    <field name="res_id"  readonly="1"/>
+                                    <field name="res_id" readonly="1" groups="base.group_extended"/>
                                     <button name="open_document" string="Open Document" type="object" icon="gtk-jump-to"/>
-                                </group>
-                                <field name="message_id" />
-                                <field name="ref_id" />
-                           </group>
-                           <separator string="Description" colspan="4"/>
-                            <field name="description" nolabel="1" colspan="4" />
+	                                <field name="message_id" groups="base.group_extended" colspan="4"/>
+                                </group>
+                                <field name="references" widget="char" size="4096" groups="base.group_extended"/>
+                            </group>
+                            <separator string="Description" colspan="4"/>
+                            <field name="body" nolabel="1" colspan="4"/>
+                            <separator string="" colspan="4"/>
+                            <group col="6" colspan="4">
+                                <field name="state" colspan="2"/>
+                                <group col="4" colspan="2">
+                                	<button name="%(action_email_compose_message_wizard)d" string="Reply" type="action" icon="terp-mail-replied"
+	                                context="{'mail':'reply', 'message_id':active_id}" states='received,outgoing,sent,exception,cancel'/>
+	                                <button name="send_email" string="Force Send" type="object" icon="gtk-execute" states='outgoing'/>
+	                                <button name="process_retry" string="Send Again" type="object" icon="gtk-execute" states='exception,cancel'/>
+	                                <button name="do_cancel" string="Cancel" type="object" icon="terp-gtk-stop" states='outgoing'/>
+                                </group>
+                            </group>
                         </page>
                         <page string="Attachments">
                             <separator string="Attachments" colspan="4"/>
                             <field name="attachment_ids" nolabel="1" colspan="4" readonly="1"/>
                         </page>
+                        <page string="Advanced">
+                            <group col="4" colspan="4">
+                                <field name="smtp_server_id"/>
+	                            <field name="sub_type"/>
+	                            <field name="debug" groups="base.group_extended"/>
+	                            <field name="history"/>
+	                            <field name="auto_delete"/>
+	                            <separator string="xheaders" colspan="4"/>
+	                            <field name="headers" colspan="4" nolabel="1" groups="base.group_extended"/>
+                            </group>
+                        </page>
                     </notebook>
                 </form>
             </field>
         </record>
 
-        <record model="ir.ui.view" id="view_mailgate_message_tree">
-            <field name="name">mailgate.message.tree</field>
-            <field name="model">mailgate.message</field>
+        <record model="ir.ui.view" id="view_email_message_tree">
+            <field name="name">email.message.tree</field>
+            <field name="model">email.message</field>
             <field name="type">tree</field>
             <field name="arch" type="xml">
-                <tree string="Emails">
-                    <field name="date" />
-                    <field name="name" string="Subject"/>
-                    <field name="email_from" string="From"/>
-                    <field name="user_id" string="Owner"/>
+            	<tree string="Emails" colors="grey:state in ('sent', 'cancel');blue:state=='outgoing';red:state=='exception';black:state=='received'">
+                    <field name="date"/>
+                    <field name="subject"/>
+                    <field name="email_from"/>
+                    <field name="user_id" string="User"/>
                     <field name="message_id" string="Message" invisible="1"/>
                     <field name="partner_id" invisible="1"/>
+                    <field name="state"/>
                     <button name="open_document" string="Open Document" type="object" icon="gtk-jump-to"/>
                     <button name="open_attachment" string="Open Attachments" type="object" icon="gtk-jump-to"/>
                 </tree>
             </field>
         </record>
 
-        <record model="ir.ui.view" id="view_mailgate_message_search">
-            <field name="name">mailgate.message.search</field>
-            <field name="model">mailgate.message</field>
+        <record model="ir.ui.view" id="view_email_message_search">
+            <field name="name">email.message.search</field>
+            <field name="model">email.message</field>
             <field name="type">search</field>
             <field name="arch" type="xml">
                 <search string="Email Search">
-                    <field name="name" string="Subject"/>
-                    <field name="date" />
-                    <field name="user_id" string="Owner"/>
+                    <separator orientation="vertical"/>
+                    <filter icon="terp-camera_test" string="Received" domain="[('state','=','received')]"/>
+                    <filter icon="terp-call-start" name="outgoing" string="Outgoing" domain="[('state','=','outgoing')]"/>
+                    <filter icon="terp-gtk-stop" string="Exception" domain="[('state','=','exception')]"/>
+                    <field name="email_from"/>
+                    <field name="subject"/>
+                    <field name="date"/>
+                    <field name="user_id" string="User"/>
                     <field name="partner_id" string="Partner Name"/>
                     <newline/>
                     <group expand="0" string="Group By..." groups="base.group_extended">
+                        <filter string="State" icon="terp-stock_effects-object-colorize" domain="[]" context="{'group_by':'state'}"/>
                         <filter string="Partner" icon="terp-partner" domain="[]" context="{'group_by':'partner_id'}"/>
-                        <filter string="Owner" name="User" icon="terp-personal" context="{'group_by':'user_id'}"/>
-                        <separator orientation="vertical"/>
-                         <filter string="Thread" icon="terp-mail-" domain="[]" context="{'group_by':'message_id'}"/>
-                         <separator orientation="vertical"/>
-                         <filter string="Month" help="Creation Month" icon="terp-go-month" domain="[]" context="{'group_by':'date'}"/>
+                        <filter string="User" name="User" icon="terp-personal" context="{'group_by':'user_id'}"/>
+                        <separator orientation="vertical"/>
+                        <filter string="Thread" icon="terp-mail-" domain="[]" context="{'group_by':'message_id'}"/>
+                        <separator orientation="vertical"/>
+                        <filter string="Month" help="Creation Month" icon="terp-go-month" domain="[]" context="{'group_by':'date'}"/>
                     </group>
                 </search>
             </field>
         </record>
 
-
-
-
         <record id="action_view_mail_message" model="ir.actions.act_window">
             <field name="name">Messages</field>
-            <field name="res_model">mailgate.message</field>
-            <field name="view_type">form</field>
-            <field name="view_mode">tree,form</field>
-            <field name="search_view_id" ref="view_mailgate_message_search"/>
-        </record>
-
-
-
-
-        <record model="ir.ui.view" id="view_mailgate_thread_form">
-            <field name="name">mailgate.thread.form</field>
-            <field name="model">mailgate.thread</field>
-            <field name="type">form</field>
-            <field name="arch" type="xml">
-                <form string="Mailgateway Thread">
-                    <separator string="History" colspan="4"/>
-                    <field name="message_ids" nolabel="1" colspan="4" mode="tree,form">
-                        <tree string="Mailgateway History">
-                            <field name="display_text" />
-                        </tree>
-                        <form string="Mailgate History">
-                            <field name="name" widget="char" size="512"/>
-                            <field name="date" />
-                            <field name="user_id" />
-                            <field name="message_id" />
-                            <field name="history" />
-                            <notebook colspan="4">
-                                <page string="Email Details">
-                                    <group col="4" colspan="4">
-                                        <separator string="Email Details" colspan="4"/>
-                                        <field name="email_from" />
-                                        <field name="email_to" widget="char" size="512"/>
-                                        <field name="email_cc" widget="char" size="512"/>
-                                        <field name="email_bcc" widget="char" size="512"/>
-                                   </group>
-                                </page>
-                                <page string="Attachments">
-                                    <separator string="Attachments" colspan="4"/>
-                                    <field name="attachment_ids" nolabel="1" colspan="4" />
-                                </page>
-                            </notebook>
-                        </form>
-                    </field>
-                </form>
-            </field>
-        </record>
-
-        <record model="ir.ui.view" id="view_mailgate_thread_tree">
-            <field name="name">mailgate.thread.tree</field>
-            <field name="model">mailgate.thread</field>
-            <field name="type">tree</field>
-            <field name="arch" type="xml">
-                <tree string="Mailgateway Thread">
-                    <field name="message_ids" />
-                </tree>
-            </field>
-        </record>
-
-
-
-
-         <!--        Emails action-->
-        <record model="ir.actions.act_window" id="action_view_mailgate_thread">
-            <field name="name">Mailgateway Threads</field>
-            <field name="res_model">mailgate.thread</field>
-            <field name="view_mode">tree,form</field>
-            <field name="view_type">form</field>
-            <field name="view_id" ref="view_mailgate_thread_tree"/>
-        </record>
-
-        <record model="ir.actions.act_window.view" id="action_view_mailgate_thread_view1">
-            <field name="sequence" eval="1"/>
-            <field name="view_mode">tree</field>
-            <field name="view_id" ref="view_mailgate_thread_tree"/>
-            <field name="act_window_id" ref="action_view_mailgate_thread"/>
-        </record>
-        <record model="ir.actions.act_window.view" id="action_view_mailgate_thread_view2">
-            <field name="sequence" eval="2"/>
-            <field name="view_mode">form</field>
-            <field name="view_id" ref="view_mailgate_thread_form"/>
-            <field name="act_window_id" ref="action_view_mailgate_thread"/>
-        </record>
-
-        <!--        Emailsaction-->
-        <record model="ir.actions.act_window" id="action_view_mailgate_message">
-            <field name="name">Emails</field>
-            <field name="res_model">mailgate.message</field>
-            <field name="view_mode">tree,form</field>
-            <field name="view_type">form</field>
+            <field name="res_model">email.message</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">tree,form</field>
             <field name="domain">[('history', '=', True)]</field>
-            <field name="view_id" ref="view_mailgate_message_tree"/>
-        </record>
-
-        <record model="ir.actions.act_window.view" id="action_view_mailgate_thread_view1">
-            <field name="sequence" eval="1"/>
-            <field name="view_mode">tree</field>
-            <field name="view_id" ref="view_mailgate_thread_tree"/>
-            <field name="act_window_id" ref="action_view_mailgate_thread"/>
-        </record>
-        <record model="ir.actions.act_window.view" id="action_view_mailgate_thread_view2">
-            <field name="sequence" eval="2"/>
-            <field name="view_mode">form</field>
-            <field name="view_id" ref="view_mailgate_thread_form"/>
-            <field name="act_window_id" ref="action_view_mailgate_thread"/>
+             <field name="context">{'search_default_outgoing':1}</field>
+            <field name="search_view_id" ref="view_email_message_search"/>
         </record>
 
         <act_window domain="[('partner_id', '=', active_id), ('history', '=', True)]"
             id="act_res_partner_emails" name="Emails"
-            res_model="mailgate.message"
+            res_model="email.message"
             src_model="res.partner"
-            view_id="view_mailgate_message_tree"
-            />
-
-    <act_window
-       id="act_res_partner_open_email" name="Attachments"
-       res_model="ir.attachment"
-       src_model="mailgate.message"
-       domain="[('res_id', '=', res_id),('res_model','=',model)]"/>
-
-           <menuitem id="base.menu_email_gateway_form"
-               parent="base.menu_mail_gateway" action="action_view_mail_message" />
+            view_id="view_email_message_tree"/>
+
+        <menuitem name="Email" id="menu_email_message_tools"
+            parent="base.menu_tools" />
+
+        <menuitem name="Messages"
+            id="menu_email_message"
+            parent="menu_email_message_tools"
+            action="action_view_mail_message" />
+
+
+        <menuitem name="Email" id="menu_config_email" parent="base.menu_lunch_survey_root" sequence="20"/>
 
     </data>
 </openerp>

=== modified file 'mail/res_partner.py'
--- mail_gateway/res_partner.py	2011-01-14 00:11:01 +0000
+++ mail/res_partner.py	2011-04-11 12:37:32 +0000
@@ -19,17 +19,16 @@
 #
 ##############################################################################
 
-from osv import fields,osv
+from osv import osv
+from osv import fields
 
 class res_partner(osv.osv):
     """ Inherits partner and adds CRM information in the partner form """
     _inherit = 'res.partner'
     _columns = {
-        'emails': fields.one2many('mailgate.message', 'partner_id',\
-                                        'Emails', readonly=True, domain=[('history','=',True)]),
+        'emails': fields.one2many('email.message', 'partner_id', 'Emails', readonly=True, domain=[('history','=',True)]),
     }
 
 res_partner()
 
-
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'mail/res_partner_view.xml'
--- mail_gateway/res_partner_view.xml	2011-01-14 00:11:01 +0000
+++ mail/res_partner_view.xml	2011-04-11 12:37:32 +0000
@@ -3,28 +3,28 @@
     <data>
 
          <!--  Partners inherited form -->
-         <record id="base.view_crm_partner_info_History" model="ir.ui.view">
-             <field name="name">res.partner.crm.history.inherit1</field>
-             <field name="model">res.partner</field>
-             <field name="type">form</field>
-             <field name="inherit_id" ref="base.view_partner_form"/>
-             <field name="arch" type="xml">
-                 <xpath expr="/form/notebook/page[@string='History']" position="attributes">
-                     <attribute name="invisible">False</attribute>
-                 </xpath>
-             </field>
-             </record>
+        <record id="base.view_crm_partner_info_History" model="ir.ui.view">
+            <field name="name">res.partner.crm.history.inherit1</field>
+            <field name="model">res.partner</field>
+            <field name="type">form</field>
+            <field name="inherit_id" ref="base.view_partner_form"/>
+            <field name="arch" type="xml">
+                <xpath expr="/form/notebook/page[@string='History']" position="attributes">
+                    <attribute name="invisible">False</attribute>
+                </xpath>
+            </field>
+        </record>
         <record id="view_emails_partner_info_form" model="ir.ui.view">
             <field name="name">res.partner.emails.info.inherit</field>
             <field name="model">res.partner</field>
             <field name="type">form</field>
             <field name="inherit_id" ref="base.view_partner_form"/>
             <field name="arch" type="xml">
-                 <page string="History" position="inside">
-                      <field name="emails" colspan="4" nolabel="1"/>
-                 </page>
+                <page string="History" position="inside">
+                    <field name="emails" colspan="4" nolabel="1"/>
+                </page>
             </field>
-       </record>
+        </record>
+
    </data>
-</openerp>
-
+</openerp>
\ No newline at end of file

=== modified file 'mail/scripts/openerp_mailgate/openerp_mailgate.py'
--- mail_gateway/scripts/openerp_mailgate/openerp_mailgate.py	2011-01-14 00:11:01 +0000
+++ mail/scripts/openerp_mailgate/openerp_mailgate.py	2011-04-11 12:37:32 +0000
@@ -54,7 +54,7 @@
         try:
             # pass message as bytes because we don't know its encoding until we parse its headers
             # and hence can't convert it to utf-8 for transport
-            res_id = self.rpc('email.server.tools', 'process_email', self.model, xmlrpclib.Binary(message), custom_values)
+            res_id = self.rpc('email.thread', 'process_email', self.model, xmlrpclib.Binary(message), custom_values)
         except Exception:
             logger = logging.getLogger('mail-gateway')
             logger.warning('Failed to process incoming email. Source of the failed mail is available at debug level.', exc_info=True)

=== modified file 'mail/security/ir.model.access.csv'
--- mail_gateway/security/ir.model.access.csv	2011-01-14 00:11:01 +0000
+++ mail/security/ir.model.access.csv	2011-04-11 12:37:32 +0000
@@ -1,5 +1,3 @@
 "id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
-"mail_gateway_mailgate_message","mail_gateway.mailgate.message","model_mailgate_message","base.group_system",1,1,1,1
-"mail_gateway_mailgate_thread","mail_gateway.mailgate.thread","model_mailgate_thread","base.group_system",1,1,1,1
-"mail_gateway_message_internal_user","mail_gateway.mailgate.message.internal","model_mailgate_message","base.group_user",1,0,0,0
-"mail_gateway_mailgate_message_partner_manager","mail_gateway.mailgate.message.partner.manager","model_mailgate_message","base.group_partner_manager",1,1,1,1
+"access_email_message","email.message","model_email_message",,1,0,0,0
+"access_mailgate_thread","email.thread","model_email_thread",,1,0,0,0

=== added directory 'mail/wizard'
=== added file 'mail/wizard/__init__.py'
--- mail/wizard/__init__.py	1970-01-01 00:00:00 +0000
+++ mail/wizard/__init__.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+import email_compose_message
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
\ No newline at end of file

=== added file 'mail/wizard/email_compose_message.py'
--- mail/wizard/email_compose_message.py	1970-01-01 00:00:00 +0000
+++ mail/wizard/email_compose_message.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,215 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+from osv import osv
+from osv import fields
+import tools
+
+class email_compose_message(osv.osv_memory):
+    _name = 'email.compose.message'
+    _inherit = 'email.message.common'
+    _description = 'This is the wizard for Compose E-mail'
+
+    def default_get(self, cr, uid, fields, context=None):
+        if context is None:
+            context = {}
+        result = super(email_compose_message, self).default_get(cr, uid, fields, context=context)
+        vals = {}
+        if context.get('active_model') and context.get('active_id') and not context.get('mail')=='reply':
+            vals = self.get_value(cr, uid, context.get('active_model'), context.get('active_id'), context)
+
+        elif context.get('mail')=='reply' and context.get('active_id', False):
+            vals = self.get_message_data(cr, uid, int(context.get('active_id', False)), context)
+
+        else:
+            result['model'] = context.get('active_model', False)
+
+        if not vals:
+            return result
+
+        if 'subject' in fields:
+            result.update({'subject' : vals.get('subject','')})
+
+        if 'email_to' in fields:
+            result.update({'email_to' : vals.get('email_to','')})
+
+        if 'email_from' in fields:
+            result.update({'email_from' : vals.get('email_from','')})
+
+        if 'body' in fields:
+            result.update({'body' : vals.get('body','')})
+
+        if 'model' in fields:
+            result.update({'model' : vals.get('model','')})
+
+        if 'email_cc' in fields:
+            result.update({'email_cc' : vals.get('email_cc','')})
+
+        if 'email_bcc' in fields:
+            result.update({'email_bcc' : vals.get('email_bcc','')})
+
+        if 'res_id' in fields:
+            result.update({'res_id' : vals.get('res_id',0)})
+
+        if 'reply_to' in fields:
+            result['reply_to'] = vals.get('reply_to','')
+
+        if 'message_id' in fields:
+            result['message_id'] =  vals.get('message_id','')
+
+        if 'attachment_ids' in fields:
+            result['attachment_ids'] = vals.get('attachment_ids',[])
+
+        if 'user_id' in fields:
+            result['user_id'] = vals.get('user_id',False)
+
+        if 'references' in fields:
+            result['references'] = vals.get('references',False)
+
+        if 'sub_type' in fields:
+            result['sub_type'] = vals.get('sub_type',False)
+
+        if 'headers' in fields:
+            result['headers'] = vals.get('headers',False)
+
+        if 'priority' in fields:
+            result['priority'] = vals.get('priority',False)
+
+        if 'debug' in fields:
+            result['debug'] = vals.get('debug',False)
+
+        return result
+
+    _columns = {
+        'attachment_ids': fields.many2many('ir.attachment','email_message_send_attachment_rel', 'wizard_id', 'attachment_id', 'Attachments'),
+    }
+
+    def get_value(self, cr, uid, model, res_id, context=None):
+        return {}
+
+    def get_message_data(self, cr, uid, message_id, context=None):
+        if context is None:
+            context = {}
+        result = {}
+        message_pool = self.pool.get('email.message')
+        if message_id:
+            message_data = message_pool.browse(cr, uid, message_id, context)
+            subject = tools.ustr(message_data and message_data.subject or '')
+            if context.get('mail','') == 'reply':
+                subject = "Re :- " + subject
+
+            description =  message_data and message_data.body  or ''
+            message_body = False
+            if context.get('mail','') == 'reply':
+                header = '-------- Original Message --------'
+                sender = 'From: %s'  % tools.ustr(message_data.email_from or '')
+                email_to = 'To: %s' %  tools.ustr(message_data.email_to or '')
+                sentdate = 'Date: %s' % message_data.date
+                desc = '\n > \t %s' % tools.ustr(description.replace('\n', "\n > \t") or '')
+                description = '\n'.join([header, sender, email_to, sentdate, desc])
+
+            result.update({
+                    'body' : description,
+                    'subject' : subject,
+                    'message_id' :  message_data and message_data.message_id or False,
+                    'attachment_ids' : message_data and message_pool.read(cr, uid, message_id, ['attachment_ids'])['attachment_ids'] or [],
+                    'res_id' : message_data and message_data.res_id or False,
+                    'email_from' : message_data and message_data.email_to or False,
+                    'email_to' : message_data and message_data.email_from or False,
+                    'email_cc' : message_data and message_data.email_cc or False,
+                    'email_bcc' : message_data and message_data.email_bcc or False,
+                    'reply_to' : message_data and message_data.reply_to or False,
+                    'model' : message_data and message_data.model or False,
+                    'user_id' : message_data and message_data.user_id and message_data.user_id.id or False,
+                    'references' : message_data and message_data.references and tools.ustr(message_data.references) or False,
+                    'sub_type' : message_data and message_data.sub_type or False,
+                    'headers' : message_data and message_data.headers or False,
+                    'priority' : message_data and message_data.priority or False,
+                    'debug': message_data and message_data.debug or False
+                })
+
+        return result
+
+    def send_mail(self, cr, uid, ids, context=None):
+        if context is None:
+            context = {}
+
+        record = self.browse(cr, uid, ids[0], context=context)
+        if context.get('mass_mail') and context['active_ids'] and context.get('template_id'):
+            email_message_pool = self.pool.get('email.message')
+            email_temp_pool = self.pool.get('email.template')
+            for res_id in context['active_ids']:
+                subject = email_temp_pool.get_template_value(cr, uid, record.subject, context['active_model'], res_id)
+                body = email_temp_pool.get_template_value(cr, uid, record.body, context['active_model'], res_id)
+                email_to = email_temp_pool.get_template_value(cr, uid, record.email_to, context['active_model'], res_id)
+                email_from = email_temp_pool.get_template_value(cr, uid, record.email_from, context['active_model'], res_id)
+                email_cc = email_temp_pool.get_template_value(cr, uid, record.email_cc, context['active_model'], res_id)
+                reply_to = email_temp_pool.get_template_value(cr, uid, record.reply_to, context['active_model'], res_id)
+
+                email_id = email_message_pool.schedule_with_attach(cr, uid, email_from,
+                           email_to, subject or False, body or False, context['active_model'], email_cc or False, openobject_id=int(res_id),
+                           context=context)
+            return {'type': 'ir.actions.act_window_close'}
+
+        if context.get('mass_mail') and context.get('active_ids') and not context.get('template_id'):
+            self.do_mass_mail(cr, uid, context['active_ids'], record.subject or False, record.body or False, context=context)
+            return {'type': 'ir.actions.act_window_close'}
+
+        email_id = self.save_to_mailbox(cr, uid, ids, context)
+        return {'type': 'ir.actions.act_window_close'}
+
+    def do_mass_mail(self, cr, uid, ids, subject, body, context=None):
+        if context is None:
+            context = {}
+
+        if context.get('active_model'):
+            email_message_pool = self.pool.get('email.message')
+            model_pool = self.pool.get(context['active_model'])
+            for data in model_pool.browse(cr, uid, ids, context=context):
+                email_id = email_message_pool.schedule_with_attach(cr, uid,
+                    data.user_id and data.user_id.address_id and data.user_id.address_id.email or False,
+                    data.email_from or False, subject, body, model=context['active_model'],
+                    email_cc=tools.ustr(data.email_cc or ''), openobject_id=int(data.id), context=context)
+        return True
+
+    def save_to_mailbox(self, cr, uid, ids, context=None):
+        email_ids = []
+        email_message_pool = self.pool.get('email.message')
+        attachment = []
+        for mail in self.browse(cr, uid, ids, context=context):
+            for attach in mail.attachment_ids:
+                attachment.append((attach.datas_fname, attach.datas))
+            references = False
+            message_id = False
+            if context.get('mail',False) == 'reply' and  mail.message_id:
+                references = mail.references and mail.references + "," + mail.message_id or mail.message_id
+            else:
+                message_id = mail.message_id
+            email_id = email_message_pool.schedule_with_attach(cr, uid, mail.email_from, mail.email_to, mail.subject, mail.body,
+                    model=mail.model, email_cc=mail.email_cc, email_bcc=mail.email_bcc, reply_to=mail.reply_to,
+                    attach=attachment, message_id=message_id, references=references, openobject_id=int(mail.res_id),
+                    subtype=mail.sub_type, x_headers=mail.headers, priority=mail.priority, smtp_server_id=mail.smtp_server_id and mail.smtp_server_id.id, context=context)
+            email_ids.append(email_id)
+        return email_ids
+
+email_compose_message()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'mail/wizard/email_compose_message_view.xml'
--- mail/wizard/email_compose_message_view.xml	1970-01-01 00:00:00 +0000
+++ mail/wizard/email_compose_message_view.xml	2011-04-11 12:37:32 +0000
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+	<data>
+
+        <record model="ir.ui.view" id="email_compose_message_wizard_form">
+            <field name="name">email.compose.message.form</field>
+            <field name="model">email.compose.message</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Reply Email">
+                    <group col="6" colspan="4">
+                        <field name="model" invisible="1"/>
+                        <field name="smtp_server_id" widget="selection" colspan="4"/>
+                        <field name="email_from" colspan="4" required="1"/>
+                        <field name="email_to" colspan="4" required="1"/>
+                        <field name="email_cc" colspan="4"/>
+                        <field name="email_bcc" colspan="4"/>
+                        <field name="reply_to" colspan="4"/>
+                        <field name="subject" colspan="4" widget="char" size="512"/>
+                    </group>
+                    <separator string="" colspan="4"/>
+                    <notebook colspan="4">
+                        <page string="Body">
+                            <field name="body" colspan="4" nolabel="1"/>
+                        </page>
+                        <page string="Attachments">
+                            <label string="Add here all attachments of the current document you want to include in the Email." colspan="4"/>
+                            <field name="attachment_ids" colspan="4" nolabel="1"/>
+                        </page>
+                    </notebook>
+                    <group col="4" colspan="4">
+                        <label string="" colspan="1"/>
+                        <button icon="gtk-close" special="cancel" string="Close"/>
+                        <button icon="gtk-ok" name="send_mail" string="Send now" type="object"/>
+                    </group>
+                </form>
+            </field>
+        </record>
+
+        <record id="action_email_compose_message_wizard" model="ir.actions.act_window">
+            <field name="name">Send E-mail</field>
+            <field name="res_model">email.compose.message</field>
+            <field name="src_model">email.compose.message</field>
+            <field name="type">ir.actions.act_window</field>
+            <field name="view_type">form</field>
+            <field name="view_mode">form</field>
+            <field name="target">new</field>
+        </record>
+
+	</data>
+</openerp>

=== modified file 'marketing_campaign/marketing_campaign.py'
--- marketing_campaign/marketing_campaign.py	2011-01-14 00:11:01 +0000
+++ marketing_campaign/marketing_campaign.py	2011-04-11 12:37:32 +0000
@@ -777,7 +777,7 @@
 class email_template(osv.osv):
     _inherit = "email.template"
     _defaults = {
-        'object_name': lambda obj, cr, uid, context: context.get('object_id',False),
+        'model_id': lambda obj, cr, uid, context: context.get('object_id',False),
     }
 
     # TODO: add constraint to prevent disabling / disapproving an email account used in a running campaign

=== modified file 'marketing_campaign/marketing_campaign_view.xml'
--- marketing_campaign/marketing_campaign_view.xml	2011-01-27 09:49:39 +0000
+++ marketing_campaign/marketing_campaign_view.xml	2011-04-11 12:37:32 +0000
@@ -266,7 +266,7 @@
                     <field name="type"/>
                     <group colspan='2' col='1'>
                         <field name="email_template_id" attrs="{'required':[('type','=','email')], 'invisible':[('type','!=','email')]}"
-                               context="{'default_object_name':object_id}" />
+                               context="{'default_model_id':object_id}" />
                         <group attrs="{'invisible':[('type','!=','report')]}" >
                             <field name="report_id" attrs="{'required':[('type','=','report')]}" context="{'object_id':object_id}"/>
                             <field name="report_directory_id" attrs="{'required':[('type','=','report')]}" />

=== modified file 'marketing_campaign/security/ir.model.access.csv'
--- marketing_campaign/security/ir.model.access.csv	2011-01-14 00:11:01 +0000
+++ marketing_campaign/security/ir.model.access.csv	2011-04-11 12:37:32 +0000
@@ -7,7 +7,7 @@
 "access_marketing_campaign_analysis_campaignadmin","campaign.analysis","model_campaign_analysis","marketing.group_marketing_user",1,1,0,0
 "access_marketing_campaign_workitem_all","marketing.campaign.workitem","model_marketing_campaign_workitem","base.group_user",1,0,0,0
 "access_email_template_user","email.template","model_email_template","marketing.group_marketing_user",1,1,0,0
-"access_email_template_account_user","email_template.account.user","email_template.model_email_template_account","marketing.group_marketing_user",1,1,0,0
+"access_email_template_account_user","ir.mail_server","base.model_ir_mail_server","marketing.group_marketing_user",1,1,0,0
 "access_marketing_campaign_system","marketing.campaign system","model_marketing_campaign","base.group_system",1,0,0,0
 "access_marketing_campaign_segment_system","marketing.campaign.segment system","model_marketing_campaign_segment","base.group_system",1,0,0,0
 "access_marketing_campaign_workitem_system","marketing.campaign.workitem system","model_marketing_campaign_workitem","base.group_system",1,0,0,0
@@ -18,4 +18,4 @@
 "access_marketing_campaign_transition_campaign_manager","marketing.campaign.transition","model_marketing_campaign_transition","marketing.group_marketing_manager",1,1,1,1
 "access_marketing_campaign_analysis_campaign_manager","campaign.analysis","model_campaign_analysis","marketing.group_marketing_manager",1,1,1,1
 "access_email_template_manager","email.template","model_email_template","marketing.group_marketing_manager",1,1,1,1
-"access_email_template_account_manager","email_template.account.manager","email_template.model_email_template_account","marketing.group_marketing_manager",1,1,1,1
+"access_email_template_account_manager","ir.mail_server","base.model_ir_mail_server","marketing.group_marketing_manager",1,1,1,1

=== modified file 'outlook/__openerp__.py'
--- outlook/__openerp__.py	2011-03-18 14:06:18 +0000
+++ outlook/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -25,17 +25,15 @@
     'version' : '1.0',
     'author' : 'OpenERP SA',
     'website' : 'http://www.openerp.com/',
-    'depends' : ['base', 'mail_gateway'],
+    'depends' : ['base', 'mail'],
     'category' : 'Tools',
     'description': '''
 This module provides the Outlook Plug-in.
 =========================================
-
 Outlook plug-in allows you to select an object that you’d like to add
 to your email and its attachments from MS Outlook. You can select a partner, a task,
 a project, an analytical account, or any other object and archive selected
-mail in mailgate.messages with attachments.
-
+mail into email.messages with attachments.
       ''',
     'init_xml' : [],
     'demo_xml' : [],

=== modified file 'outlook/plugin/openerp-outlook-plugin/tiny_xmlrpc.py'
--- outlook/plugin/openerp-outlook-plugin/tiny_xmlrpc.py	2011-02-15 12:50:01 +0000
+++ outlook/plugin/openerp-outlook-plugin/tiny_xmlrpc.py	2011-04-11 12:37:32 +0000
@@ -178,14 +178,14 @@
             model = rec[0]
             res_id = rec[1]
             #Check if mailgate installed
-            object_id = execute ( conn,'execute',self._dbname,int(self._uid),self._pwd,'ir.model','search',[('model','=','mailgate.message')])
+            object_id = execute ( conn,'execute',self._dbname,int(self._uid),self._pwd,'ir.model','search',[('model','=','email.message')])
             if not object_id:
             	win32ui.MessageBox("Mailgate is not installed on your configured database '%s' !!\n\nPlease install it to archive the mail."%(self._dbname),"Mailgate not installed",win32con.MB_ICONERROR)
             	return
             object_ids = execute ( conn,'execute',self._dbname,int(self._uid),self._pwd,'ir.model','search',[('model','=',model)])
             object_name  = execute( conn,'execute',self._dbname,int(self._uid),self._pwd,'ir.model','read',object_ids,['name'])[0]['name']
             #Reading the Object ir.model Name
-            ext_ids = execute(conn,'execute',self._dbname,int(self._uid),self._pwd,'mailgate.message','search',[('message_id','=',message_id),('model','=',model),('res_id','=',res_id)])
+            ext_ids = execute(conn,'execute',self._dbname,int(self._uid),self._pwd,'email.message','search',[('message_id','=',message_id),('model','=',model),('res_id','=',res_id)])
             if ext_ids:
             	name = execute(conn,'execute',self._dbname,int(self._uid),self._pwd,model,'read',res_id,['name'])['name']
             	ext_msg += """This mail is already archived to {0} '{1}'.\n""".format(object_name,name)
@@ -208,7 +208,7 @@
             if attachments:
             	result = self.MakeAttachment([rec], mail)
             attachment_ids = result.get(model, {}).get(res_id, [])
-            execute(conn,'execute',self._dbname,int(self._uid),self._pwd,'email.server.tools','history',model, res_id, msg, attachment_ids)
+            execute(conn,'execute',self._dbname,int(self._uid),self._pwd,'email.thread','history',model, res_id, msg, attachment_ids)
             new_msg += """- {0} : {1}\n""".format(object_name,str(rec[2]))
             flag = True
 
@@ -298,7 +298,7 @@
             endCut = message_id.find(">")
             message_id = message_id[startCut:endCut+1]
             email.replace_header('Message-Id',message_id)
-            id = execute(conn,'execute',self._dbname,int(self._uid),self._pwd,'email.server.tools','process_email',section, str(email))
+            id = execute(conn,'execute',self._dbname,int(self._uid),self._pwd,'email.thread','process_email',section, str(email))
             if id > 0:
             	flag = True
             	return flag
@@ -456,17 +456,17 @@
         import win32ui
     	conn = xmlrpclib.ServerProxy(self._uri+ '/xmlrpc/object')
     	res_vals = []
-    	mail_id = execute( conn, 'execute', self._dbname, int(self._uid), self._pwd, 'mailgate.message', 'search', [('message_id','=',message_id)])
+    	mail_id = execute( conn, 'execute', self._dbname, int(self._uid), self._pwd, 'email.message', 'search', [('message_id','=',message_id)])
         ref_mail_id = None
     	if not mail_id:
-            ref_mail_id = execute( conn, 'execute', self._dbname, int(self._uid), self._pwd, 'mailgate.message', 'search', [('references','=',message_id)])
+            ref_mail_id = execute( conn, 'execute', self._dbname, int(self._uid), self._pwd, 'email.message', 'search', [('references','=',message_id)])
             if ref_mail_id:
-                address = execute( conn, 'execute', self._dbname, int(self._uid), self._pwd, 'mailgate.message','read',ref_mail_id[0],['model','res_id'])
+                address = execute( conn, 'execute', self._dbname, int(self._uid), self._pwd, 'email.message','read',ref_mail_id[0],['model','res_id'])
                 for key, vals in address.items():
                     res_vals.append([key,vals])
                 return res_vals
             return None
-    	address = execute( conn, 'execute', self._dbname, int(self._uid), self._pwd, 'mailgate.message','read',mail_id[0],['model','res_id'])
+    	address = execute( conn, 'execute', self._dbname, int(self._uid), self._pwd, 'email.message','read',mail_id[0],['model','res_id'])
     	for key, vals in address.items():
     		res_vals.append([key,vals])
     	return res_vals

=== modified file 'project/__openerp__.py'
--- project/__openerp__.py	2011-03-22 13:12:14 +0000
+++ project/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -27,7 +27,7 @@
     "website": "http://www.openerp.com";,
     "category": "Project Management",
     "images": ["images/gantt.png", "images/project_dashboard.jpeg","images/project_task_tree.jpeg","images/project_task.jpeg","images/project.jpeg","images/task_analysis.jpeg"],
-    "depends": ["product", "analytic", "board"],
+    "depends": ["product", "analytic", "board", "mail"],
     "description": """
 Project management module tracks multi-level projects, tasks, work done on tasks, eso.
 ======================================================================================
@@ -45,7 +45,7 @@
     "update_xml": [
         "security/project_security.xml",
         "wizard/project_task_delegate_view.xml",
-        "wizard/project_task_close_view.xml",
+#        "wizard/project_task_close_view.xml",
         "wizard/project_task_reevaluate_view.xml",
         "security/ir.model.access.csv",
         "project_data.xml",

=== modified file 'project/wizard/__init__.py'
--- project/wizard/__init__.py	2011-01-14 00:11:01 +0000
+++ project/wizard/__init__.py	2011-04-11 12:37:32 +0000
@@ -19,7 +19,8 @@
 #
 ##############################################################################
 
-import project_task_close
+#import project_task_close
+import email_compose_message
 import project_task_delegate
 import project_task_reevaluate
 

=== added file 'project/wizard/email_compose_message.py'
--- project/wizard/email_compose_message.py	1970-01-01 00:00:00 +0000
+++ project/wizard/email_compose_message.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,68 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+from osv import osv
+from osv import fields
+from tools.translate import _
+
+class email_compose_message(osv.osv_memory):
+    _inherit = 'email.compose.message'
+
+    def get_value(self, cr, uid, model, resource_id, context=None):
+        if context is None:
+            context = {}
+        result = super(email_compose_message, self).get_value(cr, uid,  model, resource_id, context=context)
+        if model == 'project.task' and resource_id:
+            task_pool = self.pool.get('project.task')
+            task_data = task_pool.browse(cr, uid, resource_id, context=context)
+            partner = task_data.partner_id or task_data.project_id.partner_id
+            if task_data.project_id.warn_manager and (not task_data.project_id.user_id or task_data.project_id.user_id and not task_data.project_id.user_id.user_email) :
+                raise osv.except_osv(_('Error'), _("Please specify the Project Manager or email address of Project Manager."))
+            elif task_data.project_id.warn_customer and (not partner or not len(partner.address) or (partner and len(partner.address) and not partner.address[0].email)):
+                raise osv.except_osv(_('Error'), _("Please specify the Customer or email address of Customer."))
+
+            result.update({'email_from': task_data.user_id and task_data.user_id.user_email or False})
+            val = {
+                    'name': task_data.name,
+                    'user_id': task_data.user_id.name,
+                    'task_id': "%d/%d" % (task_data.project_id.id, task_data.id),
+                    'date_start': task_data.date_start,
+                    'date': task_data.date_end,
+                    'state': task_data.state
+            }
+            header = (task_data.project_id.warn_header or '') % val
+            footer = (task_data.project_id.warn_footer or '') % val
+            description = u'%s\n %s\n %s\n\n \n%s' % (header, task_data.description or '', footer, task_data.user_id and task_data.user_id.signature)
+            if partner and len(partner.address):
+                result.update({'email_to': result.get('email_to',False) and result.get('email_to') + ',' + partner.address[0].email})
+            result.update({
+                       'body': description or False,
+                       'email_to':   task_data.project_id.user_id and task_data.project_id.user_id.user_email or False,
+                       'subject':  _("Task '%s' Closed") % task_data.name,
+                       'model': model,
+                       'res_id': resource_id,
+                    })
+
+        return result
+
+email_compose_message()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'project/wizard/project_task_close.py'
--- project/wizard/project_task_close.py	2011-02-22 12:58:05 +0000
+++ project/wizard/project_task_close.py	2011-04-11 12:37:32 +0000
@@ -45,13 +45,13 @@
             context = {}
         record_id = context and context.get('active_id', False) or False
         task_pool = self.pool.get('project.task')
-                
+
         res = super(project_task_close, self).default_get(cr, uid, fields, context=context)
         task = task_pool.browse(cr, uid, record_id, context=context)
         project = task.project_id
         manager = project.user_id or False
         partner = task.partner_id or task.project_id.partner_id
-        
+
         if 'description' in fields:
             res.update({'description': task.description or False})
         if 'manager_warn' in fields:
@@ -67,7 +67,8 @@
     def send(self, cr, uid, ids, context=None):
         if context is None:
             context = {}
-        
+
+        email_message_obj = self.pool.get('email.message')
         task_pool = self.pool.get('project.task')
         task_id = context.get('active_id', False)
         if not task_id:
@@ -112,7 +113,7 @@
                     to_adr.append(data.manager_email)
                 if data.partner_warn and data.partner_email:
                     to_adr.append(data.partner_email)
-                mail_id = tools.email_send(from_adr, to_adr, subject, tools.ustr(body), email_bcc=[from_adr])
+                mail_id = email_message_obj.schedule_with_attach(cr, uid, from_adr, to_adr, subject, tools.ustr(body), model='project.task.close', email_bcc=[from_adr])
                 if not mail_id:
                     raise osv.except_osv(_('Error'), _("Couldn't send mail! Check the email ids and smtp configuration settings"))
         return {}

=== modified file 'project_issue/project_issue.py'
--- project_issue/project_issue.py	2011-03-09 04:36:22 +0000
+++ project_issue/project_issue.py	2011-04-11 12:37:32 +0000
@@ -26,7 +26,7 @@
 import binascii
 import time
 import tools
-
+from crm import wizard
 
 class project_issue_version(osv.osv):
     _name = "project.issue.version"
@@ -44,7 +44,7 @@
     _name = "project.issue"
     _description = "Project Issue"
     _order = "priority, id desc"
-    _inherit = ['mailgate.thread']
+    _inherit = ['email.thread']
 
     def case_open(self, cr, uid, ids, *args):
         """
@@ -144,7 +144,7 @@
         issues = []
         issue_pool = self.pool.get('project.issue')
         for task in self.pool.get('project.task').browse(cr, uid, ids, context=context):
-            issues += issue_pool.search(cr, uid, [('task_id','=',task.id)])            
+            issues += issue_pool.search(cr, uid, [('task_id','=',task.id)])
         return issues
 
     def _get_issue_work(self, cr, uid, ids, context=None):
@@ -162,8 +162,8 @@
             progress = 0.0
             if issue.task_id:
                 progress = task_pool._hours_get(cr, uid, [issue.task_id.id], field_names, args, context=context)[issue.task_id.id]['progress']
-            res[issue.id] = {'progress' : progress}     
-        return res        
+            res[issue.id] = {'progress' : progress}
+        return res
 
     _columns = {
         'id': fields.integer('ID'),
@@ -213,7 +213,7 @@
                                 method=True, multi='working_days_open', type="float", store=True),
         'working_hours_close': fields.function(_compute_day, string='Working Hours to Close the Issue', \
                                 method=True, multi='working_days_close', type="float", store=True),
-        'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
+        'message_ids': fields.one2many('email.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
         'date_action_last': fields.datetime('Last Action', readonly=1),
         'date_action_next': fields.datetime('Next Action', readonly=1),
         'progress': fields.function(_hours_get, method=True, string='Progress (%)', multi='hours', group_operator="avg", help="Computed as: Time Spent / Total Time.",
@@ -370,9 +370,9 @@
         @param cr: the current row, from the database cursor,
         @param uid: the current user’s ID for security checks
         """
-        if context is None: 
+        if context is None:
             context = {}
-        mailgate_pool = self.pool.get('email.server.tools')
+        thread_pool = self.pool.get('email.thread')
 
         subject = msg.get('subject') or _('No Title')
         body = msg.get('body')
@@ -389,28 +389,15 @@
         if msg.get('priority', False):
             vals['priority'] = priority
 
-        res = mailgate_pool.get_partner(cr, uid, msg.get('from'))
+        res = thread_pool.get_partner(cr, uid, msg.get('from'))
         if res:
             vals.update(res)
         context.update({'state_to' : 'draft'})
         res = self.create(cr, uid, vals, context=context)
         self.convert_to_bug(cr, uid, [res], context=context)
-
-        attachents = msg.get('attachments', [])
-        for attactment in attachents or []:
-            data_attach = {
-                'name': attactment,
-                'datas': binascii.b2a_base64(str(attachents.get(attactment))),
-                'datas_fname': attactment,
-                'description': 'Mail attachment',
-                'res_model': self._name,
-                'res_id': res,
-            }
-            self.pool.get('ir.attachment').create(cr, uid, data_attach)
-
         return res
 
-    def message_update(self, cr, uid, ids, vals=None, msg="", default_act='pending', context=None):
+    def message_update(self, cr, uid, ids, msg, vals=None, default_act='pending', context=None):
         """
         @param self: The object pointer
         @param cr: the current row, from the database cursor,
@@ -453,18 +440,6 @@
         res = self.write(cr, uid, ids, vals)
         return res
 
-    def msg_send(self, cr, uid, id, *args, **argv):
-
-        """ Send The Message
-            @param self: The object pointer
-            @param cr: the current row, from the database cursor,
-            @param uid: the current user’s ID for security checks,
-            @param ids: List of email’s IDs
-            @param *args: Return Tuple Value
-            @param **args: Return Dictionary of Keyword Value
-        """
-        return True
-
     def copy(self, cr, uid, id, default=None, context=None):
         issue = self.read(cr, uid, id, ['name'], context=context)
         if not default:

=== modified file 'project_issue/project_issue_view.xml'
--- project_issue/project_issue_view.xml	2011-03-09 04:36:22 +0000
+++ project_issue/project_issue_view.xml	2011-04-11 12:37:32 +0000
@@ -101,11 +101,11 @@
                                 <tree string="History">
                                     <field name="display_text" string="History Information"/>
                                     <field name="history" invisible="1"/>
-                                    <button
-                                        string="Reply" attrs="{'invisible': [('history', '!=', True)]}"
-                                        name="%(crm.action_crm_send_mail)d"
-                                        context="{'mail':'reply', 'model': 'project.issue', 'include_original' : True}"
-                                        icon="terp-mail-replied" type="action" />
+		                            <button
+		                                string="Reply" attrs="{'invisible': [('history', '!=', True)]}"
+		                                name="%(mail.action_email_compose_message_wizard)d"
+		                                context="{'mail':'reply', 'message_id':active_id}"
+		                                icon="terp-mail-replied" type="action" />
                                 </tree>
                                 <form string="History">
                                     <group col="4" colspan="4">
@@ -113,18 +113,17 @@
                                         <field name="date"/>
                                         <field name="email_to" widget="char" size="512"/>
                                         <field name="email_cc" widget="char" size="512"/>
-                                        <field name="name" colspan="4" widget="char" size="512"/>
+                                        <field name="subject" colspan="4" widget="char" size="512"/>
                                         <field name="history" invisible="1"/>
                                     </group>
                                     <notebook colspan="4">
                                         <page string="Details">
                                             <group attrs="{'invisible': [('history', '!=', True)]}">
-                                                <field name="description" colspan="4" nolabel="1" height="250"/>
-                                                <button colspan="4"
-                                                    string="Reply"
-                                                    name="%(crm.action_crm_send_mail)d"
-                                                    context="{'mail':'reply', 'model': 'project.issue', 'include_original' : True}"
-                                                    icon="terp-mail-replied" type="action" />
+                                                <field name="body" colspan="4" nolabel="1" height="250"/>
+		                                        <button colspan="4" string="Reply"
+		                                            name="%(mail.action_email_compose_message_wizard)d"
+		                                            context="{'mail':'reply', 'message_id':active_id}"
+		                                            icon="terp-mail-replied" type="action"/>
                                             </group>
                                             <group attrs="{'invisible': [('history', '=', True)]}">
                                                 <field name="display_text" colspan="4" nolabel="1"  height="250"/>
@@ -141,10 +140,10 @@
                                 name="%(crm.action_crm_add_note)d"
                                 context="{'model': 'crm.lead' }"
                                 icon="terp-document-new" type="action" />
-                            <button string="Send New Email"
-                               name="%(crm.action_crm_send_mail)d"
-                               context="{'mail':'new', 'model': 'project.issue'}"
-                               icon="terp-mail-message-new" type="action" />
+		                    <button string="Send New Email"
+		                        name="%(mail.action_email_compose_message_wizard)d"
+		                        context="{'email_model':'project.issue', 'email_res_id': active_id}"
+		                        icon="terp-mail-message-new" type="action"/>
                         </page>
                            <page string="Extra Info" groups="base.group_extended">
                             <group col="2" colspan="2">

=== modified file 'project_issue/report/project_issue_report.py'
--- project_issue/report/project_issue_report.py	2011-01-25 01:43:30 +0000
+++ project_issue/report/project_issue_report.py	2011-04-11 12:37:32 +0000
@@ -1,7 +1,7 @@
 
 # -*- coding: utf-8 -*-
 ##############################################################################
-#    
+#
 #    OpenERP, Open Source Management Solution
 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 #
@@ -16,7 +16,7 @@
 #    GNU Affero General Public License for more details.
 #
 #    You should have received a copy of the GNU Affero General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
 
@@ -102,7 +102,7 @@
                     date_trunc('day',c.create_date) as create_date,
                     extract('epoch' from (c.date_open-c.create_date))/(3600*24) as  delay_open,
                     extract('epoch' from (c.date_closed-c.create_date))/(3600*24) as  delay_close,
-                    (SELECT count(id) FROM mailgate_message WHERE model='project.issue' AND res_id=c.id) AS email
+                    (SELECT count(id) FROM email_message WHERE model='project.issue' AND res_id=c.id) AS email
                 FROM
                     project_issue c
             )""")

=== modified file 'project_issue/security/ir.model.access.csv'
--- project_issue/security/ir.model.access.csv	2011-01-14 00:11:01 +0000
+++ project_issue/security/ir.model.access.csv	2011-04-11 12:37:32 +0000
@@ -5,7 +5,7 @@
 "access_crm_case_categ_id","crm.case.categ","crm.model_crm_case_categ","project.group_project_manager",1,1,1,1
 "access_project_issue_version_project","project_issue_version manager","model_project_issue_version","project.group_project_manager",1,1,1,1
 "access_project_issue_version_project_user","project_issue_version user","model_project_issue_version","project.group_project_user",1,0,0,0
-"access_mailgate_message_project_manager","mailgate.message.manager","mail_gateway.model_mailgate_message","project.group_project_manager",1,1,1,1
+"access_email_message_project_manager","email.message.manager","mail.model_email_message","project.group_project_manager",1,1,1,1
 "access_resource_calendar_project_manager","resource.calendar.project.manager","resource.model_resource_calendar","project.group_project_manager",1,1,1,1
 "access_project_issue_report_user","project.issue.report user","model_project_issue_report","project.group_project_user",1,0,0,0
-"access_mailgate_message_issue_project_user","project.mailgate.message.issue.user","mail_gateway.model_mailgate_message","project.group_project_user",1,1,1,0
+"access_email_message_issue_project_user","email.message.user","mail.model_email_message","project.group_project_user",1,1,1,0

=== added directory 'project_issue/wizard'
=== added file 'project_issue/wizard/__init__.py'
--- project_issue/wizard/__init__.py	1970-01-01 00:00:00 +0000
+++ project_issue/wizard/__init__.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,25 @@
+# -*- encoding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
+#    $Id$
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details.
+#
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+import email_compose_message
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'project_issue/wizard/email_compose_message.py'
--- project_issue/wizard/email_compose_message.py	1970-01-01 00:00:00 +0000
+++ project_issue/wizard/email_compose_message.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,49 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+from osv import osv
+from osv import fields
+import tools
+
+class email_compose_message(osv.osv_memory):
+    _inherit = 'email.compose.message'
+
+    def get_value(self, cr, uid, model, resource_id, context=None):
+        if context is None:
+            context = {}
+        result = super(email_compose_message, self).get_value(cr, uid,  model, resource_id, context=context)
+        if model == 'project.issue' and resource_id:
+            model_obj = self.pool.get(model)
+            data = model_obj.browse(cr, uid , resource_id, context)
+            result.update({
+                    'subject' : data.name or False,
+                    'email_to' : data.email_from or False,
+                    'email_from' : data.user_id and data.user_id.address_id and data.user_id.address_id.email or False,
+                    'body' : '\n' + (tools.ustr(data.user_id.signature or '')),
+                    'email_cc' : tools.ustr(data.email_cc or ''),
+                    'model': model  or False,
+                    'res_id': resource_id  or False,
+                })
+        return result
+
+email_compose_message()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'project_mailgate/__openerp__.py'
--- project_mailgate/__openerp__.py	2011-03-18 11:08:16 +0000
+++ project_mailgate/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -27,7 +27,7 @@
     "website": "http://www.openerp.com";,
     "category": "Project Management",
     "images": ["images/project_mailgate_task.jpeg"],
-    "depends": ["project", "mail_gateway"],
+    "depends": ["project", "mail"],
     "description": """
 This module is an interface that synchronises mails with OpenERP Project Task.
 ==============================================================================

=== modified file 'project_mailgate/project_mailgate.py'
--- project_mailgate/project_mailgate.py	2011-01-14 00:11:01 +0000
+++ project_mailgate/project_mailgate.py	2011-04-11 12:37:32 +0000
@@ -25,10 +25,10 @@
 
 class project_tasks(osv.osv):
     _name = "project.task"
-    _inherit = ['mailgate.thread','project.task']
-    
+    _inherit = ['email.thread','project.task']
+
     _columns={
-                'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)], readonly=True),
+                'message_ids': fields.one2many('email.message', 'res_id', 'Messages', domain=[('model','=',_name)], readonly=True),
               }
     def message_new(self, cr, uid, msg, context=None):
 #        """
@@ -38,47 +38,33 @@
 #        @param cr: the current row, from the database cursor,
 #        @param uid: the current user’s ID for security checks
 #        """
-        mailgate_obj = self.pool.get('email.server.tools')
+        thread_obj = self.pool.get('email.thread')
         subject = msg.get('subject')
         body = msg.get('body')
         msg_from = msg.get('from')
         priority = msg.get('priority')
 
-        data = {      
+        data = {
             'name': subject,
             'description': body,
             'planned_hours' : 0.0,
         }
-        res = mailgate_obj.get_partner(cr, uid, msg_from)
+        res = thread_obj.get_partner(cr, uid, msg_from)
         if res:
             data.update(res)
-        res = self.create(cr, uid, data)    
-        
-        attachments = msg.get('attachments', [])
-        for attachment in attachments or []:
-            data_attach = {
-                'name': attachment,
-                'datas':binascii.b2a_base64(str(attachments.get(attachment))),
-                'datas_fname': attachment,
-                'description': 'Mail attachment',
-                'res_model': self._name,
-                'res_id': res,
-            }
-            self.pool.get('ir.attachment').create(cr, uid, data_attach)
+        return self.create(cr, uid, data)
 
-        return res           
-    
-    def message_update(self, cr, uid, id, msg, data={}, default_act='pending'): 
-        mailgate_obj = self.pool.get('email.server.tools')
-        msg_actions, body_data = mailgate_obj.msg_act_get(msg)           
+    def message_update(self, cr, uid, id, msg, data={}, default_act='pending'):
+        thread_obj = self.pool.get('email.thread')
+        msg_actions, body_data = thread_obj.msg_act_get(msg)
         data.update({
-            'description': body_data,            
+            'description': body_data,
         })
         act = 'do_'+default_act
         if 'state' in msg_actions:
             if msg_actions['state'] in ['draft','close','cancel','open','pending']:
                 act = 'do_' + msg_actions['state']
-        
+
         for k1,k2 in [('cost','planned_hours')]:
             try:
                 data[k2] = float(msg_actions[k1])
@@ -88,12 +74,12 @@
         if 'priority' in msg_actions:
             if msg_actions['priority'] in ('1','2','3','4','5'):
                 data['priority'] = msg_actions['priority']
-        
+
         self.write(cr, uid, [id], data)
         getattr(self,act)(cr, uid, [id])
         return True
 
-    def message_followers(self, cr, uid, ids, context=None):
+    def thread_followers(self, cr, uid, ids, context=None):
         res = []
         if isinstance(ids, (str, int, long)):
             select = [ids]
@@ -106,35 +92,32 @@
             return len(res) and res[0] or False
         return res
 
-    def msg_send(self, cr, uid, id, *args, **argv):
-        return True
-    
     def _history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, email_from=False, message_id=False, attach=[], context=None):
-        mailgate_pool = self.pool.get('mailgate.thread')
-        return mailgate_pool.history(cr, uid, cases, keyword, history=history,\
+        thread_pool = self.pool.get('email.thread')
+        return thread_pool.history(cr, uid, cases, keyword, history=history,\
                                        subject=subject, email=email, \
                                        details=details, email_from=email_from,\
                                        message_id=message_id, attach=attach, \
                                        context=context)
-        
+
     def do_draft(self, cr, uid, ids, *args, **kwargs):
         res = super(project_tasks, self).do_draft(cr, uid, ids, *args, **kwargs)
         tasks = self.browse(cr, uid, ids)
         self._history(cr, uid, tasks, _('Draft'))
         return res
-    
+
     def do_open(self, cr, uid, ids, *args, **kwargs):
         res = super(project_tasks, self).do_open(cr, uid, ids, *args, **kwargs)
         tasks = self.browse(cr, uid, ids)
         self._history(cr, uid, tasks, _('Open'))
         return res
-    
+
     def do_pending(self, cr, uid, ids, *args, **kwargs):
         res = super(project_tasks, self).do_pending(cr, uid, ids, *args, **kwargs)
         tasks = self.browse(cr, uid, ids)
         self._history(cr, uid, tasks, _('Pending'))
         return res
-    
+
     def do_close(self, cr, uid, ids, *args, **kwargs):
         res = super(project_tasks, self).do_close(cr, uid, ids, *args, **kwargs)
         tasks = self.browse(cr, uid, ids)
@@ -142,7 +125,7 @@
             if task.state == 'done':
                 self._history(cr, uid, tasks, _('Done'))
         return res
-    
+
     def do_cancel(self, cr, uid, ids, *args, **kwargs):
         res = super(project_tasks, self).do_cancel(cr, uid, ids, *args, **kwargs)
         tasks = self.browse(cr, uid, ids)

=== modified file 'project_mailgate/security/ir.model.access.csv'
--- project_mailgate/security/ir.model.access.csv	2011-01-14 00:11:01 +0000
+++ project_mailgate/security/ir.model.access.csv	2011-04-11 12:37:32 +0000
@@ -1,3 +1,3 @@
 "id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
-"access_mailgate_message_project_manager","project.mailgate.message.manager","mail_gateway.model_mailgate_message","project.group_project_manager",1,1,1,0
-"access_mailgate_message_project_user","project.mailgate.message.user","mail_gateway.model_mailgate_message","project.group_project_user",1,1,1,0
+"access_email_message_project_manager","project.email.message.manager","mail.model_email_message","project.group_project_manager",1,1,1,0
+"access_email_message_project_user","project.email.message.user","mail.model_email_message","project.group_project_user",1,1,1,0

=== modified file 'project_planning/__init__.py'
--- project_planning/__init__.py	2011-01-14 00:11:01 +0000
+++ project_planning/__init__.py	2011-04-11 12:37:32 +0000
@@ -21,5 +21,6 @@
 
 import project_planning
 import report
+import wizard
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'project_planning/project_planning_view.xml'
--- project_planning/project_planning_view.xml	2011-01-14 00:11:01 +0000
+++ project_planning/project_planning_view.xml	2011-04-11 12:37:32 +0000
@@ -109,7 +109,7 @@
                                                         <button name="do_reopen" states="done,cancelled" string="Reactivate" type="object" icon="gtk-convert"/>
                                                         <button name="do_pending" states="open" string="Pending" type="object" icon="gtk-media-pause"/>
                                                         <button groups="base.group_extended" name="%(project.action_project_task_delegate)d" states="pending,open" string="Delegate" type="action" icon="gtk-sort-descending"/>
-                                                        <button name="%(project.action_project_task_close)d" states="pending,open" string="Done" type="action" icon="gtk-jump-to"/>
+                                                        <button name="%(mail.action_email_compose_message_wizard)d" context="{'email_model':'project.task', 'email_res_id': active_id}" states="pending,open" string="Done" type="action" icon="gtk-jump-to"/>
                                                     </group>
                                                  </page>
                                                 <page groups="base.group_extended" string="Extra Info" attrs="{'readonly':[('state','=','done')]}">

=== added directory 'project_planning/wizard'
=== added file 'project_planning/wizard/__init__.py'
--- project_planning/wizard/__init__.py	1970-01-01 00:00:00 +0000
+++ project_planning/wizard/__init__.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#    
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details.
+#
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
+#
+##############################################################################
+
+import email_compose_message
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== added file 'project_planning/wizard/email_compose_message.py'
--- project_planning/wizard/email_compose_message.py	1970-01-01 00:00:00 +0000
+++ project_planning/wizard/email_compose_message.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+from osv import osv
+from osv import fields
+import tools
+
+class email_compose_message(osv.osv_memory):
+    _inherit = 'email.compose.message'
+
+    def get_value(self, cr, uid, model, resource_id, context=None):
+        if context is None:
+            context = {}
+        result = super(email_compose_message, self).get_value(cr, uid,  model, resource_id, context=context)
+        if model == 'project.task' and resource_id:
+            model_obj = self.pool.get(model)
+            data = model_obj.browse(cr, uid , resource_id, context)
+            result.update({
+                    'subject' : data.name or False,
+                    'email_to' : data.user_id.user_email or False,
+                    'email_from' : data.user_id and data.user_id.address_id and data.user_id.address_id.email or False,
+                    'body' : '\n' + (tools.ustr(data.user_id.signature or '')),
+                    'model': model  or False,
+                    'res_id': resource_id  or False,
+                })
+        return result
+
+email_compose_message()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'project_scrum/__openerp__.py'
--- project_scrum/__openerp__.py	2011-03-18 14:06:18 +0000
+++ project_scrum/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -49,7 +49,7 @@
     """,
     'author': 'OpenERP SA',
     'images': ['images/product_backlogs.jpeg', 'images/project_sprints.jpeg', 'images/scrum_dashboard.jpeg', 'images/scrum_meetings.jpeg'],
-    'depends': ['project', 'process'],
+    'depends': ['project', 'process', 'mail'],
     'init_xml': [],
     'update_xml': [
         'security/ir.model.access.csv',
@@ -57,7 +57,7 @@
         'wizard/project_scrum_backlog_create_task_view.xml',
         'wizard/project_scrum_backlog_merger_view.xml',
         'wizard/project_scrum_postpone_view.xml',
-        "wizard/project_scrum_email_view.xml",
+#        "wizard/project_scrum_email_view.xml",
         'project_scrum_view.xml',
         'wizard/project_scrum_backlog_sprint_view.xml',
         'process/project_scrum_process.xml',

=== modified file 'project_scrum/project_scrum.py'
--- project_scrum/project_scrum.py	2011-03-09 06:21:48 +0000
+++ project_scrum/project_scrum.py	2011-04-11 12:37:32 +0000
@@ -317,6 +317,7 @@
         return True
 
     def email_send(self, cr, uid, ids, email, context=None):
+        email_message_obj = self.pool.get('email.message')
         email_from = tools.config.get('email_from', False)
         meeting_id = self.browse(cr, uid, ids, context=context)[0]
         user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
@@ -325,7 +326,7 @@
         body += _('\n*Tasks since yesterday:\n_______________________%s\n*Task for Today:\n_______________________ %s\n\n*Blocks encountered:\n_______________________ %s') %(meeting_id.question_yesterday,meeting_id.question_today, meeting_id.question_blocks or _('No Blocks'))
         body += _("\n\nThank you,\n%s") % user.name
         sub_name = meeting_id.name or _('Scrum Meeting of %s') % meeting_id.date
-        flag = tools.email_send(user_email , [email], sub_name, body, reply_to=None, openobject_id=str(meeting_id.id))
+        flag = email_message_obj.schedule_with_attach(cr, uid, user_email , [email], sub_name, body, model='project.scrum.meeting', reply_to=None, openobject_id=str(meeting_id.id))
         if not flag:
             return False
         return True

=== modified file 'project_scrum/project_scrum_view.xml'
--- project_scrum/project_scrum_view.xml	2011-03-14 06:15:04 +0000
+++ project_scrum/project_scrum_view.xml	2011-04-11 12:37:32 +0000
@@ -302,7 +302,7 @@
                     </notebook>
                     <group col="8" colspan="4">
                         <field name="state" readonly="1"/>
-                        <button name="%(project_scrum.report_scrum_sprint_burndown_chart)d" 
+                        <button name="%(project_scrum.report_scrum_sprint_burndown_chart)d"
                         			string="Burndown Chart" type="action" icon="gtk-print"/>
                         <button type="object" string="Open" name="button_open" states="draft,pending" icon="terp-camera_test"/>
                         <button type="object" string="Pending" name="button_pending" states="open" icon="gtk-media-pause"/>
@@ -396,7 +396,9 @@
                         <field name="date"/>
                         <field name="sprint_id" domain="[('state', '=', 'open')]"/>
                         <field name="user_id"/>
-                        <button name="%(action_project_scrum_email)d"  string="Send Email" type="action" icon="terp-mail-message-new" />
+                        <button name="%(mail.action_email_compose_message_wizard)d"
+                                string="Send Email" type="action" icon="terp-mail-message-new"
+                                context="{'email_model': 'project.scrum.meeting', 'email_res_id': active_id}"/>
                     </group>
                     <notebook colspan="4">
                         <page string="Scrum Meeting">

=== modified file 'project_scrum/wizard/__init__.py'
--- project_scrum/wizard/__init__.py	2011-01-14 00:11:01 +0000
+++ project_scrum/wizard/__init__.py	2011-04-11 12:37:32 +0000
@@ -23,7 +23,8 @@
 import project_scrum_backlog_sprint
 import project_scrum_backlog_merger
 import project_scrum_postpone
-import project_scrum_email
+#import project_scrum_email
+import email_compose_message
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 

=== added file 'project_scrum/wizard/email_compose_message.py'
--- project_scrum/wizard/email_compose_message.py	1970-01-01 00:00:00 +0000
+++ project_scrum/wizard/email_compose_message.py	2011-04-11 12:37:32 +0000
@@ -0,0 +1,59 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>
+#
+##############################################################################
+
+from osv import osv
+from osv import fields
+from tools.translate import _
+
+class email_compose_message(osv.osv_memory):
+    _inherit = 'email.compose.message'
+
+    def get_value(self, cr, uid, model, resource_id, context=None):
+        if context is None:
+            context = {}
+        result = super(email_compose_message, self).get_value(cr, uid,  model, resource_id, context=context)
+        if model == 'project.scrum.meeting' and resource_id:
+            meeting_pool = self.pool.get('project.scrum.meeting')
+            user_pool = self.pool.get('res.users')
+            meeting = meeting_pool.browse(cr, uid, resource_id, context=context)
+
+            sprint = meeting.sprint_id
+            user_data = user_pool.browse(cr, uid, uid, context=context)
+            result.update({'email_from': user_data.address_id and user_data.address_id.email or False})
+
+            if sprint.scrum_master_id and sprint.scrum_master_id.user_email:
+                result.update({'email_to': sprint.scrum_master_id.user_email})
+            if sprint.product_owner_id and sprint.product_owner_id.user_email:
+                result.update({'email_to': result.get('email_to',False) and result.get('email_to') + ',' +  sprint.product_owner_id.user_email or sprint.product_owner_id.user_email})
+
+            subject = _("Scrum Meeting : %s") %(meeting.date)
+            message = _("Hello  , \nI am sending you Scrum Meeting : %s for the Sprint  '%s' of Project '%s'") %(meeting.date, sprint.name, sprint.project_id.name)
+            result.update({
+                       'subject': subject,
+                       'body': message,
+                       'model': model,
+                       'res_id': resource_id
+                    })
+        return result
+
+email_compose_message()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

=== modified file 'project_scrum/wizard/project_scrum_email.py'
--- project_scrum/wizard/project_scrum_email.py	2011-01-14 00:11:01 +0000
+++ project_scrum/wizard/project_scrum_email.py	2011-04-11 12:37:32 +0000
@@ -69,6 +69,7 @@
         if context is None:
             context = {}
 
+        email_message_obj = self.pool.get('email.message')
         active_id = context.get('active_id', False)
         scrum_meeting_pool = self.pool.get('project.scrum.meeting')
         user_pool = self.pool.get('res.users')
@@ -91,7 +92,7 @@
         body += "\n%s\n" %_("Task for Today")
         body += "_______________________ \n"
         body += "\n%s\n" %(meeting.question_today or _('None'))
-        body += "\n%s\n" % _('Blocking points encountered:') 
+        body += "\n%s\n" % _('Blocking points encountered:')
         body += "_______________________ \n"
         body += "\n%s\n" %(meeting.question_blocks or _('None'))
         body += "\n%s\n%s" %(_('Thank you,'), user.name)
@@ -100,8 +101,8 @@
         if data.scrum_master_email == data.product_owner_email:
             data.product_owner_email = False
         if data.scrum_master_email:
-            tools.email_send(user_email, [data.scrum_master_email], data.subject, body, reply_to=user_email)
+            email_message_obj.schedule_with_attach(cr, uid, user_email, [data.scrum_master_email], data.subject, body, model='project.scrum.email', reply_to=user_email)
         if data.product_owner_email:
-            tools.email_send(user_email, [data.product_owner_email], data.subject, body, reply_to=user_email)
+            email_message_obj.schedule_with_attach(cr, uid, user_email, [data.product_owner_email], data.subject, body, model='project.scrum.email', reply_to=user_email)
         return {'type': 'ir.actions.act_window_close'}
 project_scrum_email()

=== modified file 'share/__openerp__.py'
--- share/__openerp__.py	2011-03-18 14:06:18 +0000
+++ share/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -23,7 +23,7 @@
 {
     "name" : "Sharing Tools",
     "version" : "1.3",
-    "depends" : ["base"],
+    "depends" : ["base", "mail"],
     "author" : "OpenERP SA",
     "category": 'Tools',
     "description": """

=== modified file 'share/wizard/share_wizard.py'
--- share/wizard/share_wizard.py	2011-03-08 11:22:08 +0000
+++ share/wizard/share_wizard.py	2011-04-11 12:37:32 +0000
@@ -22,7 +22,6 @@
 import random
 import time
 
-import tools
 from osv import osv, fields
 from osv.expression import expression
 from tools.translate import _
@@ -171,7 +170,7 @@
                                    during recursion
            @param suffix: optional suffix to append to the field path to reach the main object
         """
-        
+
         if relation_fields is None:
             relation_fields = []
         local_rel_fields = []
@@ -222,7 +221,7 @@
         """
         model_access_obj = self.pool.get('ir.model.access')
         user_obj = self.pool.get('res.users')
-        target_model_ids = [x[1].id for x in fields_relations] 
+        target_model_ids = [x[1].id for x in fields_relations]
         perms_to_add = (mode == 'readonly') and READ_ONLY_ACCESS or FULL_ACCESS
         current_user = user_obj.browse(cr, uid, uid, context=context)
 
@@ -296,7 +295,7 @@
                     related_domain = []
                     for element in domain:
                         if domain_expr._is_leaf(element):
-                            left, operator, right = element 
+                            left, operator, right = element
                             left = '%s.%s'%(rel_field, left)
                             element = left, operator, right
                         related_domain.append(element)
@@ -368,7 +367,7 @@
             user_obj.write(cr, 1, user_ids, {
                                    'groups_id': [(4,group_id)],
                             })
-        self._setup_action_and_shortcut(cr, uid, wizard_data, user_ids, 
+        self._setup_action_and_shortcut(cr, uid, wizard_data, user_ids,
             (wizard_data.user_type == 'new'), context=context)
 
 
@@ -388,7 +387,7 @@
         obj0, obj1, obj2, obj3 = self._get_relationship_classes(cr, uid, model, context=context)
         mode = wizard_data.access_mode
 
-        # Add access to [obj0] and [obj1] according to chosen mode   
+        # Add access to [obj0] and [obj1] according to chosen mode
         self._add_access_rights_for_share_group(cr, uid, group_id, mode, obj0, context=context)
         self._add_access_rights_for_share_group(cr, uid, group_id, mode, obj1, context=context)
 
@@ -399,18 +398,18 @@
 
         # IR.RULES
         #   A. On [obj0]: 1 rule with domain of shared action
-        #   B. For each model in [obj1]: 1 rule in the form: 
+        #   B. For each model in [obj1]: 1 rule in the form:
         #           many2one_rel.domain_of_obj0
         #        where many2one_rel is the many2one used in the definition of the
         #        one2many, and domain_of_obj0 is the sharing domain
-        #        For example if [obj0] is project.project with a domain of 
+        #        For example if [obj0] is project.project with a domain of
         #                ['id', 'in', [1,2]]
-        #        then we will have project.task in [obj1] and we need to create this 
+        #        then we will have project.task in [obj1] and we need to create this
         #        ir.rule on project.task:
         #                ['project_id.id', 'in', [1,2]]
-        #   C. And on [obj0], [obj1], [obj2], [obj3]: add all rules from all groups of 
-        #     the user that is sharing 
-        #     (Warning: rules must be copied instead of linked if they contain a reference 
+        #   C. And on [obj0], [obj1], [obj2], [obj3]: add all rules from all groups of
+        #     the user that is sharing
+        #     (Warning: rules must be copied instead of linked if they contain a reference
         #     to uid, and it must be replaced correctly)
         rule_obj = self.pool.get('ir.rule')
         # A.
@@ -443,6 +442,7 @@
         }
 
     def send_emails(self, cr, uid, ids, context=None):
+        email_message_obj = self.pool.get('email.message')
         user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
         if not user.user_email:
             raise osv.except_osv(_('Email required'), _('The current user must have an email address configured in User Preferences to be able to send outgoing emails.'))
@@ -462,11 +462,12 @@
                     body += _("This additional data has been automatically added to your current access.\n")
                     body += _("You may use your existing login and password to view it. As a reminder, your login is %s.\n") % result_line.login
 
-                if not tools.email_send(
+                if not email_message_obj.schedule_with_attach(cr, uid,
                                             user.user_email,
                                             [email_to],
                                             subject,
-                                            body):
+                                            body,
+                                            model='share.wizard'):
                     self.__logger.warning('Failed to send sharing email from %s to %s', user.user_email, email_to)
         return {'type': 'ir.actions.act_window_close'}
 share_create()

=== modified file 'survey/__openerp__.py'
--- survey/__openerp__.py	2011-03-18 11:47:37 +0000
+++ survey/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -33,7 +33,7 @@
 Partners are also sent mails with user name and password for the invitation of the survey
     """,
     'author': 'OpenERP SA',
-    'depends': ['base_tools'],
+    'depends': ['base_tools', 'mail'],
     'update_xml': ['survey_report.xml',
                    'survey_data.xml',
                    'wizard/survey_selection.xml',

=== modified file 'survey/wizard/survey_answer.py'
--- survey/wizard/survey_answer.py	2011-03-10 06:02:06 +0000
+++ survey/wizard/survey_answer.py	2011-04-11 12:37:32 +0000
@@ -49,7 +49,7 @@
         @param context: A standard dictionary for contextual values
         @return : Dictionary value for created view of particular survey pages.
         """
-        
+
         result = super(survey_question_wiz, self).fields_view_get(cr, uid, view_id, \
                                         view_type, context, toolbar,submenu)
 
@@ -61,6 +61,7 @@
         sur_response_obj = self.pool.get('survey.response')
         que_col_head = self.pool.get('survey.question.column.heading')
         user_obj = self.pool.get('res.users')
+        email_message_obj = self.pool.get('email.message')
         if context is None:
             context = {}
         if view_type in ['form']:
@@ -79,7 +80,7 @@
                 wiz_id = surv_name_wiz.create(cr, uid, res_data)
                 sur_name_rec = surv_name_wiz.browse(cr, uid, wiz_id, context=context)
                 context.update({'sur_name_id' :wiz_id})
-  
+
             if context.has_key('active_id'):
                 context.pop('active_id')
 
@@ -428,7 +429,7 @@
                         if user_email and resp_email:
                             user_name = user_obj.browse(cr, uid, uid, context=context).name
                             mail = "Hello " + survey_data.responsible_id.name + ",\n\n " + str(user_name) + " Give Response Of " + survey_data.title + " Survey.\n\n Thanks,"
-                            tools.email_send(user_email, [resp_email], "Survey Answer Of " + str(user_name) , mail, attach = attachments)
+                            email_message_obj.schedule_with_attach(cr, uid, user_email, [resp_email], "Survey Answer Of " + str(user_name) , mail, model='survey.question.wiz', attach = attachments)
 
                     xml_form = etree.Element('form', {'string': _('Complete Survey Answer')})
                     etree.SubElement(xml_form, 'separator', {'string': 'Complete Survey', 'colspan': "4"})
@@ -447,7 +448,7 @@
 
         @param self: The object pointer
         @param cr: the current row, from the database cursor,
-        @param uid: the current user’s ID for security checks,        
+        @param uid: the current user’s ID for security checks,
         @param res_ids: List of survey answer IDs,
         @param report_name: name of the report,
         @param file_name: To give file name of the report,

=== modified file 'survey/wizard/survey_send_invitation.py'
--- survey/wizard/survey_send_invitation.py	2011-03-15 13:27:26 +0000
+++ survey/wizard/survey_send_invitation.py	2011-04-11 12:37:32 +0000
@@ -102,6 +102,7 @@
         partner_ids = record['partner_ids']
         user_ref= self.pool.get('res.users')
         survey_ref= self.pool.get('survey')
+        email_message_obj = self.pool.get('email.message')
 
         model_data_obj = self.pool.get('ir.model.data')
         group_id = model_data_obj._get_id(cr, uid, 'base', 'group_survey_user')
@@ -151,8 +152,8 @@
                     mail = record['mail']%{'login':addr.email, 'passwd':user.password, \
                                                 'name' : addr.name}
                     if record['send_mail_existing']:
-                        tools.email_send(record['mail_from'], [addr.email] , \
-                                         record['mail_subject_existing'] , mail)
+                        email_message_obj.schedule_with_attach(cr, uid, record['mail_from'], [addr.email] , \
+                                         record['mail_subject_existing'] , mail, model='survey.send.invitation')
                         existing+= "- %s (Login: %s,  Password: %s)\n" % (user.name, addr.email, \
                                                                           user.password)
                     continue
@@ -162,8 +163,8 @@
                         mail = record['mail']%{'login': user_email.login, \
                                                         'passwd': user_email.password, 'name': addr.name}
                         if record['send_mail_existing']:
-                            tools.email_send(record['mail_from'], [addr.email],\
-                                                  record['mail_subject_existing'], mail)
+                            email_message_obj.schedule_with_attach(cr, uid, record['mail_from'], [addr.email],\
+                                                  record['mail_subject_existing'], mail, model='survey.send.invitation')
                             res_user+= "- %s (Login: %s,  Password: %s)\n" % \
                                  (user_email.name, user_email.login, user_email.password)
                     continue
@@ -171,8 +172,8 @@
                 out+= addr.email + ',' + passwd + '\n'
                 mail= record['mail'] % {'login' : addr.email, 'passwd' : passwd, 'name' : addr.name}
                 if record['send_mail']:
-                    ans = tools.email_send(record['mail_from'], [addr.email], \
-                                           record['mail_subject'], mail,attach = attachments)
+                    ans = email_message_obj.schedule_with_attach(cr, uid, record['mail_from'], [addr.email], \
+                                           record['mail_subject'], mail, model='survey.send.invitation', attach = attachments)
 
                     if ans:
                         res_data = {'name': addr.name or 'Unknown',

=== modified file 'thunderbird/__openerp__.py'
--- thunderbird/__openerp__.py	2011-03-18 14:06:18 +0000
+++ thunderbird/__openerp__.py	2011-04-11 12:37:32 +0000
@@ -24,7 +24,7 @@
     "version" : "1.0",
     "author" : ['OpenERP SA', 'Axelor'],
     "website" : "http://www.openerp.com/";,
-    "depends" : ["base","mail_gateway"],
+    "depends" : ["base","mail"],
     "category" : "Tools",
     "description": """
 This module is required for the Thuderbird Plug-in to work properly.
@@ -35,7 +35,6 @@
 account, or any other object and attach the selected mail as a .eml file in
 the attachment of a selected record. You can create documents for CRM Lead,
 HR Applicant and Project Issue from selected mails.
-
       """,
     "init_xml" : [],
     "demo_xml" : [],

=== modified file 'thunderbird/partner/partner.py'
--- thunderbird/partner/partner.py	2011-01-14 00:11:01 +0000
+++ thunderbird/partner/partner.py	2011-04-11 12:37:32 +0000
@@ -24,12 +24,13 @@
 import email
 import tools
 import binascii
-class email_server_tools(osv.osv_memory):
-    _inherit = "email.server.tools"
+class email_thread(osv.osv):
+    _inherit = "email.thread"
     def history_message(self, cr, uid, model, res_id, message, context=None):
         #@param message: string of mail which is read from EML File
         attachment_pool = self.pool.get('ir.attachment')
-        msg = self.parse_message(message)
+        email_message_pool = self.pool.get('email.message')
+        msg = email_message_pool.parse_message(message)
         attachments = msg.get('attachments', [])
         att_ids = []
         for attachment in attachments:
@@ -43,91 +44,7 @@
             }
             att_ids.append(attachment_pool.create(cr, uid, data_attach))
         return self.history(cr, uid, model, res_id, msg, att_ids)
-
-    def parse_message(self, message):
-        #TOCHECK: put this function in mailgateway module
-        if isinstance(message, unicode):
-            message = message.encode('utf-8')
-        msg_txt = email.message_from_string(message)
-        message_id = msg_txt.get('message-id', False)
-        msg = {}
-        fields = msg_txt.keys()
-        msg['id'] = message_id
-        msg['message-id'] = message_id
-        if 'Subject' in fields:
-            msg['subject'] = self._decode_header(msg_txt.get('Subject'))
-
-        if 'Content-Type' in fields:
-            msg['content-type'] = msg_txt.get('Content-Type')
-
-        if 'From' in fields:
-            msg['from'] = self._decode_header(msg_txt.get('From'))
-
-        if 'Delivered-To' in fields:
-            msg['to'] = self._decode_header(msg_txt.get('Delivered-To'))
-
-        if 'CC' in fields:
-            msg['cc'] = self._decode_header(msg_txt.get('CC'))
-
-        if 'Reply-to' in fields:
-            msg['reply'] = self._decode_header(msg_txt.get('Reply-To'))
-
-        if 'Date' in fields:
-            msg['date'] = self._decode_header(msg_txt.get('Date'))
-
-        if 'Content-Transfer-Encoding' in fields:
-            msg['encoding'] = msg_txt.get('Content-Transfer-Encoding')
-
-        if 'References' in fields:
-            msg['references'] = msg_txt.get('References')
-
-        if 'In-Reply-To' in fields:
-            msg['in-reply-to'] = msg_txt.get('In-Reply-To')
-
-        if 'X-Priority' in fields:
-            msg['priority'] = msg_txt.get('X-Priority', '3 (Normal)').split(' ')[0]
-
-        if not msg_txt.is_multipart() or 'text/plain' in msg.get('Content-Type', ''):
-            encoding = msg_txt.get_content_charset()
-            body = msg_txt.get_payload(decode=True)
-            msg['body'] = tools.ustr(body, encoding)
-
-        attachments = {}
-        has_plain_text = False
-        if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', ''):
-            body = ""
-            for part in msg_txt.walk():
-                if part.get_content_maintype() == 'multipart':
-                    continue
-
-                encoding = part.get_content_charset()
-                filename = part.get_filename()
-                if part.get_content_maintype()=='text':
-                    content = part.get_payload(decode=True)
-                    if filename:
-                        attachments[filename] = content
-                    elif not has_plain_text:
-                        # main content parts should have 'text' maintype
-                        # and no filename. we ignore the html part if
-                        # there is already a plaintext part without filename,
-                        # because presumably these are alternatives.
-                        content = tools.ustr(content, encoding)
-                        if part.get_content_subtype() == 'html':
-                            body = tools.ustr(tools.html2plaintext(content))
-                        elif part.get_content_subtype() == 'plain':
-                            body = content
-                            has_plain_text = True
-                elif part.get_content_maintype() in ('application', 'image'):
-                    if filename :
-                        attachments[filename] = part.get_payload(decode=True)
-                    else:
-                        res = part.get_payload(decode=True)
-                        body += tools.ustr(res, encoding)
-
-            msg['body'] = body
-            msg['attachments'] = attachments
-        return msg
-email_server_tools()
+email_thread()
 
 class thunderbird_partner(osv.osv_memory):
     _name = "thunderbird.partner"
@@ -149,10 +66,10 @@
         ref_ids = str(dictcreate.get('ref_ids')).split(';')
         msg = dictcreate.get('message')
         mail = msg
-        msg = self.pool.get('email.server.tools').parse_message(msg)
-        server_tools_pool = self.pool.get('email.server.tools')
+        msg = self.pool.get('email.message').parse_message(msg)
+        thread_pool = self.pool.get('email.thread')
         message_id = msg.get('message-id', False)
-        msg_pool = self.pool.get('mailgate.message')
+        msg_pool = self.pool.get('email.message')
         msg_ids = []
         res = {}
         res_ids = []
@@ -191,7 +108,7 @@
                 res['datas'] = base64.b64encode(mail)
                 res['res_id'] = res_id