1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2011 NovaPoint Group LLC (<http://www.novapointgroup.com>)
6
# Copyright (C) 2004-2010 OpenERP SA (<http://www.openerp.com>)
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>
21
##############################################################################
28
from tools.translate import _
29
from osv import osv, fields
31
LOGGER = netsvc.Logger()
33
class email_template_mailbox(osv.osv):
34
_inherit = "email_template.mailbox"
36
Inherit send_this_mail function to add email on partner history after sending email.
38
def send_this_mail(self, cr, uid, ids=None, context=None):
40
attachment_pool = self.pool.get('ir.attachment')
41
for id in (ids or []):
43
account_obj = self.pool.get('email_template.account')
44
values = self.read(cr, uid, id, [], context=context)
47
if values['attachments_ids']:
48
attach_to_send = self.pool.get('ir.attachment').read(cr, uid, values['attachments_ids'], ['datas_fname', 'datas', 'name'],
50
attach_to_send = map(lambda x: (x['datas_fname'] or x['name'], base64.decodestring(x['datas'])), attach_to_send)
52
if values.get('body_html'):
53
body = values.get('body_html')
56
body = values.get('body_text')
59
result = tools.email_send(
60
values.get('email_from') or u'',
61
[values.get('email_to')],
62
values['subject'] or u'',
64
reply_to=values.get('reply_to') or u'',
65
email_bcc=[values.get('email_bcc') or u''],
66
email_cc=[values.get('email_cc') or u''],
68
attach=attach_to_send,
69
openobject_id=values['message_id']
75
account = account_obj.browse(cr, uid, values['account_id'][0], context=context)
76
send_model_name = values.get('rel_model', False)
77
send_model_id = values.get('rel_model_ref', False)
78
template_id = values.get('ref_template', False)
80
send_model_name = self.pool.get('ir.model').browse(cr, uid, send_model_name[0], context=context).model
81
## Code to add sent mail details in partner history
84
historize = self.pool.get('email.template').read(cr, uid, template_id[0], ['add_to_history'],
85
context=context)['add_to_history']
86
if send_model_name and send_model_id and historize:
87
if send_model_name in ['account.voucher', 'sale.order', 'crm.lead', 'calendar.attendee',
88
'crm.phonecall', 'purchase.order', 'account.invoice', 'account.voucher']:
89
send_model_pool = self.pool.get(send_model_name)
91
send_model = send_model_pool.browse(cr, uid, send_model_id, context=context)
92
if send_model and send_model.partner_id and send_model.partner_id.id:
93
self.pool.get('mailgate.message').create(cr, uid, {
94
'partner_id': send_model.partner_id.id,
95
'name': values['subject'] or u'',
96
'model': send_model_name,
97
'res_id': send_model_id,
98
'date': time.strftime('%Y-%m-%d %H:%M:%S'),
101
'message': body or u'',
102
'email_from': values.get('email_from') or u'',
103
'email_to': values.get('email_to'),
104
'email_cc': values.get('email_cc') or u'',
105
'email_bcc': values.get('email_bcc') or u'',
106
'attachment_ids': [[6, 0, values['attachments_ids']]],
109
except (Exception), e:
110
logger = netsvc.Logger()
111
msg = "Adding email sent history in partner: " + str(e)
112
logger.notifyChannel("email_template_modification", netsvc.LOG_ERROR, msg)
113
if account.auto_delete:
114
self.write(cr, uid, id, {'folder': 'trash', 'state': 'sent', 'date_mail': time.strftime("%Y-%m-%d %H:%M:%S")}, context=context)
115
self.unlink(cr, uid, [id], context=context)
116
# Remove attachments for this mail
117
attachment_pool.unlink(cr, uid, values['attachments_ids'], context=context)
119
self.write(cr, uid, id, {'folder': 'sent', 'state': 'sent', 'date_mail': time.strftime("%Y-%m-%d %H:%M:%S")}, context=context)
120
self.historise(cr, uid, [id], "Email sent successfully", context=context)
122
self.write(cr, uid, id, {'state': 'exception', 'folder': 'outbox'}, context=context)
124
error = result['error_msg']
126
error = 'Unable to send mail. Please check SMTP is configured properly.'
127
self.historise(cr, uid, [id], error, context=context)
129
except Exception, error:
130
logger = netsvc.Logger()
131
logger.notifyChannel("email-template", netsvc.LOG_ERROR, _("Sending of Mail %s failed. Probable Reason:Could not login to server\n \
132
error: %s") % (id, error))
133
self.historise(cr, uid, [id], error, context=context)
137
'rel_model': fields.many2one('ir.model', 'Model', readonly=True),
138
'rel_model_ref': fields.integer('Referred Document', readonly=True),
139
'ref_template': fields.many2one('email.template', 'Template', readonly=True),
140
'state': fields.selection([
141
('sending', 'Sending'),
143
('sending','Sending'),
144
('exception', 'Exception'),
146
], 'Status', required=True),
153
email_template_mailbox()
154
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
b'\\ No newline at end of file'