2010-07-01 13:16:22 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
2010-08-18 15:22:27 +00:00
# Copyright (C) 2009 Sharoon Thomas
# Copyright (C) 2004-2010 OpenERP SA (<http://www.openerp.com>)
2010-07-01 13:16:22 +00:00
#
# This program is free software: you can redistribute it and/or modify
2010-08-18 15:22:27 +00:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
2010-07-01 13:16:22 +00:00
#
# 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
2010-08-18 15:22:27 +00:00
# GNU General Public License for more details.
2010-07-01 13:16:22 +00:00
#
2010-08-18 15:22:27 +00:00
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
2010-07-01 13:16:22 +00:00
#
##############################################################################
2010-06-02 14:36:27 +00:00
from osv import osv , fields
import time
import netsvc
from tools . translate import _
import tools
LOGGER = netsvc . Logger ( )
class email_template_mailbox ( osv . osv ) :
_name = " email_template.mailbox "
_description = ' Email Mailbox '
_rec_name = " subject "
_order = " date_mail desc "
def run_mail_scheduler ( self , cursor , user , context = None ) :
"""
2010-08-10 11:35:06 +00:00
This method is called by OpenERP Scheduler
2010-06-02 14:36:27 +00:00
to periodically send emails
"""
try :
2010-12-13 06:43:09 +00:00
self . send_all_mail ( cursor , user , context = context )
2010-06-02 14:36:27 +00:00
except Exception , e :
LOGGER . notifyChannel (
2010-11-18 16:21:31 +00:00
" Email Template " ,
2010-06-02 14:36:27 +00:00
netsvc . LOG_ERROR ,
2010-11-18 16:21:31 +00:00
_ ( " Error sending mail: %s " ) % e )
2010-06-02 14:36:27 +00:00
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 ) :
2010-06-25 09:52:31 +00:00
result = True
2010-08-26 12:41:51 +00:00
attachment_pool = self . pool . get ( ' ir.attachment ' )
2010-06-11 07:44:19 +00:00
for id in ( ids or [ ] ) :
2010-06-02 14:36:27 +00:00
try :
account_obj = self . pool . get ( ' email_template.account ' )
values = self . read ( cr , uid , id , [ ] , context )
payload = { }
if values [ ' attachments_ids ' ] :
for attid in values [ ' attachments_ids ' ] :
2010-08-26 12:41:51 +00:00
attachment = attachment_pool . browse ( cr , uid , attid , context ) #,['datas_fname','datas'])
2010-06-02 14:36:27 +00:00
payload [ attachment . datas_fname ] = attachment . datas
result = account_obj . send_mail ( cr , uid ,
[ values [ ' account_id ' ] [ 0 ] ] ,
2010-08-18 15:22:27 +00:00
{ ' 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 ' ' } ,
2010-06-02 14:36:27 +00:00
values [ ' subject ' ] or u ' ' ,
2010-08-18 15:22:27 +00:00
{ ' text ' : values . get ( ' body_text ' ) or u ' ' , ' html ' : values . get ( ' body_html ' ) or u ' ' } ,
payload = payload ,
message_id = values [ ' message_id ' ] ,
context = context )
2010-06-02 14:36:27 +00:00
if result == True :
2010-08-26 12:41:51 +00:00
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 )
self . historise ( cr , uid , [ id ] , " Email sent successfully " , context )
2010-06-02 14:36:27 +00:00
else :
2010-06-24 10:52:26 +00:00
error = result [ ' error_msg ' ]
self . historise ( cr , uid , [ id ] , error , context )
2010-06-02 14:36:27 +00:00
except Exception , error :
logger = netsvc . Logger ( )
2010-08-18 15:22:27 +00:00
logger . notifyChannel ( " email-template " , netsvc . LOG_ERROR , _ ( " Sending of Mail %s failed. Probable Reason:Could not login to server \n Error: %s " ) % ( id , error ) )
2010-06-02 14:36:27 +00:00
self . historise ( cr , uid , [ id ] , error , context )
self . write ( cr , uid , id , { ' state ' : ' na ' } , context )
2010-06-24 10:52:26 +00:00
return result
2010-06-02 14:36:27 +00:00
def historise ( self , cr , uid , ids , message = ' ' , context = None ) :
for id in ids :
history = self . read ( cr , uid , id , [ ' history ' ] , context ) . get ( ' history ' , ' ' )
2010-08-26 12:00:20 +00:00
self . write ( cr , uid , id , { ' history ' : ( history or ' ' ) + " \n " + time . strftime ( " % Y- % m- %d % H: % M: % S " ) + " : " + tools . ustr ( message ) } , context )
2010-06-02 14:36:27 +00:00
_columns = {
' email_from ' : fields . char (
' From ' ,
size = 64 ) ,
' email_to ' : fields . char (
2010-08-18 15:22:27 +00:00
' Recipient (To) ' ,
2010-06-02 14:36:27 +00:00
size = 250 , ) ,
' email_cc ' : fields . char (
2010-08-18 15:22:27 +00:00
' CC ' ,
2010-06-02 14:36:27 +00:00
size = 250 ) ,
' email_bcc ' : fields . char (
2010-08-18 15:22:27 +00:00
' BCC ' ,
size = 250 ) ,
' reply_to ' : fields . char (
' Reply-To ' ,
size = 250 ) ,
' message_id ' : fields . char (
' Message-ID ' ,
2010-06-02 14:36:27 +00:00
size = 250 ) ,
' subject ' : fields . char (
2010-08-18 15:22:27 +00:00
' Subject ' ,
2010-06-02 14:36:27 +00:00
size = 200 , ) ,
' body_text ' : fields . text (
' Standard Body (Text) ' ) ,
' body_html ' : fields . text (
2010-08-18 15:22:27 +00:00
' Body (Rich Text Clients Only) ' ) ,
2010-06-02 14:36:27 +00:00
' attachments_ids ' : fields . many2many (
' ir.attachment ' ,
' mail_attachments_rel ' ,
' mail_id ' ,
' att_id ' ,
' Attachments ' ) ,
' account_id ' : fields . many2one (
' email_template.account ' ,
' User account ' ,
required = True ) ,
' user ' : fields . related (
' account_id ' ,
' user ' ,
type = " many2one " ,
relation = " res.users " ,
string = " User " ) ,
' server_ref ' : fields . integer (
' Server Reference of mail ' ,
help = " Applicable for inward items only " ) ,
' mail_type ' : fields . selection ( [
( ' multipart/mixed ' ,
' Has Attachments ' ) ,
( ' multipart/alternative ' ,
' Plain Text & HTML with no attachments ' ) ,
( ' multipart/related ' ,
' Intermixed content ' ) ,
( ' text/plain ' ,
' Plain Text ' ) ,
( ' text/html ' ,
' HTML Body ' ) ,
] , ' Mail Contents ' ) ,
#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 ) ,
' state ' : fields . selection ( [
( ' na ' , ' Not Applicable ' ) ,
( ' sending ' , ' Sending ' ) ,
] , ' Status ' , required = True ) ,
2010-08-25 09:50:57 +00:00
' date_mail ' : fields . datetime ( ' Rec/Sent Date ' , help = " Date on which Email Sent or Received " ) ,
2010-06-02 14:36:27 +00:00
' history ' : fields . text (
' History ' ,
readonly = True ,
store = True )
}
_defaults = {
' state ' : lambda * a : ' na ' ,
' folder ' : lambda * a : ' outbox ' ,
}
2010-08-25 07:24:23 +00:00
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 )
2010-06-02 14:36:27 +00:00
email_template_mailbox ( )
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: