1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2009 Sharoon Thomas
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 ##############################################################################
23 from osv import osv, fields
26 from tools.translate import _
30 LOGGER = netsvc.Logger()
32 class email_template_mailbox(osv.osv):
33 _name = "email_template.mailbox"
34 _description = 'Email Mailbox'
36 _order = "date_mail desc"
38 def run_mail_scheduler(self, cursor, user, context=None):
40 This method is called by OpenERP Scheduler
41 to periodically send emails
44 self.send_all_mail(cursor, user, context=context)
49 _("Error sending mail: %s") % e)
51 def send_all_mail(self, cr, uid, ids=None, context=None):
56 filters = [('folder', '=', 'outbox'), ('state', '!=', 'sending')]
57 if 'filters' in context.keys():
58 for each_filter in context['filters']:
59 filters.append(each_filter)
60 ids = self.search(cr, uid, filters, context=context)
61 self.write(cr, uid, ids, {'state':'sending'}, context)
62 self.send_this_mail(cr, uid, ids, context)
65 def send_this_mail(self, cr, uid, ids=None, context=None):
66 #previous method to send email (link with email account can be found at the revision 4172 and below
68 attachment_pool = self.pool.get('ir.attachment')
69 for id in (ids or []):
71 account_obj = self.pool.get('email_template.account')
72 values = self.read(cr, uid, id, [], context)
75 if values['attachments_ids']:
76 attach_to_send = self.pool.get('ir.attachment').read(cr, uid, values['attachments_ids'], ['datas_fname','datas', 'name'])
77 attach_to_send = map(lambda x: (x['datas_fname'] or x['name'], base64.decodestring(x['datas'])), attach_to_send)
79 if values.get('body_html'):
80 body = values.get('body_html')
83 body = values.get('body_text')
86 result = tools.email_send(
87 values.get('email_from') or u'',
88 [values.get('email_to')],
89 values['subject'] or u'',
91 reply_to=values.get('reply_to') or u'',
92 email_bcc=values.get('email_bcc') or u'',
93 email_cc=values.get('email_cc') or u'',
95 attach=attach_to_send,
96 openobject_id=values['message_id']
101 account = account_obj.browse(cr, uid, values['account_id'][0], context=context)
102 if account.auto_delete:
103 self.write(cr, uid, id, {'folder': 'trash'}, context=context)
104 self.unlink(cr, uid, [id], context=context)
105 # Remove attachments for this mail
106 attachment_pool.unlink(cr, uid, values['attachments_ids'], context=context)
108 self.write(cr, uid, id, {'folder':'sent', 'state':'na', 'date_mail':time.strftime("%Y-%m-%d %H:%M:%S")}, context)
109 self.historise(cr, uid, [id], "Email sent successfully", context)
111 error = result['error_msg']
112 self.historise(cr, uid, [id], error, context)
114 except Exception, error:
115 logger = netsvc.Logger()
116 logger.notifyChannel("email-template", netsvc.LOG_ERROR, _("Sending of Mail %s failed. Probable Reason:Could not login to server\nError: %s") % (id, error))
117 self.historise(cr, uid, [id], error, context)
118 self.write(cr, uid, id, {'state':'na'}, context)
121 def historise(self, cr, uid, ids, message='', context=None):
123 history = self.read(cr, uid, id, ['history'], context).get('history', '')
124 self.write(cr, uid, id, {'history': (history or '' )+ "\n" + time.strftime("%Y-%m-%d %H:%M:%S") + ": " + tools.ustr(message)}, context)
127 'email_from':fields.char(
130 'email_to':fields.char(
133 'email_cc':fields.char(
136 'email_bcc':fields.char(
139 'reply_to':fields.char(
142 'message_id':fields.char(
145 'subject':fields.char(
148 'body_text':fields.text(
149 'Standard Body (Text)'),
150 'body_html':fields.text(
151 'Body (Rich Text Clients Only)'),
152 'attachments_ids':fields.many2many(
154 'mail_attachments_rel',
158 'account_id' :fields.many2one(
159 'email_template.account',
162 'user':fields.related(
166 relation="res.users",
168 'server_ref':fields.integer(
169 'Server Reference of mail',
170 help="Applicable for inward items only"),
171 'mail_type':fields.selection([
174 ('multipart/alternative',
175 'Plain Text & HTML with no attachments'),
176 ('multipart/related',
177 'Intermixed content'),
183 #I like GMAIL which allows putting same mail in many folders
184 #Lets plan it for 0.9
185 'folder':fields.selection([
186 ('drafts', 'Drafts'),
187 ('outbox', 'Outbox'),
189 ('sent', 'Sent Items'),
190 ], 'Folder', required=True),
191 'state':fields.selection([
192 ('na', 'Not Applicable'),
193 ('sending', 'Sending'),
194 ], 'Status', required=True),
195 'date_mail':fields.datetime('Rec/Sent Date', help="Date on which Email Sent or Received"),
196 'history':fields.text(
203 'state': lambda * a: 'na',
204 'folder': lambda * a: 'outbox',
207 def unlink(self, cr, uid, ids, context=None):
209 It just changes the folder of the item to "Trash", if it is no in Trash folder yet,
210 or completely deletes it if it is already in Trash.
214 for mail in self.browse(cr, uid, ids, context=context):
215 if mail.folder == 'trash':
216 to_remove.append(mail.id)
218 to_update.append(mail.id)
219 # Changes the folder to trash
220 self.write(cr, uid, to_update, {'folder': 'trash'}, context=context)
221 return super(email_template_mailbox, self).unlink(cr, uid, to_remove, context=context)
223 email_template_mailbox()
225 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: