1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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 ##############################################################################
24 from osv import osv, fields
25 from tools.translate import _
31 class crm_send_new_email_attachment(osv.osv_memory):
32 _name = 'crm.send.mail.attachment'
35 'binary' : fields.binary('Attachment', required=True),
36 'name' : fields.char('Name', size=128, required=True),
37 'wizard_id' : fields.many2one('crm.send.mail', 'Wizard', required=True),
40 crm_send_new_email_attachment()
42 class crm_send_new_email(osv.osv_memory):
43 """ Sends new email for the case"""
44 _name = "crm.send.mail"
45 _description = "Send new email"
48 'email_to' : fields.char('To', size=512, required=True),
49 'email_from' : fields.char('From', size=128, required=True),
50 'reply_to' : fields.char('Reply To', size=128, required=True, help="Reply-to of the Sales team defined on this case"),
51 'email_cc' : fields.char('CC', size=512, help="Carbon Copy: list of recipients that will receive a copy of this mail"),
52 'subject': fields.char('Subject', size=512, required=True),
53 'body': fields.text('Message Body', required=True),
54 'state': fields.selection(crm.AVAILABLE_STATES, string='Set New State To', required=True),
55 'attachment_ids' : fields.one2many('crm.send.mail.attachment', 'wizard_id'),
58 def action_send(self, cr, uid, ids, context=None):
59 """ This sends an email to ALL the addresses of the selected partners.
61 hist_obj = self.pool.get('mailgate.message')
66 if not context.get('active_model'):
67 raise osv.except_osv(_('Error'), _('Can not send mail!'))
69 model = context.get('active_model')
70 case_pool = self.pool.get(model)
71 res_id = context and context.get('active_id', False) or False
73 for obj in self.browse(cr, uid, ids, context=context):
75 (x.name, base64.decodestring(x.binary)) for x in obj.attachment_ids
81 case = case_pool.browse(cr, uid, res_id, context=context)
82 if context.get('mail', 'new') == 'new':
84 message_id = case.message_ids[0].message_id
85 elif context.get('mail') == 'forward':
86 # extract attachements from case and emails according to mode
88 attach_pool = self.pool.get('ir.attachment')
89 direct_attachments = attach_pool.search(cr, uid, [('res_model', '=', 'crm.lead'), ('res_id', '=', res_id)], context=context)
90 attachments += attach_pool.browse(cr, uid, direct_attachments, context=context)
91 if obj.history in ['latest', 'whole'] and case.message_ids:
92 msgs = case.message_ids
93 if obj.history == 'latest':
95 attachments.extend(itertools.chain(*[m.attachment_ids for m in msgs]))
96 attach_all = [(a.datas_fname or a.name, base64.decodestring(a.datas)) for a in attachments if a.datas]
100 hist = hist_obj.browse(cr, uid, res_id, context=context)
101 message_id = hist.message_id
103 case_pool = self.pool.get(model)
106 case = case_pool.browse(cr, uid, res_id, context=context)
107 emails = [obj.email_to]
108 email_cc = re.findall(r'([^ ,<@]+@[^> ,]+)', obj.email_cc)
109 emails = filter(None, emails)
112 body = body and tools.ustr(body) or ''
113 email_from = getattr(obj, 'email_from', False)
116 x_headers['References'] = "%s" % (message_id)
118 flag = tools.email_send(
125 reply_to=obj.reply_to,
126 openobject_id=str(case.id),
131 raise osv.except_osv(_('Error!'), _('Unable to send mail. Please check SMTP is configured properly.'))
133 msg_dict = {'new': 'Send', 'reply': 'Reply', 'forward': 'Forward'}
134 case_pool.history(cr, uid, [case], _(msg_dict[context.get('mail', 'new')]), history=True, \
135 email=obj.email_to, details=body, \
136 subject=obj.subject, email_from=email_from, \
137 email_cc=', '.join(email_cc), message_id=message_id, \
138 references=ref_id or message_id, attach=attach)
139 if obj.state == 'unchanged':
141 elif obj.state == 'done':
142 case_pool.case_close(cr, uid, [case.id])
143 elif obj.state == 'draft':
144 case_pool.case_reset(cr, uid, [case.id])
145 elif obj.state in ['cancel', 'open', 'pending']:
146 act = 'case_' + obj.state
147 getattr(case_pool, act)(cr, uid, [case.id])
151 def default_get(self, cr, uid, fields, context=None):
153 This function gets default values
158 if not context.get('active_model'):
159 raise osv.except_osv(_('Error'), _('Can not send mail!'))
161 res = super(crm_send_new_email, self).default_get(cr, uid, fields, context=context)
163 if context.get('mail') == 'reply':
164 res.update(self.get_reply_defaults(cr, uid, fields, context=context))
167 model = context.get('active_model')
168 mod_obj = self.pool.get(model)
169 res_id = context and context.get('active_ids', []) or []
171 user_obj = self.pool.get('res.users')
172 user_mail_from = user_obj._get_email_from(cr, uid, [uid], context=context)[uid]
174 for case in mod_obj.browse(cr, uid, res_id):
175 if 'email_to' in fields:
176 res.update({'email_to': case.email_from and tools.ustr(case.email_from) or ''})
177 if 'email_from' in fields:
178 res.update({'email_from': user_mail_from and tools.ustr(user_mail_from) or ''})
179 if 'reply_to' in fields:
180 res.update({'reply_to': case.section_id.reply_to})
181 if 'subject' in fields:
182 res.update({'subject': tools.ustr(context.get('subject', case.name) or '')})
183 if 'email_cc' in fields:
184 res.update({'email_cc': tools.ustr(case.email_cc or '')})
186 res.update({'body': u'\n'+(tools.ustr(case.user_id.signature or ''))})
187 if 'state' in fields:
188 res.update({'state': u'pending'})
192 def get_reply_defaults(self, cr, uid, fields, context=None):
194 This function gets default values for reply mail
196 hist_obj = self.pool.get('mailgate.message')
197 res_ids = context and context.get('active_ids', []) or []
199 user_obj = self.pool.get('res.users')
200 user_mail_from = user_obj._get_email_from(cr, uid, [uid], context=context)[uid]
202 include_original = context and context.get('include_original', False) or False
204 for hist in hist_obj.browse(cr, uid, res_ids, context=context):
207 # In the case where the crm.case does not exist in the database
211 model_pool = self.pool.get(model)
213 case = model_pool.browse(cr, uid, res_id)
214 if 'email_to' in fields:
215 res.update({'email_to': case.email_from and tools.ustr(case.email_from) or False})
216 if 'email_from' in fields:
217 res.update({'email_from': user_mail_from and tools.ustr(user_mail_from) or False})
219 signature = u'\n' + (tools.ustr(case.user_id.signature or '')) + u'\n'
220 original = [signature]
222 if include_original == True and 'body' in fields:
223 header = u'-------- Original Message --------'
224 sender = u'From: %s' %(tools.ustr(hist.email_from or ''))
225 to = u'To: %s' % (tools.ustr(hist.email_to or ''))
226 sentdate = u'Date: %s' % (tools.ustr(hist.date))
227 desc = u'\n%s'%(tools.ustr(hist.description))
229 original = [signature, header, sender, to, sentdate, desc]
231 res['body']= u'\n' + u'\n'.join(original)
233 if 'subject' in fields:
234 res.update({u'subject': u'Re: %s' %(tools.ustr(hist.name or ''))})
235 if 'email_cc' in fields:
236 res.update({'email_cc': case.email_cc and tools.ustr(case.email_cc) or False})
237 if 'reply_to' in fields:
238 res.update({'reply_to': case.section_id.reply_to})
239 if 'state' in fields:
240 res['state'] = u'pending'
243 def view_init(self, cr, uid, fields_list, context=None):
245 This function checks for precondition before wizard executes
246 @param self: The object pointer
247 @param cr: the current row, from the database cursor,
248 @param uid: the current user’s ID for security checks,
249 @param fields: List of fields for default value
250 @param context: A standard dictionary for contextual values
256 if not context.get('active_model'):
257 raise osv.except_osv(_('Error'), _('Can not send mail!'))