[FIX]: mail_gateway: Encoding problem in send email
[odoo/odoo.git] / addons / mail_gateway / mail_gateway.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>)
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
19 #
20 ##############################################################################
21
22 from osv import osv, fields
23 import time
24 import tools
25 import binascii
26 import email
27 from email.header import decode_header
28 import base64
29 import re
30 from tools.translate import _
31 import logging
32
33 _logger = logging.getLogger('mailgate')
34
35 class mailgate_thread(osv.osv):
36     '''
37     Mailgateway Thread
38     '''
39     _name = 'mailgate.thread'
40     _description = 'Mailgateway Thread'
41
42     _columns = {
43         'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('history', '=', True)]),
44         'log_ids': fields.one2many('mailgate.message', 'res_id', 'Logs', domain=[('history', '=', False)]),
45     }
46
47     def message_new(self, cr, uid, msg, context):
48         raise Exception, _('Method is not implemented')
49
50     def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context={}):
51         raise Exception, _('Method is not implemented')
52
53     def emails_get(self, cr, uid, ids, context=None):
54         raise Exception, _('Method is not implemented')
55
56     def msg_send(self, cr, uid, id, *args, **argv):
57         raise Exception, _('Method is not implemented')
58
59     def _history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, \
60                     email_from=False, message_id=False, references=None, attach=None, context=None):
61         """
62         @param self: The object pointer
63         @param cr: the current row, from the database cursor,
64         @param uid: the current user’s ID for security checks,
65         @param cases: a browse record list
66         @param keyword: Case action keyword e.g.: If case is closed "Close" keyword is used
67         @param history: Value True/False, If True it makes entry in case History otherwise in Case Log
68         @param email: Email address if any
69         @param details: Details of case history if any 
70         @param atach: Attachment sent in email
71         @param context: A standard dictionary for contextual values"""
72         if context is None:
73             context = {}
74         if attach is None:
75             attach = []
76
77         # The mailgate sends the ids of the cases and not the object list
78
79         if all(isinstance(case_id, (int, long)) for case_id in cases):
80             cases = self.browse(cr, uid, cases, context=context)
81
82         att_obj = self.pool.get('ir.attachment')
83         obj = self.pool.get('mailgate.message')
84
85         for case in cases:
86             data = {
87                 'name': keyword, 
88                 'user_id': uid, 
89                 'model' : case._name, 
90                 'res_id': case.id, 
91                 'date': time.strftime('%Y-%m-%d %H:%M:%S'), 
92                 'message_id': message_id, 
93             }
94             attachments = []
95             if history:
96                 for att in attach:
97                     attachments.append(att_obj.create(cr, uid, {'name': att[0], 'datas': base64.encodestring(att[1])}))
98
99                 data = {
100                     'name': subject or 'History', 
101                     'history': True, 
102                     'user_id': uid, 
103                     'model' : case._name, 
104                     'res_id': case.id,
105                     'date': time.strftime('%Y-%m-%d %H:%M:%S'), 
106                     'description': details or (hasattr(case, 'description') and case.description or False), 
107                     'email_to': email or \
108                         (hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
109                          case.user_id.address_id.email) or tools.config.get('email_from', False), 
110                     'email_from': email_from or \
111                         (hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
112                          case.user_id.address_id.email) or tools.config.get('email_from', False), 
113                     'partner_id': hasattr(case, 'partner_id') and (case.partner_id and case.partner_id.id or False) or False, 
114                     'references': references, 
115                     'message_id': message_id, 
116                     'attachment_ids': [(6, 0, attachments)]
117                 }
118             res = obj.create(cr, uid, data, context)
119         return True
120 mailgate_thread()
121
122 class mailgate_message(osv.osv):
123     '''
124     Mailgateway Message
125     '''
126     _name = 'mailgate.message'
127     _description = 'Mailgateway Message'
128     _order = 'id desc'
129     _columns = {
130         'name':fields.char('Message', size=64), 
131         'model': fields.char('Object Name', size=128), 
132         'res_id': fields.integer('Resource ID'),
133         'ref_id': fields.char('Reference Id', size=256, readonly=True, help="Message Id in Email Server.", select=True),
134         'date': fields.datetime('Date'), 
135         'history': fields.boolean('Is History?'),
136         'user_id': fields.many2one('res.users', 'User Responsible', readonly=True), 
137         'message': fields.text('Description'), 
138         'email_from': fields.char('Email From', size=84), 
139         'email_to': fields.char('Email To', size=84), 
140         'email_cc': fields.char('Email CC', size=84), 
141         'email_bcc': fields.char('Email BCC', size=84), 
142         'message_id': fields.char('Message Id', size=1024, readonly=True, help="Message Id on Email.", select=True),
143         'references': fields.text('References', readonly=True, help="Referencess emails."),
144         'description': fields.text('Description'), 
145         'partner_id': fields.many2one('res.partner', 'Partner', required=False), 
146         'attachment_ids': fields.many2many('ir.attachment', 'message_attachment_rel', 'message_id', 'attachment_id', 'Attachments'), 
147     }
148
149 mailgate_message()
150
151 class mailgate_tool(osv.osv_memory):
152
153     _name = 'email.server.tools'
154     _description = "Email Server Tools"
155     
156     def _to_decode(self, s, charsets):
157         if not s:
158             return s
159         for charset in charsets:
160             if charset:
161                 try:
162                     return s.decode(charset)
163                 except UnicodeError:
164                     pass
165         return s.decode('latin1')
166
167     def _decode_header(self, text):
168         if text:
169             text = decode_header(text.replace('\r', '')) 
170         return ''.join(map(lambda x:self._to_decode(x[0], [x[1]]), text or []))
171  
172     def to_email(self, text):
173         _email = re.compile(r'.*<.*@.*\..*>', re.UNICODE)
174         def record(path):
175             eml = path.group()
176             index = eml.index('<')
177             eml = eml[index:-1].replace('<', '').replace('>', '')
178             return eml
179
180         bits = _email.sub(record, text)
181         return bits
182     
183     def history(self, cr, uid, model, res_ids, msg, attach, context=None):
184         """This function creates history for mails fetched
185         @param self: The object pointer
186         @param cr: the current row, from the database cursor,
187         @param uid: the current user’s ID for security checks,
188         @param model: OpenObject Model
189         @param res_ids: Ids of the record of OpenObject model created 
190         @param msg: Email details
191         @param attach: Email attachments
192         """
193         if isinstance(res_ids, (int, long)):
194             res_ids = [res_ids]
195
196         msg_pool = self.pool.get('mailgate.message')
197         for res_id in res_ids:
198             msg_data = {
199                         'name': msg.get('subject', 'No subject'), 
200                         'date': msg.get('date') , 
201                         'description': msg.get('body', msg.get('from')), 
202                         'history': True,
203                         'res_model': model, 
204                         'email_cc': msg.get('cc'), 
205                         'email_from': msg.get('from'), 
206                         'email_to': msg.get('to'), 
207                         'message_id': msg.get('message-id'), 
208                         'references': msg.get('references'), 
209                         'res_id': res_id,
210                         'user_id': uid, 
211                         'attachment_ids': [(6, 0, attach)]
212             }
213             msg_id = msg_pool.create(cr, uid, msg_data, context=context)
214         return True
215     
216     def email_send(self, cr, uid, model, res_id, msg, from_email=False, email_default=False):
217         """This function Sends return email on submission of  Fetched email in OpenERP database
218         @param self: The object pointer
219         @param cr: the current row, from the database cursor,
220         @param uid: the current user’s ID for security checks,
221         @param model: OpenObject Model
222         @param res_id: Id of the record of OpenObject model created from the Email details 
223         @param msg: Email details
224         @param email_default: Default Email address in case of any Problem
225         """
226         history_pool = self.pool.get('mailgate.message')
227         model_pool = self.pool.get(model)
228         from_email = from_email or tools.config.get('email_from', None)
229         message = email.message_from_string(tools.ustr(msg).encode('utf-8'))
230         subject = "[%s] %s" %(res_id, message['Subject'])
231         #msg_mails = []
232         #mails = [self._decode_header(message['From']), self._decode_header(message['To'])]
233         #mails += self._decode_header(message.get('Cc', '')).split(',')
234
235         values = {}
236         if hasattr(model_pool, 'emails_get'):
237             values = model_pool.emails_get(cr, uid, [res_id])
238         emails = values.get(res_id, {})
239
240         priority = emails.get('priority', [3])[0]
241         em = emails['user_email'] + emails['email_from'] + emails['email_cc']
242         msg_mails = map(self.to_email, filter(None, em))
243
244         #mm = [self._decode_header(message['From']), self._decode_header(message['To'])]
245         #mm += self._decode_header(message.get('Cc', '')).split(',')
246
247         #msg_mails = map(self.to_email, filter(None, mm))        
248         
249         encoding = message.get_content_charset()
250         message['body'] = message.get_payload(decode=True)
251         if encoding:
252             message['body'] = self._to_decode(message['body'], [encoding])
253
254         from_mail = self._decode_header(message['From'])
255         body = _("""
256 Hello %s,""" % (from_mail))
257         body += _("""
258
259     Your Request ID: %s""") % (res_id)
260         body += _("""
261         
262 Thanks
263
264 -------- Original Message --------        
265 %s
266 """) % (self._to_decode(message['body'], [encoding]))
267         res = None
268         try:
269             res = tools.email_send(from_email, msg_mails, subject, body, openobject_id=res_id)
270         except Exception, e:
271             if email_default:
272                 temp_msg = '[%s] %s'%(res_id, message['Subject'])
273                 del message['Subject']
274                 message['Subject'] = '[OpenERP-FetchError] %s' %(temp_msg)
275                 tools.email_send(from_email, email_default, message.get('Subject'), message.get('body'), openobject_id=res_id)
276         return res
277
278     def process_email(self, cr, uid, model, message, attach=True, context=None):
279         """This function Processes email and create record for given OpenERP model 
280         @param self: The object pointer
281         @param cr: the current row, from the database cursor,
282         @param uid: the current user’s ID for security checks,
283         @param model: OpenObject Model
284         @param message: Email details
285         @param attach: Email attachments
286         @param context: A standard dictionary for contextual values"""
287
288         model_pool = self.pool.get(model)
289         if not context:
290             context = {}
291         res_id = False
292         # Create New Record into particular model
293         def create_record(msg):
294             if hasattr(model_pool, 'message_new'):
295                 res_id = model_pool.message_new(cr, uid, msg, context)
296             else:
297                 data = {
298                     'name': msg.get('subject'),
299                     'email_from': msg.get('from'),
300                     'email_cc': msg.get('cc'),
301                     'user_id': False,
302                     'description': msg.get('body'),
303                     'state' : 'draft',
304                 }
305                 data.update(self.get_partner(cr, uid, msg.get('from'), context=context))
306                 res_id = model_pool.create(cr, uid, data, context=context)
307
308                 att_ids = []
309                 if attach:
310                     for attachment in msg.get('attachments', []):
311                         data_attach = {
312                             'name': attachment,
313                             'datas': binascii.b2a_base64(str(attachments.get(attachment))),
314                             'datas_fname': attachment,
315                             'description': 'Mail attachment',
316                             'res_model': model,
317                             'res_id': res_id,
318                         }
319                         att_ids.append(self.pool.get('ir.attachment').create(cr, uid, data_attach))
320
321             return res_id
322
323         history_pool = self.pool.get('mailgate.message')
324
325         # Warning: message_from_string doesn't always work correctly on unicode,
326         # we must use utf-8 strings here :-(
327         msg_txt = email.message_from_string(tools.ustr(message).encode('utf-8'))
328         message_id = msg_txt.get('Message-ID', False)
329         msg = {}
330
331         if not message_id:
332             # Very unusual situation, be we should be fault-tolerant here
333             message_id = time.time()
334             msg_txt['Message-ID'] = message_id
335             _logger.info('Message without message-id, generating a random one: %s', message_id)
336
337         fields = msg_txt.keys()
338         msg['id'] = message_id
339         msg['message-id'] = message_id
340
341         if 'Subject' in fields:
342             msg['subject'] = self._decode_header(msg_txt.get('Subject'))
343
344         if 'Content-Type' in fields:
345             msg['content-type'] = msg_txt.get('Content-Type')
346
347         if 'From' in fields:
348             msg['from'] = self._decode_header(msg_txt.get('From'))
349
350         if 'Delivered-To' in fields:
351             msg['to'] = self._decode_header(msg_txt.get('Delivered-To'))
352
353         if 'Cc' in fields:
354             msg['cc'] = self._decode_header(msg_txt.get('Cc'))
355
356         if 'Reply-To' in fields:
357             msg['reply'] = self._decode_header(msg_txt.get('Reply-To'))
358
359         if 'Date' in fields:
360             msg['date'] = msg_txt.get('Date')
361
362         if 'Content-Transfer-Encoding' in fields:
363             msg['encoding'] = msg_txt.get('Content-Transfer-Encoding')
364
365         if 'References' in fields:
366             msg['references'] = msg_txt.get('References')
367
368         if 'X-Priority' in fields:
369             msg['priority'] = msg_txt.get('X-priority', '3 (Normal)').split(' ')[0]
370
371         if not msg_txt.is_multipart() or 'text/plain' in msg.get('content-type', ''):
372             encoding = msg_txt.get_content_charset()
373             msg['body'] = msg_txt.get_payload(decode=True)
374             if encoding:
375                 msg['body'] = tools.ustr(msg['body'])
376
377         attachments = {}
378         if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', ''):
379             body = ""
380             counter = 1
381             for part in msg_txt.walk():
382                 if part.get_content_maintype() == 'multipart':
383                     continue
384
385                 encoding = part.get_content_charset()
386
387                 if part.get_content_maintype()=='text':
388                     content = part.get_payload(decode=True)
389                     filename = part.get_filename()
390                     if filename :
391                         attachments[filename] = content
392                     else:
393                         if encoding:
394                             content = unicode(content, encoding)
395                         if part.get_content_subtype() == 'html':
396                             body = tools.html2plaintext(content)
397                         elif part.get_content_subtype() == 'plain':
398                             body = content
399                 elif part.get_content_maintype()=='application' or part.get_content_maintype()=='image' or part.get_content_maintype()=='text':
400                     filename = part.get_filename();
401                     if filename :
402                         attachments[filename] = part.get_payload(decode=True)
403                     else:
404                         res = part.get_payload(decode=True)
405                         if encoding:
406                             res = tools.ustr(res)
407
408                         body += res
409
410             msg['body'] = body
411             msg['attachments'] = attachments
412         res_ids = []
413         new_res_id = False
414         if msg.get('references'):
415             references = msg.get('references')
416             if '\r\n' in references:
417                 references = msg.get('references').split('\r\n')
418             else:
419                 references = msg.get('references').split(' ')
420             for ref in references:
421                 ref = ref.strip()
422                 res_id = tools.misc.reference_re.search(ref)
423                 if res_id:
424                     res_id = res_id.group(1)
425                 else:
426                     res_id = tools.misc.res_re.search(msg['subject'])
427                     if res_id:
428                         res_id = res_id.group(1)
429                 if res_id:
430                     res_id = int(res_id)
431                     res_ids.append(res_id)
432                     model_pool = self.pool.get(model)
433
434                     vals = {}
435                     if hasattr(model_pool, 'message_update'):
436                         model_pool.message_update(cr, uid, [res_id], vals, msg, context=context)
437
438         if not len(res_ids):
439             new_res_id = create_record(msg)
440             res_ids = [new_res_id]
441         # Store messages
442         context.update({'model' : model})
443         if hasattr(model_pool, '_history'):
444             model_pool._history(cr, uid, res_ids, _('Receive'), history=True, 
445                             subject = msg.get('subject'), 
446                             email = msg.get('to'), 
447                             details = msg.get('body'), 
448                             email_from = msg.get('from'), 
449                             message_id = msg.get('message-id'), 
450                             references = msg.get('references', False),
451                             attach = msg.get('attachments', {}).items(), 
452                             context = context)
453         else:
454             self.history(cr, uid, model, res_ids, msg, att_ids, context=context)
455         return new_res_id
456
457     def get_partner(self, cr, uid, from_email, context=None):
458         """This function returns partner Id based on email passed
459         @param self: The object pointer
460         @param cr: the current row, from the database cursor,
461         @param uid: the current user’s ID for security checks
462         @param from_email: email address based on that function will search for the correct
463         """
464         address_pool = self.pool.get('res.partner.address')
465         res = {
466             'partner_address_id': False,
467             'partner_id': False
468         }
469         from_email = self.to_email(from_email)
470         address_ids = address_pool.search(cr, uid, [('email', '=', from_email)])
471         if address_ids:
472             address = address_pool.browse(cr, uid, address_ids[0])
473             res['partner_address_id'] = address_ids[0]
474             res['partner_id'] = address.partner_id.id
475
476         return res
477
478 mailgate_tool()
479
480