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 _
29 LOGGER = netsvc.Logger()
31 class email_template_mailbox(osv.osv):
32 _name = "email_template.mailbox"
33 _description = 'Email Mailbox'
35 _order = "date_mail desc"
37 def run_mail_scheduler(self, cursor, user, context=None):
39 This method is called by OpenERP Scheduler
40 to periodically send emails
43 self.send_all_mail(cursor, user, context=context)
48 _("Error sending mail: %s") % e)
50 def send_all_mail(self, cr, uid, ids=None, context=None):
55 filters = [('folder', '=', 'outbox'), ('state', '!=', 'sending')]
56 if 'filters' in context.keys():
57 for each_filter in context['filters']:
58 filters.append(each_filter)
59 ids = self.search(cr, uid, filters, context=context)
60 self.write(cr, uid, ids, {'state':'sending'}, context)
61 self.send_this_mail(cr, uid, ids, context)
64 def send_this_mail(self, cr, uid, ids=None, context=None):
66 attachment_pool = self.pool.get('ir.attachment')
67 for id in (ids or []):
69 account_obj = self.pool.get('email_template.account')
70 values = self.read(cr, uid, id, [], context)
72 if values['attachments_ids']:
73 for attid in values['attachments_ids']:
74 attachment = attachment_pool.browse(cr, uid, attid, context)#,['datas_fname','datas'])
75 payload[attachment.datas_fname] = attachment.datas
76 result = account_obj.send_mail(cr, uid,
77 [values['account_id'][0]],
78 {'To':values.get('email_to') or u'',
79 'CC':values.get('email_cc') or u'',
80 'BCC':values.get('email_bcc') or u'',
81 'Reply-To':values.get('reply_to') or u''},
82 values['subject'] or u'',
83 {'text':values.get('body_text') or u'', 'html':values.get('body_html') or u''},
85 message_id=values['message_id'],
88 account = account_obj.browse(cr, uid, values['account_id'][0], context=context)
89 if account.auto_delete:
90 self.write(cr, uid, id, {'folder': 'trash'}, context=context)
91 self.unlink(cr, uid, [id], context=context)
92 # Remove attachments for this mail
93 attachment_pool.unlink(cr, uid, values['attachments_ids'], context=context)
95 self.write(cr, uid, id, {'folder':'sent', 'state':'na', 'date_mail':time.strftime("%Y-%m-%d %H:%M:%S")}, context)
96 self.historise(cr, uid, [id], "Email sent successfully", context)
98 error = result['error_msg']
99 self.historise(cr, uid, [id], error, context)
101 except Exception, error:
102 logger = netsvc.Logger()
103 logger.notifyChannel("email-template", netsvc.LOG_ERROR, _("Sending of Mail %s failed. Probable Reason:Could not login to server\nError: %s") % (id, error))
104 self.historise(cr, uid, [id], error, context)
105 self.write(cr, uid, id, {'state':'na'}, context)
108 def historise(self, cr, uid, ids, message='', context=None):
110 history = self.read(cr, uid, id, ['history'], context).get('history', '')
111 self.write(cr, uid, id, {'history': (history or '' )+ "\n" + time.strftime("%Y-%m-%d %H:%M:%S") + ": " + tools.ustr(message)}, context)
114 'email_from':fields.char(
117 'email_to':fields.char(
120 'email_cc':fields.char(
123 'email_bcc':fields.char(
126 'reply_to':fields.char(
129 'message_id':fields.char(
132 'subject':fields.char(
135 'body_text':fields.text(
136 'Standard Body (Text)'),
137 'body_html':fields.text(
138 'Body (Rich Text Clients Only)'),
139 'attachments_ids':fields.many2many(
141 'mail_attachments_rel',
145 'account_id' :fields.many2one(
146 'email_template.account',
149 'user':fields.related(
153 relation="res.users",
155 'server_ref':fields.integer(
156 'Server Reference of mail',
157 help="Applicable for inward items only"),
158 'mail_type':fields.selection([
161 ('multipart/alternative',
162 'Plain Text & HTML with no attachments'),
163 ('multipart/related',
164 'Intermixed content'),
170 #I like GMAIL which allows putting same mail in many folders
171 #Lets plan it for 0.9
172 'folder':fields.selection([
173 ('drafts', 'Drafts'),
174 ('outbox', 'Outbox'),
176 ('sent', 'Sent Items'),
177 ], 'Folder', required=True),
178 'state':fields.selection([
179 ('na', 'Not Applicable'),
180 ('sending', 'Sending'),
181 ], 'Status', required=True),
182 'date_mail':fields.datetime('Rec/Sent Date', help="Date on which Email Sent or Received"),
183 'history':fields.text(
190 'state': lambda * a: 'na',
191 'folder': lambda * a: 'outbox',
194 def unlink(self, cr, uid, ids, context=None):
196 It just changes the folder of the item to "Trash", if it is no in Trash folder yet,
197 or completely deletes it if it is already in Trash.
201 for mail in self.browse(cr, uid, ids, context=context):
202 if mail.folder == 'trash':
203 to_remove.append(mail.id)
205 to_update.append(mail.id)
206 # Changes the folder to trash
207 self.write(cr, uid, to_update, {'folder': 'trash'}, context=context)
208 return super(email_template_mailbox, self).unlink(cr, uid, to_remove, context=context)
210 email_template_mailbox()
212 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: