[ADD]: Added mail_gateway module which will replace the email gateway functionalities of crm module. Now we should be able to connect the mailgateway to several objects. Removed the gateway stuff from crm and put them in this new module.

bzr revid: uco@tinyerp.co.in-20091230130931-kulhk1qsmov9c1qo
This commit is contained in:
uco (OpenERP) 2009-12-30 18:39:31 +05:30
parent 995763d040
commit e7546b682c
17 changed files with 472 additions and 298 deletions

View File

@ -46,7 +46,8 @@ between mails and Open ERP.""",
'website': 'http://www.openerp.com',
'depends': ['base',
'caldav',
'process'
'process',
'mail_gateway',
],
'init_xml': ['crm_data.xml',
'crm_meeting_data.xml',

View File

@ -32,7 +32,7 @@ import tools
from osv import fields,osv,orm
from osv.orm import except_orm
from scripts.openerp_mailgate import openerp_mailgate
#from scripts.openerp_mailgate import openerp_mailgate
import email
import netsvc
from poplib import POP3, POP3_SSL
@ -74,7 +74,7 @@ class crm_case_section(osv.osv):
'reply_to': fields.char('Reply-To', size=64, help="The email address put in the 'Reply-To' of all emails sent by Open ERP about cases in this section"),
'parent_id': fields.many2one('crm.case.section', 'Parent Section'),
'child_ids': fields.one2many('crm.case.section', 'parent_id', 'Child Sections'),
"gateway_ids" : fields.one2many("crm.email.gateway",'section_id',"Email Gateways"),
"gateway_ids" : fields.one2many("mail.gateway",'section_id',"Email Gateways"),
'calendar' : fields.boolean('Calendar', help='Allows to show calendar'),
}
_defaults = {
@ -209,147 +209,14 @@ class crm_case_section(osv.osv):
return res
crm_case_section()
class crm_email_gateway_server(osv.osv):
_name = "crm.email.gateway.server"
_description = "Email Gateway Server"
_columns = {
'name': fields.char('Server Address',size=64,required=True ,help="IMAP/POP Address Of Email gateway Server"),
'login': fields.char('User',size=64,required=True,help="User Login Id of Email gateway"),
'password': fields.char('Password',size=64,required=True,help="User Password Of Email gateway"),
'server_type': fields.selection([("pop","POP"),("imap","Imap")],"Type of Server", required=True, help="Type of Email gateway Server"),
'port': fields.integer("Port" , help="Port Of Email gateway Server. If port is omitted, the standard POP3 port (110) is used for POP EMail Server and the standard IMAP4 port (143) is used for IMAP Sever."),
'ssl': fields.boolean('SSL',help ="Use Secure Authentication"),
'active': fields.boolean('Active', help="If the active field is set to true, it will allow you to hide the email gateway server without removing it."),
}
_defaults = {
'server_type':lambda * a:'pop',
'active':lambda * a:True,
}
def onchange_server_type(self, cr, uid, ids, server_type=False, ssl=False):
port = 0
if server_type == 'pop':
port = ssl and 995 or 110
elif server_type == 'imap':
port = ssl and 993 or 143
return {'value':{'port':port}}
crm_email_gateway_server()
class crm_email_gateway(osv.osv):
_name = "crm.email.gateway"
class mail_gateway(osv.osv):
_inherit = "mail.gateway"
_description = "Email Gateway"
_columns = {
'name': fields.char('Name',size=64,help="Name of Mail Gateway."),
'server_id': fields.many2one('crm.email.gateway.server',"Gateway Server", required=True),
'to_email_id': fields.char('TO', size=64, help="Email address used in the From field of outgoing messages"),
'cc_email_id': fields.char('CC',size=64,help="Default eMail in case of any trouble."),
'section_id': fields.many2one('crm.case.section',"Section",required=True),
'mail_history': fields.one2many("crm.email.history","gateway_id","History", readonly=True)
'section_id': fields.many2one('crm.case.section',"Section",required=True),
}
def _fetch_mails(self, cr, uid, ids=False, context={}):
'''
Function called by the scheduler to fetch mails
'''
cr.execute('select * from crm_email_gateway gateway \
inner join crm_email_gateway_server server \
on server.id = gateway.server_id where server.active = True')
ids2 = map(lambda x: x[0], cr.fetchall() or [])
return self.fetch_mails(cr, uid, ids=ids2, context=context)
def parse_mail(self, cr, uid, gateway_id, email_message, email_parser=None, context={}):
msg_id = case_id = note = False
user_obj = self.pool.get('res.users')
mail_history_obj = self.pool.get('crm.email.history')
users = user_obj.read(cr, uid, uid, ['password'])
mailgateway = self.browse(cr, uid, gateway_id, context=context)
try :
if not email_parser:
email_parser = openerp_mailgate.email_parser(uid, users['password'], mailgateway.section_id.id,
mailgateway.to_email_id or '', mailgateway.cc_email_id or '', dbname=cr.dbname,
host=tools.config['interface'] or 'localhost', port=tools.config['port'] or '8069')
msg_txt = email.message_from_string(email_message)
msg_id = msg_txt['Message-ID']
case_id = email_parser.parse(msg_txt)[0]
except Exception, e:
note = "Error in Parsing Mail: %s " %(str(e))
netsvc.Logger().notifyChannel('Emailgate:Parsing mail:%s' % (mailgateway.name or
'%s (%s)'%(mailgateway.server_id.login, mailgateway.server_id.name)), netsvc.LOG_ERROR, str(e))
mail_history_obj.create(cr, uid, {'name':msg_id, 'case_id': case_id, 'gateway_id':mailgateway.id, 'note':note})
return case_id,note
def fetch_mails(self, cr, uid, ids=[], section_ids=[], context={}):
if len(section_ids):
casesection_obj = self.pool.get('crm.case.section')
for section in casesection_obj.read(cr, uid, section_ids, ['gateway_ids']):
ids += section['gateway_ids']
log_messages = []
for mailgateway in self.browse(cr, uid, ids):
try :
mailgate_server = mailgateway.server_id
if not mailgate_server.active:
continue
mailgate_name = mailgateway.name or "%s (%s)" % (mailgate_server.login, mailgate_server.name)
log_messages.append("Mail Server : %s" % mailgate_name)
log_messages.append("="*40)
new_messages = []
if mailgate_server.server_type == 'pop':
if mailgate_server.ssl:
pop_server = POP3_SSL(mailgate_server.name or 'localhost', mailgate_server.port or 110)
else:
pop_server = POP3(mailgate_server.name or 'localhost', mailgate_server.port or 110)
pop_server.user(mailgate_server.login)
pop_server.pass_(mailgate_server.password)
pop_server.list()
(numMsgs, totalSize) = pop_server.stat()
for i in range(1, numMsgs + 1):
(header, msges, octets) = pop_server.retr(i)
case_id, note = self.parse_mail(cr, uid, mailgateway.id, '\n'.join(msges))
log = ''
if case_id:
log = _('Case Successfull Created : %d'% case_id)
if note:
log = note
log_messages.append(log)
new_messages.append(i)
pop_server.quit()
elif mailgate_server.server_type == 'imap':
if mailgate_server.ssl:
imap_server = IMAP4_SSL(mailgate_server.name or 'localhost', mailgate_server.port or 143)
else:
imap_server = IMAP4(mailgate_server.name or 'localhost', mailgate_server.port or 143)
imap_server.login(mailgate_server.login, mailgate_server.password)
imap_server.select()
typ, data = imap_server.search(None, '(UNSEEN)')
for num in data[0].split():
typ, data = imap_server.fetch(num, '(RFC822)')
case_id, note = self.parse_mail(cr, uid, mailgateway.id, data[0][1])
log = ''
if case_id:
log = 'Case Successfully Created : %d'% case_id
if note:
log = note
log_messages.append(log)
new_messages.append(num)
imap_server.close()
imap_server.logout()
except Exception, e:
log_messages.append("Error in Fetching Mail: %s " %(str(e)))
netsvc.Logger().notifyChannel('Emailgate:Fetching mail:[%d]%s' % (mailgate_server.id, mailgate_server.name), netsvc.LOG_ERROR, str(e))
log_messages.append("-"*25)
log_messages.append("Total Read Mail: %d\n\n" %(len(new_messages)))
return log_messages
crm_email_gateway()
mail_gateway()
class crm_case_categ(osv.osv):
_name = "crm.case.categ"
@ -1040,19 +907,6 @@ class crm_case_history(osv.osv):
}
crm_case_history()
class crm_email_history(osv.osv):
_name = "crm.email.history"
_description = "Email History"
_columns = {
'name': fields.char('Message Id', size=64, help="Message Id in Email Server."),
'case_id': fields.many2one('crm.case',"Case"),
'gateway_id': fields.many2one('crm.email.gateway',"Email Gateway", required=True),
'note': fields.text('Notes'),
}
_order = 'id desc'
crm_email_history()
class crm_email_add_cc_wizard(osv.osv_memory):
_name = "crm.email.add.cc"
_description = "Email Add CC"

View File

@ -42,8 +42,8 @@
<field name="allow_unlink" select="2"/>
<field name="calendar" select="2"/>
</group>
<field name="reply_to" select="2"/>
<field name="gateway_ids" widget="one2many_list" nolabel="1" colspan="4">
<field name="reply_to" select="2"/>
<field name="gateway_ids" widget="one2many_list" nolabel="1" colspan="4">
<tree string="Email Gateway" editable="bottom">
<field name="name" />
<field name="server_id" />
@ -56,12 +56,10 @@
<field name="mail_history" widget="one2many_list" nolabel="1" colspan="4">
<tree string="Email History">
<field name="name"/>
<field name="case_id"/>
<field name="note"/>
</tree>
<form string="Email History">
<field name="name"/>
<field name="case_id"/>
<separator colspan="4" string="Notes"/>
<field name="note" nolabel="1" colspan="4"/>
</form>
@ -649,89 +647,5 @@
<act_window domain="[('user_id', '=', active_id),('state','&lt;&gt;','done'),('state','&lt;&gt;','cancel'),('state','&lt;&gt;','pending')]" id="act_res_users_2_crm_case_opened" name="Open cases" res_model="crm.case" src_model="res.users" view_mode="tree,form,calendar" view_type="form"/>
<record id="crm_email_gateway_form" model="ir.ui.view">
<field name="name">crm.email.gateway.form</field>
<field name="model">crm.email.gateway</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Email Gateway">
<field name="name"/>
<field name="section_id" />
<field name="server_id" />
<field name="to_email_id"/>
<field name="cc_email_id" />
<field name="mail_history" widget="one2many_list" nolabel="1" colspan="4">
<tree string="Email History">
<field name="name"/>
<field name="case_id"/>
<field name="note"/>
</tree>
<form string="Email History">
<field name="name"/>
<field name="case_id"/>
<separator colspan="4" string="Notes"/>
<field name="note" nolabel="1" colspan="4"/>
</form>
</field>
</form>
</field>
</record>
<record id="crm_email_gateway_tree" model="ir.ui.view">
<field name="name">crm.email.gateway.tree</field>
<field name="model">crm.email.gateway</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree string="Email Gateway">
<field name="name"/>
<field name="section_id" />
<field name="server_id" />
</tree>
</field>
</record>
<record id="crm_email_gateway_server_form" model="ir.ui.view">
<field name="name">crm.email.gateway.server.form</field>
<field name="model">crm.email.gateway.server</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Email Gateway Server">
<field name="server_type" colspan="4" on_change="onchange_server_type(server_type,ssl)"/>
<field name="name"/>
<field name="port" />
<field name="login" />
<field name="password" password="True"/>
<field name="ssl" on_change="onchange_server_type(server_type,ssl)"/>
<field name="active" />
</form>
</field>
</record>
<record id="crm_email_gateway_server_tree" model="ir.ui.view">
<field name="name">crm.email.gateway.server.tree</field>
<field name="model">crm.email.gateway.server</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree string="Email Gateway Server">
<field name="name"/>
<field name="port" />
<field name="server_type"/>
<field name="ssl" />
</tree>
</field>
</record>
<record id="crm_email_gateway_act" model="ir.actions.act_window">
<field name="name">Email Gateway</field>
<field name="res_model">crm.email.gateway</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="crm_email_gateway_tree"/>
</record>
<menuitem id="crm_email_gateway_menu" name="Email Gateway" parent="next_id_51" action="crm_email_gateway_act" />
</data>
</openerp>

View File

@ -8,11 +8,5 @@
name="crm.case.section.fetchmail"
id="wizard_crm_case_section_fetchmail"/>
<wizard string="Fetch mail"
model="crm.email.gateway"
name="crm.case.mailgate.fetchmail"
id="wizard_crm_case_mailgateway_fetchmail"/>
</data>
</openerp>

View File

@ -13,12 +13,6 @@
"access_crm_case_rule_manager","crm.case.rule.manager","model_crm_case_rule","crm.group_crm_manager",1,1,1,1
"access_crm_case_log_manager","crm.case.log manager","model_crm_case_log","crm.group_crm_manager",1,1,1,1
"access_crm_case_history_manager","crm.case.history manager","model_crm_case_history","crm.group_crm_manager",1,1,1,1
"access_crm_email_gateway_server_manager","crm.email.gateway.server","model_crm_email_gateway_server","crm.group_crm_manager",1,1,1,1
"access_crm_email_gateway_server_user","crm.email.gateway.server","model_crm_email_gateway_server","crm.group_crm_user",1,0,0,0
"access_crm_email_gateway_manager","crm.email.gateway","model_crm_email_gateway","crm.group_crm_manager",1,1,1,1
"access_crm_email_gateway_user","crm.email.gateway","model_crm_email_gateway","crm.group_crm_user",1,0,0,0
"access_crm_email_history_manager","crm.email.history","model_crm_email_history","crm.group_crm_manager",1,1,1,1
"access_crm_email_history_user","crm.email.history","model_crm_email_history","crm.group_crm_user",1,0,0,0
"access_crm_email_add_cc_manager","crm.email.add.cc","model_crm_email_add_cc","crm.group_crm_manager",1,1,1,1
"access_crm_email_add_cc_user","crm.email.add.cc","model_crm_email_add_cc","crm.group_crm_user",1,0,0,0
"access_crm_case_stage","crm.case.stage","model_crm_case_stage","crm.group_crm_user",1,0,0,0

1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
13 access_crm_case_rule_manager crm.case.rule.manager model_crm_case_rule crm.group_crm_manager 1 1 1 1
14 access_crm_case_log_manager crm.case.log manager model_crm_case_log crm.group_crm_manager 1 1 1 1
15 access_crm_case_history_manager crm.case.history manager model_crm_case_history crm.group_crm_manager 1 1 1 1
access_crm_email_gateway_server_manager crm.email.gateway.server model_crm_email_gateway_server crm.group_crm_manager 1 1 1 1
access_crm_email_gateway_server_user crm.email.gateway.server model_crm_email_gateway_server crm.group_crm_user 1 0 0 0
access_crm_email_gateway_manager crm.email.gateway model_crm_email_gateway crm.group_crm_manager 1 1 1 1
access_crm_email_gateway_user crm.email.gateway model_crm_email_gateway crm.group_crm_user 1 0 0 0
access_crm_email_history_manager crm.email.history model_crm_email_history crm.group_crm_manager 1 1 1 1
access_crm_email_history_user crm.email.history model_crm_email_history crm.group_crm_user 1 0 0 0
16 access_crm_email_add_cc_manager crm.email.add.cc model_crm_email_add_cc crm.group_crm_manager 1 1 1 1
17 access_crm_email_add_cc_user crm.email.add.cc model_crm_email_add_cc crm.group_crm_user 1 0 0 0
18 access_crm_case_stage crm.case.stage model_crm_case_stage crm.group_crm_user 1 0 0 0

View File

@ -50,23 +50,16 @@ def _default(self , cr, uid, data, context):
gateway_pool=pool.get('crm.case.section')
sections = gateway_pool.browse(cr, uid, data['ids'], context=context)
server = []
for section in sections:
for gateway in section.gateway_ids:
if gateway.server_id.active:
server.append(gateway.name or '%s (%s)'%(gateway.server_id.login, gateway.server_id.name) )
# for section in sections:
# for gateway in section.gateway_ids:
# if gateway.server_id.active:
# server.append(gateway.name or '%s (%s)'%(gateway.server_id.login, gateway.server_id.name) )
data['form']['server'] = '\n'.join(server)
return data['form']
def section_fetch_mail(self , cr, uid, data, context):
pool = pooler.get_pool(cr.dbname)
gateway_pool=pool.get('crm.email.gateway')
messages = gateway_pool.fetch_mails(cr, uid, ids=[], section_ids=data['ids'], context=context)
data['form']['message'] = '\n'.join(messages)
return data['form']
def mailgate_fetch_mail(self , cr, uid, data, context):
pool = pooler.get_pool(cr.dbname)
gateway_pool=pool.get('crm.email.gateway')
gateway_pool=pool.get('mail.gateway')
messages = gateway_pool.fetch_mails(cr, uid, ids=data['ids'], context=context)
data['form']['message'] = '\n'.join(messages)
return data['form']
@ -87,22 +80,6 @@ class wiz_section_fetch_mail(wizard.interface):
},
},
}
class wiz_mailgateway_fetch_mail(wizard.interface):
states = {
'init': {
'actions': [],
'result': {'type': 'form', 'arch':_email_form, 'fields':_email_fields, 'state':[('end','Cancel','gtk-cancel'), ('fetch','Fetch','gtk-execute')]}
},
'fetch': {
'actions': [mailgate_fetch_mail],
'result': {'type': 'form', 'arch': _email_done_form,
'fields': _email_done_fields,
'state': (
('end', 'Close'),
)
},
},
}
wiz_section_fetch_mail('crm.case.section.fetchmail')
wiz_mailgateway_fetch_mail('crm.case.mailgate.fetchmail')
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 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 mailgateway
import wizard
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 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/>.
#
##############################################################################
{
'name': 'EMail Gateway',
'version': '1.0',
'category': 'Generic Modules/Mail Gate',
'description': """The generic email gateway system for the synchronisation interface
between mails and Open ERP.
""",
'author': 'Tiny',
'website': 'http://www.openerp.com',
'depends': ['base', 'process'],
'init_xml': [],
'update_xml': [
'mailgateway_wizard.xml',
'mailgateway_view.xml',
'security/ir.model.access.csv',
],
'demo_xml': [],
'installable': True,
'active': False,
'certificate': None,
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,184 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 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 tools.translate import _
import tools
from osv import fields,osv,orm
from osv.orm import except_orm
from scripts.openerp_mailgate import openerp_mailgate
import email
import netsvc
from poplib import POP3, POP3_SSL
from imaplib import IMAP4, IMAP4_SSL
class mail_gateway_server(osv.osv):
_name = "mail.gateway.server"
_description = "Email Gateway Server"
_columns = {
'name': fields.char('Server Address',size=64,required=True ,help="IMAP/POP Address Of Email gateway Server"),
'login': fields.char('User',size=64,required=True,help="User Login Id of Email gateway"),
'password': fields.char('Password',size=64,required=True,help="User Password Of Email gateway"),
'server_type': fields.selection([("pop","POP"),("imap","Imap")],"Type of Server", required=True, help="Type of Email gateway Server"),
'port': fields.integer("Port" , help="Port Of Email gateway Server. If port is omitted, the standard POP3 port (110) is used for POP EMail Server and the standard IMAP4 port (143) is used for IMAP Sever."),
'ssl': fields.boolean('SSL',help ="Use Secure Authentication"),
'active': fields.boolean('Active', help="If the active field is set to true, it will allow you to hide the email gateway server without removing it."),
}
_defaults = {
'server_type':lambda * a:'pop',
'active':lambda * a:True,
}
def onchange_server_type(self, cr, uid, ids, server_type=False, ssl=False):
port = 0
if server_type == 'pop':
port = ssl and 995 or 110
elif server_type == 'imap':
port = ssl and 993 or 143
return {'value':{'port':port}}
mail_gateway_server()
class mail_gateway(osv.osv):
_name = "mail.gateway"
_description = "Email Gateway"
_columns = {
'name': fields.char('Name',size=64,help="Name of Mail Gateway."),
'server_id': fields.many2one('mail.gateway.server',"Gateway Server", required=True),
'to_email_id': fields.char('TO', size=64, help="Email address used in the From field of outgoing messages"),
'cc_email_id': fields.char('CC',size=64,help="Default eMail in case of any trouble."),
'mail_history': fields.one2many("mail.gateway.history","gateway_id","History", readonly=True)
}
def _fetch_mails(self, cr, uid, ids=False, context={}):
'''
Function called by the scheduler to fetch mails
'''
cr.execute('select * from mail_gateway gateway \
inner join mail_gateway_server server \
on server.id = gateway.server_id where server.active = True')
ids2 = map(lambda x: x[0], cr.fetchall() or [])
return self.fetch_mails(cr, uid, ids=ids2, context=context)
def parse_mail(self, cr, uid, gateway_id, email_message, email_parser=None, context={}):
msg_id = case_id = note = False
user_obj = self.pool.get('res.users')
mail_history_obj = self.pool.get('mail.gateway.history')
users = user_obj.read(cr, uid, uid, ['password'])
mailgateway = self.browse(cr, uid, gateway_id, context=context)
try :
if not email_parser:
email_parser = openerp_mailgate.email_parser(uid, users['password'],
mailgateway.to_email_id or '', mailgateway.cc_email_id or '', dbname=cr.dbname,
host=tools.config['interface'] or 'localhost', port=tools.config['port'] or '8069')
msg_txt = email.message_from_string(email_message)
msg_id = msg_txt['Message-ID']
res_id = email_parser.parse(msg_txt)[0]
res_model = False
except Exception, e:
note = "Error in Parsing Mail: %s " %(str(e))
netsvc.Logger().notifyChannel('Emailgate: Parsing mail:%s' % (mailgateway.name or
'%s (%s)'%(mailgateway.server_id.login, mailgateway.server_id.name)), netsvc.LOG_ERROR, str(e))
mail_history_obj.create(cr, uid, {'name':msg_id, 'res_id': res_id, 'res_model': res_model,'gateway_id':mailgateway.id, 'note':note})
return res_id, res_model, note
def fetch_mails(self, cr, uid, ids=[], context={}):
log_messages = []
for mailgateway in self.browse(cr, uid, ids):
try :
mailgate_server = mailgateway.server_id
if not mailgate_server.active:
continue
mailgate_name = mailgateway.name or "%s (%s)" % (mailgate_server.login, mailgate_server.name)
log_messages.append("Mail Server : %s" % mailgate_name)
log_messages.append("="*40)
new_messages = []
if mailgate_server.server_type == 'pop':
if mailgate_server.ssl:
pop_server = POP3_SSL(mailgate_server.name or 'localhost', mailgate_server.port or 995)
else:
pop_server = POP3(mailgate_server.name or 'localhost', mailgate_server.port or 110)
pop_server.user(mailgate_server.login)
pop_server.pass_(mailgate_server.password)
pop_server.list()
(numMsgs, totalSize) = pop_server.stat()
for i in range(1, numMsgs + 1):
(header, msges, octets) = pop_server.retr(i)
res_id, res_model, note = self.parse_mail(cr, uid, mailgateway.id, '\n'.join(msges))
log = ''
if res_id:
log = _('Object Successfully Created : %d of %s'% (res_id, res_model))
if note:
log = note
log_messages.append(log)
new_messages.append(i)
pop_server.quit()
elif mailgate_server.server_type == 'imap':
if mailgate_server.ssl:
imap_server = IMAP4_SSL(mailgate_server.name or 'localhost', mailgate_server.port or 993)
else:
imap_server = IMAP4(mailgate_server.name or 'localhost', mailgate_server.port or 143)
imap_server.login(mailgate_server.login, mailgate_server.password)
imap_server.select()
typ, data = imap_server.search(None, '(UNSEEN)')
for num in data[0].split():
typ, data = imap_server.fetch(num, '(RFC822)')
res_id, res_model, note = self.parse_mail(cr, uid, mailgateway.id, data[0][1])
log = ''
if res_id:
log = _('Object Successfully Created : %d of %s'% (res_id, res_model))
if note:
log = note
log_messages.append(log)
new_messages.append(num)
imap_server.close()
imap_server.logout()
except Exception, e:
log_messages.append("Error in Fetching Mail: %s " %(str(e)))
netsvc.Logger().notifyChannel('Emailgate: Fetching mail:[%d]%s' % (mailgate_server.id, mailgate_server.name), netsvc.LOG_ERROR, str(e))
log_messages.append("-"*25)
log_messages.append("Total Read Mail: %d\n\n" %(len(new_messages)))
return log_messages
mail_gateway()
class mail_gateway_history(osv.osv):
_name = "mail.gateway.history"
_description = "Mail Gateway History"
_columns = {
'name': fields.char('Message Id', size=64, help="Message Id in Email Server."),
'res_id': fields.integer("Resource ID"),
'res_model': fields.many2one('ir.model',"Model"),
'gateway_id': fields.many2one('mail.gateway',"Mail Gateway", required=True),
'note': fields.text('Notes'),
}
_order = 'id desc'
mail_gateway_history()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,87 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="mail_gateway_form" model="ir.ui.view">
<field name="name">mail.gateway.form</field>
<field name="model">mail.gateway</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Email Gateway">
<field name="name"/>
<field name="server_id" />
<field name="to_email_id"/>
<field name="cc_email_id" />
<field name="mail_history" widget="one2many_list" nolabel="1" colspan="4">
<tree string="Email History">
<field name="name"/>
<field name="res_id"/>
<field name="res_model"/>
<field name="note"/>
</tree>
<form string="Email History">
<field name="name"/>
<field name="res_id"/>
<field name="res_model"/>
<separator colspan="4" string="Notes"/>
<field name="note" nolabel="1" colspan="4"/>
</form>
</field>
</form>
</field>
</record>
<record id="mail_gateway_tree" model="ir.ui.view">
<field name="name">mail.gateway.tree</field>
<field name="model">mail.gateway</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree string="Email Gateway">
<field name="name"/>
<field name="server_id" />
</tree>
</field>
</record>
<record id="mail_gateway_server_form" model="ir.ui.view">
<field name="name">mail.gateway.server.form</field>
<field name="model">mail.gateway.server</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Email Gateway Server">
<field name="server_type" colspan="4" on_change="onchange_server_type(server_type,ssl)"/>
<field name="name"/>
<field name="port" />
<field name="login" />
<field name="password" password="True"/>
<field name="ssl" on_change="onchange_server_type(server_type,ssl)"/>
<field name="active" />
</form>
</field>
</record>
<record id="mail_gateway_server_tree" model="ir.ui.view">
<field name="name">mail.gateway.server.tree</field>
<field name="model">mail.gateway.server</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree string="Email Gateway Server">
<field name="name"/>
<field name="port" />
<field name="server_type"/>
<field name="ssl" />
</tree>
</field>
</record>
<record id="mail_gateway_act" model="ir.actions.act_window">
<field name="name">Email Gateway</field>
<field name="res_model">mail.gateway</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="mail_gateway_tree"/>
</record>
<menuitem id="email_gateway_menu_parent" name="Email Gateway" parent="base.menu_config" />
<menuitem id="email_gateway_menu" parent="email_gateway_menu_parent" action="mail_gateway_act"/>
</data>
</openerp>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<wizard string="Fetch Mail"
model="mail.gateway"
name="mailgate.fetchmail"
id="wizard_mailgateway_fetchmail"/>
</data>
</openerp>

View File

@ -135,12 +135,8 @@ class rpc_proxy(object):
return self.rpc.execute(self.dbname, self.user_id, self.passwd, *request)
class email_parser(object):
def __init__(self, uid, password, section, email, email_default, dbname, host, port):
def __init__(self, uid, password, email, email_default, dbname, host, port):
self.rpc = rpc_proxy(uid, password, host=host, port=port, dbname=dbname)
try:
self.section_id = int(section)
except:
self.section_id = self.rpc('crm.case.section', 'search', [('code','=',section)])[0]
self.email = email
self.email_default = email_default
self.canal_id = False
@ -169,7 +165,6 @@ class email_parser(object):
message = self.msg_body_get(msg)
data = {
'name': self._decode_header(msg['Subject']),
'section_id': self.section_id,
'email_from': self._decode_header(msg['From']),
'email_cc': self._decode_header(msg['Cc'] or ''),
'canal_id': self.canal_id,
@ -205,16 +200,6 @@ class email_parser(object):
return id
# #change the return type format to dictionary
# {
# 'body':'body part',
# 'attachment':{
# 'file_name':'file data',
# 'file_name':'file data',
# 'file_name':'file data',
# }
# }
# #
def msg_body_get(self, msg):
message = {};
message['body'] = '';

View File

@ -0,0 +1,4 @@
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink

View File

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 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 wizard_fetch_mail
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 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 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 wizard
import time
import pooler
import tools
import os
_email_form = '''<?xml version="1.0"?>
<form string="Email Gateway">
<separator string="Fetching Emails : " />
<field name="server" colspan="4" nolabel="1" />
</form>'''
_email_done_form = '''<?xml version="1.0"?>
<form string="Email Gateway">
<separator string="Log Detail" />
<newline/>
<field name="message" colspan="4" nolabel="1"/>
</form>'''
_email_fields = {
'server': {'string':"Server", 'type':'text', 'readonly':True},
}
_email_done_fields = {
'message': {'string':"Log Detail", 'type':'text', 'readonly':True},
}
def mailgate_fetch_mail(self , cr, uid, data, context):
pool = pooler.get_pool(cr.dbname)
gateway_pool=pool.get('mail.gateway')
messages = gateway_pool.fetch_mails(cr, uid, ids=data['ids'], context=context)
data['form']['message'] = '\n'.join(messages)
return data['form']
class wiz_mailgateway_fetch_mail(wizard.interface):
states = {
'init': {
'actions': [],
'result': {'type': 'form', 'arch':_email_form, 'fields':_email_fields, 'state':[('end','Cancel','gtk-cancel'), ('fetch','Fetch','gtk-execute')]}
},
'fetch': {
'actions': [mailgate_fetch_mail],
'result': {'type': 'form', 'arch': _email_done_form,
'fields': _email_done_fields,
'state': (
('end', 'Close'),
)
},
},
}
wiz_mailgateway_fetch_mail('mailgate.fetchmail')
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: