[IMP] improve email, email_gateway and email_template module.

bzr revid: ysa@tinyerp.com-20110202084029-do4g3euykt5k79gd
This commit is contained in:
YSA (Openerp) 2011-02-02 14:10:29 +05:30
parent 26c2523134
commit 052a059029
16 changed files with 941 additions and 980 deletions

View File

@ -19,9 +19,9 @@
#
##############################################################################
import emails
import res_partner
import email_smtp_server
import email_message
import res_partner
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -31,11 +31,12 @@
'depends': ['base', 'base_tools'],
'init_xml': [],
'update_xml': [
"email_view.xml",
"email_message_view.xml",
"res_partner_view.xml",
'email_smtp_server_view.xml',
'security/ir.model.access.csv',
'email_smtp_server_data.xml'
'email_smtp_server_data.xml',
'email_gateway_scheduler_data.xml',
],
'demo_xml': [],

View File

@ -0,0 +1,267 @@
# -*- 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.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')
def format_date_tz(date, tz=None):
if not date:
return 'n/a'
format = tools.DEFAULT_SERVER_DATETIME_FORMAT
return tools.server_to_local_timestamp(date, format, format, tz)
class email_message(osv.osv):
'''
Email Message
'''
_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.
@param cr: A database cursor
@param uid: ID of the user currently logged in
@param ids: the ID of messages
@param context: A standard dictionary
"""
action_data = False
if ids:
message_id = ids[0]
mailgate_data = self.browse(cr, uid, message_id, context=context)
model = mailgate_data.model
res_id = mailgate_data.res_id
action_pool = self.pool.get('ir.actions.act_window')
action_ids = action_pool.search(cr, uid, [('res_model', '=', model)])
if action_ids:
action_data = action_pool.read(cr, uid, action_ids[0], context=context)
action_data.update({
'domain' : "[('id','=',%d)]"%(res_id),
'nodestroy': True,
'context': {}
})
return action_data
def open_attachment(self, cr, uid, ids, context=None):
""" To Open attachments
@param self: The object pointer.
@param cr: A database cursor
@param uid: ID of the user currently logged in
@param ids: the ID of messages
@param context: A standard dictionary
"""
action_data = False
action_pool = self.pool.get('ir.actions.act_window')
message_pool = self.browse(cr ,uid, ids, context=context)[0]
att_ids = [x.id for x in message_pool.attachment_ids]
action_ids = action_pool.search(cr, uid, [('res_model', '=', 'ir.attachment')])
if action_ids:
action_data = action_pool.read(cr, uid, action_ids[0], context=context)
action_data.update({
'domain': [('id','in',att_ids)],
'nodestroy': True
})
return action_data
def truncate_data(self, cr, uid, data, context=None):
data_list = data and data.split('\n') or []
if len(data_list) > 3:
res = '\n\t'.join(data_list[:3]) + '...'
else:
res = '\n\t'.join(data_list)
return res
def _get_display_text(self, cr, uid, ids, name, arg, context=None):
if context is None:
context = {}
tz = context.get('tz')
result = {}
for message in self.browse(cr, uid, ids, context=context):
msg_txt = ''
if message.history:
msg_txt += (message.email_from or '/') + _(' wrote on ') + format_date_tz(message.date, tz) + ':\n\t'
if message.description:
msg_txt += self.truncate_data(cr, uid, message.description, context=context)
else:
msg_txt = (message.user_id.name or '/') + _(' on ') + format_date_tz(message.date, tz) + ':\n\t'
if message.name == _('Opportunity'):
msg_txt += _("Converted to Opportunity")
elif message.name == _('Note'):
msg_txt = (message.user_id.name or '/') + _(' added note on ') + format_date_tz(message.date, tz) + ':\n\t'
msg_txt += self.truncate_data(cr, uid, message.description, context=context)
elif message.name == _('Stage'):
msg_txt += _("Changed Stage to: ") + message.description
else:
msg_txt += _("Changed Status to: ") + message.name
result[message.id] = msg_txt
return result
_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),
'display_text': fields.function(_get_display_text, method=True, type='text', size="512", string='Display Text'),
'reply_to':fields.char('Reply-To', size=250, readonly=True),
'account_id' :fields.many2one('email.smtp_server', 'User account', required=True, readonly=True),
#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, readonly=True),
'state':fields.selection([
('na', 'Not Applicable'),
('sending', 'Sending'),
('wait', 'Waiting'),
], 'Status', required=True, readonly=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)
def init(self, cr):
cr.execute("""SELECT indexname
FROM pg_indexes
WHERE indexname = 'email_message_res_id_model_idx'""")
if not cr.fetchone():
cr.execute("""CREATE INDEX email_message_res_id_model_idx
ON email_message (model, res_id)""")
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.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_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)
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:

View File

@ -0,0 +1,128 @@
<?xml version="1.0"?>
<openerp>
<data>
<menuitem id="base.menu_mail_gateway" name="Emails"
parent="base.menu_base_config" sequence="1" />
<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" widget="char" string="Subject" required="1"/>
<field name="date" required="1"/>
<field name="user_id" string="Owner"/>
<field name="partner_id" readonly="1" />
</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"/>
<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"/>
<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" />
</page>
<page string="Attachments">
<separator string="Attachments" colspan="4"/>
<field name="attachment_ids" nolabel="1" colspan="4" readonly="1"/>
</page>
<page string="Advanced">
<field name="account_id" colspan="2" />
<field name="folder" colspan="2" select="2"/>
<field name="state"/>
</page>
</notebook>
</form>
</field>
</record>
<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"/>
<field name="message_id" string="Message" invisible="1"/>
<field name="partner_id" invisible="1"/>
<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_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"/>
<field name="partner_id" string="Partner Name"/>
<newline/>
<group expand="0" string="Group By..." groups="base.group_extended">
<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'}"/>
</group>
</search>
</field>
</record>
<record id="action_view_mail_message" model="ir.actions.act_window">
<field name="name">Messages</field>
<field name="res_model">email.message</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</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="email.message"
src_model="res.partner"
view_id="view_email_message_tree"/>
<act_window
id="act_res_partner_open_email" name="Attachments"
res_model="ir.attachment"
src_model="email.message"
domain="[('res_id', '=', res_id),('res_model','=',model)]"/>
<menuitem name="Email Template" id="menu_email_template_tools"
parent="base.menu_tools" />
<menuitem name="Emails"
id="menu_email_message"
parent="menu_email_template_tools"
action="action_view_mail_message" />
</data>
</openerp>

View File

@ -243,10 +243,6 @@ unless it is already specified in the From Email, e.g: John Doe <john@doe.com>",
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:
@ -261,16 +257,14 @@ unless it is already specified in the From Email, e.g: John Doe <john@doe.com>",
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':
if payload:
payload_part = MIMEMultipart(_subtype='mixed')
text_part = MIMEMultipart(_subtype=text_subtype)
text_part = MIMEMultipart(_subtype='mixed')
payload_part.attach(text_part)
else:
# otherwise a single multipart/mixed will do the whole job
payload_part = text_part = MIMEMultipart(_subtype=text_subtype)
payload_part = text_part = MIMEMultipart(_subtype='mixed')
if subject:
payload_part['Subject'] = subject
@ -301,16 +295,16 @@ unless it is already specified in the From Email, e.g: John Doe <john@doe.com>",
# 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'))
# 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:
@ -397,7 +391,7 @@ unless it is already specified in the From Email, e.g: John Doe <john@doe.com>",
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)
self.pool.get('email.message').send_all_mail(cr, uid, [], context=ctx)
return True
def decode_header_text(self, text):

View File

@ -1,96 +0,0 @@
<?xml version="1.0"?>
<openerp>
<data>
<menuitem id="base.menu_mail_gateway" name="Emails"
parent="base.menu_base_config" sequence="1" />
<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>
<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>

View File

@ -1,489 +0,0 @@
# -*- 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.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 users 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 users ID for security checks,
@param cases: a browse record list
@param keyword: Case action keyword e.g.: If case is closed "Close" keyword is used
@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:
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 or (hasattr(case, 'description') and case.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 = {
'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()
def format_date_tz(date, tz=None):
if not date:
return 'n/a'
format = tools.DEFAULT_SERVER_DATETIME_FORMAT
return tools.server_to_local_timestamp(date, format, format, tz)
class mailgate_tool(osv.osv_memory):
_name = 'email.server.tools'
_description = "Email Server Tools"
def _decode_header(self, text):
"""Returns unicode() string conversion of the the given encoded smtp header"""
if text:
text = decode_header(text.replace('\r', ''))
return ''.join([tools.ustr(x[0], x[1]) for x in text])
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 users 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 users 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
# 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)
message_id = msg_txt.get('message-id', False)
msg = {}
if not message_id:
# 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)
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)
if 'text/html' in msg_txt.get('Content-Type', ''):
body = tools.html2plaintext(body)
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
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)
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 users 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()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -25,7 +25,7 @@ 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': fields.one2many('email.message', 'partner_id',\
'Emails', readonly=True, domain=[('history','=',True)]),
}

View File

@ -1,2 +1 @@
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
"mail_gateway_mailgate_thread","mail_gateway.mailgate.thread","model_mailgate_thread","base.group_system",1,1,1,1

1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
mail_gateway_mailgate_thread mail_gateway.mailgate.thread model_mailgate_thread base.group_system 1 1 1 1

View File

@ -32,12 +32,10 @@
'init_xml': [],
'update_xml': [
"email_gateway_view.xml",
'security/ir.model.access.csv'
'email_gateway_scheduler_data.xml',
'security/ir.model.access.csv',
],
'demo_xml': [],
'installable': True,
'active': False,
'certificate': '001056784984222247309',
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -34,229 +34,456 @@ import xmlrpclib
_logger = logging.getLogger('mailgate')
class mailgate_thread(osv.osv):
'''
Mailgateway Thread
'''
_name = 'mailgate.thread'
_description = 'Mailgateway 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 users 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 users ID for security checks,
@param cases: a browse record list
@param keyword: Case action keyword e.g.: If case is closed "Close" keyword is used
@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('email.message')
for case in cases:
attachments = []
for att in attach:
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 or (hasattr(case, 'description') and case.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 = {
'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()
def format_date_tz(date, tz=None):
if not date:
return 'n/a'
format = tools.DEFAULT_SERVER_DATETIME_FORMAT
return tools.server_to_local_timestamp(date, format, format, tz)
class email_message(osv.osv):
'''
Email Message
'''
_name = 'email.message'
_description = 'Email Message'
_order = 'date desc'
class mailgate_tool(osv.osv_memory):
def open_document(self, cr, uid, ids, context=None):
""" To Open Document
@param self: The object pointer.
@param cr: A database cursor
@param uid: ID of the user currently logged in
@param ids: the ID of messages
@param context: A standard dictionary
_name = 'email.server.tools'
_description = "Email Server Tools"
def _decode_header(self, text):
"""Returns unicode() string conversion of the the given encoded smtp header"""
if text:
text = decode_header(text.replace('\r', ''))
return ''.join([tools.ustr(x[0], x[1]) for x in text])
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 users 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
"""
action_data = False
if ids:
message_id = ids[0]
mailgate_data = self.browse(cr, uid, message_id, context=context)
model = mailgate_data.model
res_id = mailgate_data.res_id
if isinstance(res_ids, (int, long)):
res_ids = [res_ids]
action_pool = self.pool.get('ir.actions.act_window')
action_ids = action_pool.search(cr, uid, [('res_model', '=', model)])
if action_ids:
action_data = action_pool.read(cr, uid, action_ids[0], context=context)
action_data.update({
'domain' : "[('id','=',%d)]"%(res_id),
'nodestroy': True,
'context': {}
})
return action_data
def open_attachment(self, cr, uid, ids, context=None):
""" To Open attachments
@param self: The object pointer.
@param cr: A database cursor
@param uid: ID of the user currently logged in
@param ids: the ID of messages
@param context: A standard dictionary
"""
action_data = False
action_pool = self.pool.get('ir.actions.act_window')
message_pool = self.browse(cr ,uid, ids, context=context)[0]
att_ids = [x.id for x in message_pool.attachment_ids]
action_ids = action_pool.search(cr, uid, [('res_model', '=', 'ir.attachment')])
if action_ids:
action_data = action_pool.read(cr, uid, action_ids[0], context=context)
action_data.update({
'domain': [('id','in',att_ids)],
'nodestroy': True
})
return action_data
def truncate_data(self, cr, uid, data, context=None):
data_list = data and data.split('\n') or []
if len(data_list) > 3:
res = '\n\t'.join(data_list[:3]) + '...'
else:
res = '\n\t'.join(data_list)
return res
def _get_display_text(self, cr, uid, ids, name, arg, context=None):
if context is None:
context = {}
tz = context.get('tz')
result = {}
for message in self.browse(cr, uid, ids, context=context):
msg_txt = ''
if message.history:
msg_txt += (message.email_from or '/') + _(' wrote on ') + format_date_tz(message.date, tz) + ':\n\t'
if message.description:
msg_txt += self.truncate_data(cr, uid, message.description, context=context)
else:
msg_txt = (message.user_id.name or '/') + _(' on ') + format_date_tz(message.date, tz) + ':\n\t'
if message.name == _('Opportunity'):
msg_txt += _("Converted to Opportunity")
elif message.name == _('Note'):
msg_txt = (message.user_id.name or '/') + _(' added note on ') + format_date_tz(message.date, tz) + ':\n\t'
msg_txt += self.truncate_data(cr, uid, message.description, context=context)
elif message.name == _('Stage'):
msg_txt += _("Changed Stage to: ") + message.description
else:
msg_txt += _("Changed Status to: ") + message.name
result[message.id] = msg_txt
return result
_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),
'display_text': fields.function(_get_display_text, method=True, type='text', size="512", string='Display Text'),
'reply_to':fields.char('Reply-To', size=250, readonly=True),
'account_id' :fields.many2one('email.smtp_server', 'User account', required=True, readonly=True),
#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, readonly=True),
'state':fields.selection([
('na', 'Not Applicable'),
('sending', 'Sending'),
('wait', 'Waiting'),
], 'Status', required=True, readonly=True),
}
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)
def init(self, cr):
cr.execute("""SELECT indexname
FROM pg_indexes
WHERE indexname = 'email_message_res_id_model_idx'""")
if not cr.fetchone():
cr.execute("""CREATE INDEX email_message_res_id_model_idx
ON email_message (model, res_id)""")
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)
msg_pool = self.pool.get('email.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 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_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)
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 users 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
# 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)
message_id = msg_txt.get('message-id', False)
msg = {}
if not message_id:
# 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)
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)
if 'text/html' in msg_txt.get('Content-Type', ''):
body = tools.html2plaintext(body)
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:
self.write(cr, uid, id, {'folder':'sent', 'state':'na', 'date_mail':time.strftime("%Y-%m-%d %H:%M:%S")}, context)
res = part.get_payload(decode=True)
body += tools.ustr(res, encoding)
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)
else:
error = result['error_msg']
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)
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
if not len(res_ids):
new_res_id, attachment_ids = create_record(msg)
res_ids = [new_res_id]
email_message()
# 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 users 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()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -2,123 +2,92 @@
<openerp>
<data>
<record model="ir.ui.view" id="view_email_message_form">
<field name="name">email.message.form</field>
<field name="model">email.message</field>
<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="Email message">
<group colspan="4" col="6">
<field name="name" widget="char" string="Subject" required="1"/>
<field name="date" required="1"/>
<field name="user_id" string="Owner"/>
<field name="partner_id" readonly="1" />
</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"/>
<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"/>
<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" />
</page>
<page string="Attachments">
<separator string="Attachments" colspan="4"/>
<field name="attachment_ids" nolabel="1" colspan="4" readonly="1"/>
</page>
<page string="Advanced">
<field name="account_id" colspan="2" />
<field name="folder" colspan="2" select="2"/>
<field name="state"/>
</page>
</notebook>
<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_email_message_tree">
<field name="name">email.message.tree</field>
<field name="model">email.message</field>
<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="Emails">
<field name="date" />
<field name="name" string="Subject"/>
<field name="email_from" string="From"/>
<field name="user_id" string="Owner"/>
<field name="message_id" string="Message" invisible="1"/>
<field name="partner_id" invisible="1"/>
<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 string="Mailgateway Thread">
<field name="message_ids" />
</tree>
</field>
</record>
<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"/>
<field name="partner_id" string="Partner Name"/>
<newline/>
<group expand="0" string="Group By..." groups="base.group_extended">
<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'}"/>
</group>
</search>
</field>
</record>
<record id="action_view_mail_message" model="ir.actions.act_window">
<field name="name">Messages</field>
<field name="res_model">email.message</field>
<field name="view_type">form</field>
<!-- 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="search_view_id" ref="view_email_message_search"/>
<field name="view_type">form</field>
<field name="view_id" ref="view_mailgate_thread_tree"/>
</record>
<act_window domain="[('partner_id', '=', active_id), ('history', '=', True)]"
id="act_res_partner_emails" name="Emails"
res_model="email.message"
src_model="res.partner"
view_id="view_email_message_tree"/>
<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>
<act_window
id="act_res_partner_open_email" name="Attachments"
res_model="ir.attachment"
src_model="email.message"
domain="[('res_id', '=', res_id),('res_model','=',model)]"/>
<menuitem name="Email Template" id="menu_email_template_tools"
parent="base.menu_tools" />
<menuitem name="Emails"
id="menu_email_message"
parent="menu_email_template_tools"
action="action_view_mail_message" />
<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>

View File

@ -70,11 +70,6 @@ def get_value(cursor, user, recid, message=None, template=None, context=None):
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)
@ -97,9 +92,7 @@ class email_template(osv.osv):
['model'], context)['model']
else:
mod_name = False
return {
'value':{'model_int_name':mod_name}
}
return {'value':{'model_int_name':mod_name}}
_columns = {
'name' : fields.char('Name', size=100, required=True),
@ -245,7 +238,6 @@ This is useful for CRM leads for example"),
_defaults = {
'template_language' : lambda *a:'mako',
}
_sql_constraints = [
@ -322,7 +314,6 @@ This is useful for CRM leads for example"),
@param template_language: name of template engine
@return: computed expression
"""
expression = ''
if template_language == 'mako':
if field_name:
@ -332,14 +323,6 @@ This is useful for CRM leads for example"),
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 += "}}"
return expression
def onchange_model_object_field(self, cr, uid, ids, model_object_field, template_language, context=None):
@ -431,7 +414,6 @@ This is useful for CRM leads for example"),
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')
@ -440,7 +422,7 @@ This is useful for CRM leads for example"),
'datas': data,
'datas_fname': filename,
'description': name or _('No Description'),
'res_model':'email_template.mailbox',
'res_model':'email.message',
'res_id': mailbox_id,
}
attachment_id = attachment_obj.create(cursor,
@ -448,7 +430,7 @@ This is useful for CRM leads for example"),
attachment_data,
context)
if attachment_id:
self.pool.get('email_template.mailbox').write(
self.pool.get('email.message').write(
cursor,
user,
mailbox_id,
@ -458,13 +440,7 @@ This is useful for CRM leads for example"),
},
context)
def generate_attach_reports(self,
cursor,
user,
template,
record_id,
mail,
context=None):
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.
@ -509,12 +485,7 @@ This is useful for CRM leads for example"),
return True
def _generate_mailbox_item_from_template(self,
cursor,
user,
template,
record_id,
context=None):
def _generate_mailbox_item_from_template(self, cursor, user, template, record_id, context=None):
"""
Generates an email from the template for
record record_id of target object
@ -640,7 +611,7 @@ This is useful for CRM leads for example"),
mailbox_values['body_text'] += sign
if mailbox_values['body_html']:
mailbox_values['body_html'] += sign
mailbox_id = self.pool.get('email_template.mailbox').create(
mailbox_id = self.pool.get('email.message').create(
cursor,
user,
mailbox_values,
@ -649,19 +620,14 @@ This is useful for CRM leads for example"),
return mailbox_id
def generate_mail(self,
cursor,
user,
template_id,
record_ids,
context=None):
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')
mailbox_obj = self.pool.get('email.message')
for record_id in record_ids:
mailbox_id = self._generate_mailbox_item_from_template(
cursor,
@ -684,8 +650,7 @@ This is useful for CRM leads for example"),
mail,
context
)
self.pool.get('email_template.mailbox').write(
self.pool.get('email.message').write(
cursor,
user,
mailbox_id,
@ -693,12 +658,9 @@ This is useful for CRM leads for example"),
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)
result = self.pool.get('email.message').send_this_mail(cursor, user, [mailbox_id], context)
return result
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!
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -23,6 +23,7 @@
from osv import osv, fields
from tools.translate import _
from email_template.email_template import get_value
import tools
class email_template_preview(osv.osv_memory):

View File

@ -29,7 +29,6 @@ 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!
@ -171,14 +170,14 @@ class email_template_send_wizard(osv.osv_memory):
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):
if self.pool.get('email.message').write(cr, uid, mailid, {'folder':'drafts'}, context):
return {'type':'ir.actions.act_window_close' }
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):
if self.pool.get('email.message').write(cr, uid, mailid, {'folder':'outbox'}, context):
return {'type':'ir.actions.act_window_close' }
def get_generated(self, cr, uid, ids=None, context=None):
@ -191,7 +190,7 @@ class email_template_send_wizard(osv.osv_memory):
#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)
self.pool.get('email.message').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),
@ -211,9 +210,10 @@ class email_template_send_wizard(osv.osv_memory):
if context is None:
context = {}
mail_ids = []
email_message_obj = self.pool.get('email.message')
template = self._get_template(cr, uid, context)
for id in context['src_rec_ids']:
screen_vals = self.read(cr, uid, ids[0], [],context)
screen_vals = self.read(cr, uid, ids[0], [], context)
account = self.pool.get('email.smtp_server').read(cr, uid, screen_vals['from'], context=context)
vals = {
'email_from': tools.ustr(account['name']) + "<" + tools.ustr(account['email_id']) + ">",
@ -236,7 +236,7 @@ class email_template_send_wizard(osv.osv_memory):
attachment_ids = []
#Create partly the mail and later update attachments
mail_id = self.pool.get('email_template.mailbox').create(cr, uid, vals, context)
mail_id = email_message_obj.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']
@ -254,7 +254,7 @@ class email_template_send_wizard(osv.osv_memory):
'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_model': 'email.message',
'res_id': mail_id
}, context)
attachment_ids.append( attachment_id )
@ -262,13 +262,13 @@ class email_template_send_wizard(osv.osv_memory):
# 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_model': 'email.message',
'res_id': mail_id,
}, context)
attachment_ids.append( new_id )
if attachment_ids:
self.pool.get('email_template.mailbox').write(cr, uid, mail_id, {
email_message_obj.write(cr, uid, mail_id, {
'attachments_ids': [[6, 0, attachment_ids]],
'mail_type': 'multipart/mixed'
}, context)