[MERGE+IMP] crm: utf-8 problems in mailagte

bzr revid: hmo@tinyerp.com-20100331141442-wsdldsd0ckja2c6i
This commit is contained in:
Harry (Open ERP) 2010-03-31 19:44:42 +05:30
commit fbeab98cf1
6 changed files with 102 additions and 43 deletions

View File

@ -344,11 +344,13 @@ class crm_case(osv.osv):
obj = self.pool.get('crm.case.history')
data['description'] = details or case.description
data['email_to'] = email or \
(case.section_id and case.section_id.reply_to) or \
(case.user_id and case.user_id.address_id and \
case.user_id.address_id.email) or False
case.user_id.address_id.email) or tools.config.get('email_from',False)
data['email_from'] = email_from or \
(case.section_id and case.section_id.reply_to) or \
(case.user_id and case.user_id.address_id and \
case.user_id.address_id.email) or False
case.user_id.address_id.email) or tools.config.get('email_from',False)
res = obj.create(cr, uid, data, context)
return True
_history = __history

View File

@ -55,11 +55,14 @@ class case(osv.osv):
if case.section_id.reply_to and case.email_from:
src = case.email_from
dest = case.section_id.reply_to
body = case.email_last or case.description
body = ""
body = case.email_last or case.description
if not destination:
src, dest = dest, src
if case.user_id.signature:
body += '\n\n%s' % (case.user_id.signature or '')
if body and case.user_id.signature:
body += '\n\n%s' % (case.user_id.signature)
body = self.format_body(body)
dest = [dest]
attach_to_send = None
@ -74,15 +77,16 @@ class case(osv.osv):
src,
dest,
"Reminder: [%s] %s" % (str(case.id), case.name, ),
self.format_body(body),
body,
reply_to=case.section_id.reply_to,
openobject_id=str(case.id),
attach=attach_to_send
)
if flag:
raise osv.except_osv(_('Email!'),("Email Successfully Sent"))
else:
raise osv.except_osv(_('Email Fail!'),("Email is not sent successfully"))
self._history(cr, uid, [case], _('Send'), history=True, email=dest, details=body, email_from=src)
#if flag:
# raise osv.except_osv(_('Email!'),("Email Successfully Sent"))
#else:
# raise osv.except_osv(_('Email Fail!'),("Email is not sent successfully"))
return True
def _check(self, cr, uid, ids=False, context={}):
@ -120,6 +124,23 @@ case()
class base_action_rule(osv.osv):
_inherit = 'base.action.rule'
_description = 'Action Rules'
def email_send(self, cr, uid, obj, emails, body, emailfrom=tools.config.get('email_from',False), context={}):
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:
reply_to = obj.section_id.reply_to
else:
reply_to = emailfrom
if not emailfrom:
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))
def do_check(self, cr, uid, action, obj, context={}):
ok = super(base_action_rule, self).do_check(cr, uid, action, obj, context=context)
@ -146,7 +167,7 @@ class base_action_rule(osv.osv):
res = super(base_action_rule, self).do_action(cr, uid, action, model_obj, obj, context=context)
write = {}
if action.act_section_id:
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

View File

@ -34,24 +34,28 @@ from osv.orm import except_orm
class crm_cases(osv.osv):
_name = "crm.case"
_inherit = "crm.case"
_inherit = "crm.case"
def msg_new(self, cr, uid, msg):
mailgate_obj = self.pool.get('mail.gateway')
msg_body = mailgate_obj.msg_body_get(msg)
msg_subject = mailgate_obj._decode_header(msg['Subject'])
msg_from = mailgate_obj._decode_header(msg['From'])
msg_cc = mailgate_obj._decode_header(msg['Cc'])
body = self.format_body(msg_body['body'])
data = {
'name': msg['Subject'],
'email_from': msg['From'],
'email_cc': msg['Cc'],
'name': msg_subject,
'email_from': msg_from,
'email_cc': msg_cc,
'user_id': False,
'description': msg_body['body'],
'description': body,
}
res = mailgate_obj.partner_get(cr, uid, msg['From'])
res = mailgate_obj.partner_get(cr, uid, msg_from)
if res:
data.update(res)
res = self.create(cr, uid, data)
cases = self.browse(cr, uid, [res])
self._history(cr, uid, cases, _('Receive'), history=True, email=msg['From'])
self._history(cr, uid, cases, _('Receive'), history=True, details=body, email_from=msg_from)
return res
def msg_update(self, cr, uid, ids, msg, data={}, default_act='pending'):
@ -79,10 +83,10 @@ class crm_cases(osv.osv):
if 'partner' in msg_actions:
data['email_from'] = msg_actions['partner'][:128]
msg_from = self._decode_header(msg['From'])
res = self.write(cr, uid, select, data)
cases = self.browse(cr, uid, select)
self._history(cr, uid, cases, _('Receive'), history=True, email=msg['From'])
self._history(cr, uid, cases, _('Receive'), history=True, details=body_data, email_from=msg['From'])
getattr(self,act)(cr, uid, select)
return res

View File

@ -34,7 +34,7 @@ import time, socket
email_re = re.compile(r"([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6})")
case_re = re.compile(r"\[([0-9]+)\]", re.UNICODE)
command_re = re.compile("^Set-([a-z]+) *: *(.+)$", re.I + re.UNICODE)
reference_re = re.compile("<.*-tinycrm-(\\d+)@(.*)>", re.UNICODE)
reference_re = re.compile("<.*-openobject-(\\d+)@(.*)>", re.UNICODE)
priorities = {
'1': '1 (Highest)',
@ -157,21 +157,37 @@ class email_parser(object):
'partner_id': adr[0].get('partner_id', False) and adr[0]['partner_id'][0] or False
}
def _to_decode(self, s, charsets):
for charset in charsets:
if charset:
try:
return s.decode(charset)
except UnicodeError:
pass
try:
return s.decode('ascii')
except UnicodeError:
return s
def _decode_header(self, s):
from email.Header import decode_header
s = decode_header(s)
return ''.join(map(lambda x:x[0].decode(x[1] or 'ascii', 'replace'), s))
s = decode_header(s)
return ''.join(map(lambda x:self._to_decode(x[0], [x[1]]), s))
def msg_new(self, msg):
message = self.msg_body_get(msg)
msg_subject = self._decode_header(msg['Subject'])
msg_from = self._decode_header(msg['From'])
msg_cc = self._decode_header(msg['Cc'] or '')
data = {
'name': self._decode_header(msg['Subject']),
'email_from': self._decode_header(msg['From']),
'email_cc': self._decode_header(msg['Cc'] or ''),
'name': msg_subject,
'email_from': msg_from,
'email_cc': msg_cc,
'user_id': False,
'description': message['body'],
}
data.update(self.partner_get(self._decode_header(msg['From'])))
data.update(self.partner_get(msg_from))
try:
id = self.rpc(self.model, 'create', data)
@ -222,7 +238,7 @@ class email_parser(object):
if part.get_content_maintype()=='text':
buf = part.get_payload(decode=True)
if buf:
txt = buf.decode(part.get_charsets()[0] or 'ascii', 'replace')
txt = self._to_decode(buf, part.get_charsets())
txt = re.sub("<(\w)>", replace, txt)
txt = re.sub("<\/(\w)>", replace, txt)
if txt and part.get_content_subtype() == 'plain':

View File

@ -79,14 +79,15 @@ class crm_send_new_email(osv.osv_memory):
if case.user_id.signature:
body += '\n\n%s' % (case.user_id.signature)
case_pool._history(cr, uid, [case], _('Send'), history=True, email=data['email_to'], details=body)
body = case_pool.format_body(body)
email_from = data.get('email_from', False)
case_pool._history(cr, uid, [case], _('Send'), history=True, email=data['email_to'], details=body, email_from=email_from)
flag = tools.email_send(
email_from,
emails,
data['subject'],
case_pool.format_body(body),
body,
attach=attach,
reply_to=case.section_id.reply_to,
openobject_id=str(case.id),
@ -107,6 +108,7 @@ class crm_send_new_email(osv.osv_memory):
# raise osv.except_osv(_('Email!'), ("Email Successfully Sent"))
# else:
# raise osv.except_osv(_('Warning!'), _("Email not sent !"))
return {}
def default_get(self, cr, uid, fields, context=None):
@ -133,7 +135,9 @@ class crm_send_new_email(osv.osv_memory):
if 'email_to' in fields:
res.update({'email_to': case.email_from})
if 'email_from' in fields:
res.update({'email_from': (case.user_id and case.user_id.address_id and case.user_id.address_id.email) or tools.config.get('email_from',False)})
res.update({'email_from': (case.section_id and case.section_id.reply_to) or \
(case.user_id and case.user_id.address_id and \
case.user_id.address_id.email) or tools.config.get('email_from',False)})
if 'subject' in fields:
res.update({'subject': '[%s] %s' %(str(case.id), case.name or '')})
if 'email_cc' in fields:
@ -156,12 +160,14 @@ class crm_send_new_email(osv.osv_memory):
model_pool = self.pool.get(model)
case = model_pool.browse(cr, uid, hist.log_id.res_id)
if 'email_to' in fields:
res['email_to']=case.email_from or hist.email_from or ''
res.update({'email_to': case.email_from or hist.email_from or False})
if 'email_from' in fields:
res['email_from']=(case.user_id and case.user_id.address_id and case.user_id.address_id.email) or hist.email_to or tools.config.get('email_from','')
res.update({'email_from': (case.section_id and case.section_id.reply_to) or \
(case.user_id and case.user_id.address_id and \
case.user_id.address_id.email) or hist.email_to or tools.config.get('email_from',False)})
if 'text' in fields:
header = '\n\n-------- Original Message --------'
sender = 'From: %s' % (hist.email_from or '')
header = '-------- Original Message --------'
sender = 'From: %s' %(hist.email_from or '')
to = 'To: %s' % (hist.email_to or '')
sentdate = 'Date: %s' % (hist.date)
desc = '\n%s'%(hist.description)

View File

@ -210,10 +210,22 @@ class mail_gateway(osv.osv):
}
return res
def _decode_header(self, s):
def _to_decode(self, s, charsets):
for charset in charsets:
if charset:
try:
return s.decode(charset)
except UnicodeError:
pass
try:
return s.decode('ascii')
except UnicodeError:
return s
def _decode_header(self, s):
from email.Header import decode_header
s = decode_header(s)
return ''.join(map(lambda x:x[0].decode(x[1] or 'ascii', 'replace'), s))
return ''.join(map(lambda x:self._to_decode(x[0], x[1]), s))
def msg_new(self, cr, uid, msg, model):
message = self.msg_body_get(msg)
@ -250,7 +262,7 @@ class mail_gateway(osv.osv):
if part.get_content_maintype()=='text':
buf = part.get_payload(decode=True)
if buf:
txt = buf.decode(part.get_charsets()[0] or 'ascii', 'replace')
txt = self._to_decode(buf, part.get_charsets)
txt = re.sub("<(\w)>", replace, txt)
txt = re.sub("<\/(\w)>", replace, txt)
if txt and part.get_content_subtype() == 'plain':
@ -362,13 +374,11 @@ class mail_gateway(osv.osv):
del msg['Subject']
msg['Subject'] = '[%s] %s' %(str(res_id), subject)
em = [user_email, from_email] + (cc_email or '').split(',')
em = [user_email or '', from_email] + (cc_email or '').split(',')
emails = map(self.emails_get, filter(None, em))
mm = [self._decode_header(msg['From']), self._decode_header(msg['To'])]+self._decode_header(msg.get('Cc','')).split(',')
msg_mails = map(self.emails_get, filter(None, mm))
emails = filter(lambda m: m and m not in msg_mails, emails)
emails = filter(lambda m: m and m not in msg_mails, emails)
try:
self.msg_send(msg, mailgateway.reply_to, emails, priority, res_id)
if hasattr(self.pool.get(res_model), 'msg_send'):