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
2010-09-15 12:32:13 +00:00
# Copyright (C) 2010-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
import base64
import random
import netsvc
2010-11-27 20:14:08 +00:00
import logging
2010-08-18 15:22:27 +00:00
import re
2010-06-02 14:36:27 +00:00
TEMPLATE_ENGINES = [ ]
from osv import osv , fields
from tools . translate import _
2010-10-06 05:26:12 +00:00
2010-06-02 14:36:27 +00:00
import tools
import pooler
2010-07-13 16:24:26 +00:00
import logging
2010-06-02 14:36:27 +00:00
def get_value ( cursor , user , recid , message = None , template = None , context = None ) :
"""
Evaluates an expression and returns its value
@param cursor : Database Cursor
@param user : ID of current user
@param recid : ID of the target record under evaluation
@param message : The expression to be evaluated
@param template : BrowseRecord object of the current template
2010-08-10 11:35:06 +00:00
@param context : OpenERP Context
2010-06-02 14:36:27 +00:00
@return : Computed message ( unicode ) or u " "
"""
pool = pooler . get_pool ( cursor . dbname )
if message is None :
message = { }
#Returns the computed expression
if message :
try :
message = tools . ustr ( message )
2010-11-19 13:48:01 +00:00
object = pool . get ( template . model_int_name ) . browse ( cursor , user , recid , context = context )
2010-06-02 14:36:27 +00:00
env = {
2010-11-19 13:48:01 +00:00
' user ' : pool . get ( ' res.users ' ) . browse ( cursor , user , user , context = context ) ,
2010-06-02 14:36:27 +00:00
' db ' : cursor . dbname
2011-02-03 10:14:28 +00:00
}
templ = MakoTemplate ( message , input_encoding = ' utf-8 ' )
reply = MakoTemplate ( message ) . render_unicode ( object = object , peobject = object , env = env , format_exceptions = True )
2010-06-02 14:36:27 +00:00
return reply or False
except Exception :
2010-07-13 16:24:26 +00:00
logging . exception ( " can ' t render %r " , message )
2010-06-02 14:36:27 +00:00
return u " "
else :
return message
class email_template ( osv . osv ) :
" Templates for sending Email "
2010-10-26 08:47:22 +00:00
2010-06-02 14:36:27 +00:00
_name = " email.template "
_description = ' Email Templates for Models '
def change_model ( self , cursor , user , ids , object_name , context = None ) :
if object_name :
mod_name = self . pool . get ( ' ir.model ' ) . read (
cursor ,
user ,
object_name ,
[ ' model ' ] , context ) [ ' model ' ]
else :
mod_name = False
2011-02-02 08:40:29 +00:00
return { ' value ' : { ' model_int_name ' : mod_name } }
2010-06-02 14:36:27 +00:00
_columns = {
2010-06-10 12:44:50 +00:00
' name ' : fields . char ( ' Name ' , size = 100 , required = True ) ,
2010-08-26 14:06:25 +00:00
' object_name ' : fields . many2one ( ' ir.model ' , ' Resource ' ) ,
2010-06-02 14:36:27 +00:00
' model_int_name ' : fields . char ( ' Model Internal Name ' , size = 200 , ) ,
2010-08-18 15:22:27 +00:00
' from_account ' : fields . many2one (
2011-02-01 11:43:08 +00:00
' email.smtp_server ' ,
2010-08-18 15:22:27 +00:00
string = " Email Account " ,
help = " Emails will be sent from this approved account. " ) ,
2010-06-02 14:36:27 +00:00
' def_to ' : fields . char (
2010-07-16 09:05:13 +00:00
' Recipient (To) ' ,
2010-06-02 14:36:27 +00:00
size = 250 ,
2010-10-26 08:47:22 +00:00
help = " The Recipient of email. "
2010-09-01 07:07:24 +00:00
" Placeholders can be used here. "
" e.g. $ {object.email_to} " ) ,
2010-06-02 14:36:27 +00:00
' def_cc ' : fields . char (
2010-08-18 15:22:27 +00:00
' CC ' ,
2010-06-02 14:36:27 +00:00
size = 250 ,
2010-08-18 15:22:27 +00:00
help = " Carbon Copy address(es), comma-separated. "
2010-09-01 07:07:24 +00:00
" Placeholders can be used here. "
" e.g. $ {object.email_cc} " ) ,
2010-06-02 14:36:27 +00:00
' def_bcc ' : fields . char (
2010-08-18 15:22:27 +00:00
' BCC ' ,
2010-06-02 14:36:27 +00:00
size = 250 ,
2010-08-18 15:22:27 +00:00
help = " Blind Carbon Copy address(es), comma-separated. "
2010-09-01 07:07:24 +00:00
" Placeholders can be used here. "
" e.g. $ {object.email_bcc} " ) ,
2010-10-26 08:47:22 +00:00
' reply_to ' : fields . char ( ' Reply-To ' ,
size = 250 ,
2010-08-18 15:22:27 +00:00
help = " The address recipients should reply to, "
2010-09-01 07:07:24 +00:00
" if different from the From address. "
" Placeholders can be used here. "
" e.g. $ {object.email_reply_to} " ) ,
2010-10-26 08:47:22 +00:00
' message_id ' : fields . char ( ' Message-ID ' ,
size = 250 ,
2010-08-26 14:06:25 +00:00
help = " Specify the Message-ID SMTP header to use in outgoing emails. Please note that this overrides the Resource tracking option! Placeholders can be used here. " ) ,
' track_campaign_item ' : fields . boolean ( ' Resource Tracking ' ,
help = " Enable this is you wish to include a special \
tracking marker in outgoing emails so you can identify replies and link \
them back to the corresponding resource record . \
2010-10-26 08:47:22 +00:00
This is useful for CRM leads for example " ),
2010-06-02 14:36:27 +00:00
' lang ' : fields . char (
' Language ' ,
size = 250 ,
help = " The default language for the email. "
" Placeholders can be used here. "
" eg. $ {object.partner_id.lang} " ) ,
' def_subject ' : fields . char (
2010-08-26 14:06:25 +00:00
' Subject ' ,
2010-06-02 14:36:27 +00:00
size = 200 ,
2010-08-26 14:06:25 +00:00
help = " The subject of email. "
2010-06-02 14:36:27 +00:00
" Placeholders can be used here. " ,
translate = True ) ,
' def_body_text ' : fields . text (
' Standard Body (Text) ' ,
help = " The text version of the mail " ,
translate = True ) ,
' def_body_html ' : fields . text (
' Body (Text-Web Client Only) ' ,
help = " The text version of the mail " ,
translate = True ) ,
' use_sign ' : fields . boolean (
2010-06-10 12:44:50 +00:00
' Signature ' ,
2010-10-26 08:47:22 +00:00
help = " the signature from the User details "
2010-07-16 09:05:13 +00:00
" will be appended to the mail " ) ,
2010-06-02 14:36:27 +00:00
' file_name ' : fields . char (
2010-09-13 00:43:42 +00:00
' Report Filename ' ,
2010-06-02 14:36:27 +00:00
size = 200 ,
2010-09-13 00:43:42 +00:00
help = " Name of the generated report file. Placeholders can be used in the filename. eg: 2009_SO003.pdf " ,
2010-06-02 14:36:27 +00:00
translate = True ) ,
' report_template ' : fields . many2one (
' ir.actions.report.xml ' ,
' Report to send ' ) ,
2010-08-18 15:22:27 +00:00
' attachment_ids ' : fields . many2many (
' ir.attachment ' ,
' email_template_attachment_rel ' ,
' email_template_id ' ,
' attachment_id ' ,
' Attached Files ' ,
help = " You may attach existing files to this template, "
2010-10-26 08:47:22 +00:00
" so they will be added in all emails created from this template " ) ,
2010-06-02 14:36:27 +00:00
' ref_ir_act_window ' : fields . many2one (
' ir.actions.act_window ' ,
' Window Action ' ,
2010-10-26 08:47:22 +00:00
help = " Action that will open this email template on Resource records " ,
2010-06-02 14:36:27 +00:00
readonly = True ) ,
' ref_ir_value ' : fields . many2one (
' ir.values ' ,
' Wizard Button ' ,
2010-10-26 08:47:22 +00:00
help = " Button in the side bar of the form view of this Resource that will invoke the Window Action " ,
2010-06-02 14:36:27 +00:00
readonly = True ) ,
' allowed_groups ' : fields . many2many (
' res.groups ' ,
' template_group_rel ' ,
' templ_id ' , ' group_id ' ,
string = " Allowed User Groups " ,
help = " Only users from these groups will be "
" allowed to send mails from this Template " ) ,
' model_object_field ' : fields . many2one (
' ir.model.fields ' ,
string = " Field " ,
help = " Select the field from the model you want to use. "
" \n If it is a relationship field you will be able to "
" choose the nested values in the box below \n (Note:If "
" there are no values make sure you have selected the "
" correct model) " ,
store = False ) ,
' sub_object ' : fields . many2one (
' ir.model ' ,
' Sub-model ' ,
help = ' When a relation field is used this field '
' will show you the type of field you have selected ' ,
store = False ) ,
' sub_model_object_field ' : fields . many2one (
' ir.model.fields ' ,
' Sub Field ' ,
help = " When you choose relationship fields "
" this field will specify the sub value you can use. " ,
store = False ) ,
' null_value ' : fields . char (
' Null Value ' ,
help = " This Value is used if the field is empty " ,
size = 50 , store = False ) ,
' copyvalue ' : fields . char (
' Expression ' ,
size = 100 ,
help = " Copy and paste the value in the "
" location you want to use a system value. " ,
store = False ) ,
' table_html ' : fields . text (
' HTML code ' ,
help = " Copy this html code to your HTML message "
" body for displaying the info in your mail. " ,
store = False ) ,
2011-02-01 11:43:08 +00:00
' auto_delete ' : fields . boolean ( ' Auto Delete ' , help = " Permanently delete emails after sending " ) ,
2010-06-02 14:36:27 +00:00
}
_sql_constraints = [
2010-10-28 11:40:19 +00:00
( ' name ' , ' unique (name) ' , ' The template name must be unique ! ' )
2010-06-02 14:36:27 +00:00
]
2010-11-19 13:48:01 +00:00
def create_action ( self , cr , uid , ids , context = None ) :
2010-06-25 09:52:31 +00:00
vals = { }
2010-11-23 07:05:05 +00:00
if context is None :
2010-11-19 13:48:01 +00:00
context = { }
template_obj = self . browse ( cr , uid , ids , context = context ) [ 0 ]
2010-06-25 09:52:31 +00:00
src_obj = template_obj . object_name . model
2010-06-02 14:36:27 +00:00
vals [ ' ref_ir_act_window ' ] = self . pool . get ( ' ir.actions.act_window ' ) . create ( cr , uid , {
2010-06-25 12:04:22 +00:00
' name ' : template_obj . name ,
2010-06-02 14:36:27 +00:00
' type ' : ' ir.actions.act_window ' ,
' res_model ' : ' email_template.send.wizard ' ,
' src_model ' : src_obj ,
' view_type ' : ' form ' ,
2010-06-25 09:52:31 +00:00
' context ' : " { ' src_model ' : ' %s ' , ' template_id ' : ' %d ' , ' src_rec_id ' :active_id, ' src_rec_ids ' :active_ids} " % ( src_obj , template_obj . id ) ,
2010-06-02 14:36:27 +00:00
' view_mode ' : ' form,tree ' ,
' view_id ' : self . pool . get ( ' ir.ui.view ' ) . search ( cr , uid , [ ( ' name ' , ' = ' , ' email_template.send.wizard.form ' ) ] , context = context ) [ 0 ] ,
' target ' : ' new ' ,
' auto_refresh ' : 1
} , context )
vals [ ' ref_ir_value ' ] = self . pool . get ( ' ir.values ' ) . create ( cr , uid , {
2010-06-25 09:52:31 +00:00
' name ' : _ ( ' Send Mail ( %s ) ' ) % template_obj . name ,
2010-06-02 14:36:27 +00:00
' model ' : src_obj ,
' key2 ' : ' client_action_multi ' ,
' value ' : " ir.actions.act_window, " + str ( vals [ ' ref_ir_act_window ' ] ) ,
' object ' : True ,
} , context )
2010-06-25 09:52:31 +00:00
self . write ( cr , uid , ids , {
2010-06-02 14:36:27 +00:00
' ref_ir_act_window ' : vals [ ' ref_ir_act_window ' ] ,
' ref_ir_value ' : vals [ ' ref_ir_value ' ] ,
} , context )
2010-06-25 09:52:31 +00:00
return True
2010-06-02 14:36:27 +00:00
2010-11-19 13:48:01 +00:00
def unlink_action ( self , cr , uid , ids , context = None ) :
for template in self . browse ( cr , uid , ids , context = context ) :
2010-06-02 14:36:27 +00:00
try :
if template . ref_ir_act_window :
self . pool . get ( ' ir.actions.act_window ' ) . unlink ( cr , uid , template . ref_ir_act_window . id , context )
if template . ref_ir_value :
self . pool . get ( ' ir.values ' ) . unlink ( cr , uid , template . ref_ir_value . id , context )
except :
raise osv . except_osv ( _ ( " Warning " ) , _ ( " Deletion of Record failed " ) )
2010-06-25 09:52:31 +00:00
2010-11-19 13:48:01 +00:00
def delete_action ( self , cr , uid , ids , context = None ) :
self . unlink_action ( cr , uid , ids , context = context )
2010-06-25 09:52:31 +00:00
return True
2010-10-26 08:47:22 +00:00
2010-06-25 09:52:31 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
2010-11-19 13:48:01 +00:00
self . unlink_action ( cr , uid , ids , context = context )
return super ( email_template , self ) . unlink ( cr , uid , ids , context = context )
2010-10-26 08:47:22 +00:00
2010-06-02 14:36:27 +00:00
def copy ( self , cr , uid , id , default = None , context = None ) :
if default is None :
default = { }
default = default . copy ( )
old = self . read ( cr , uid , id , [ ' name ' ] , context = context )
new_name = _ ( " Copy of template " ) + old . get ( ' name ' , ' No Name ' )
check = self . search ( cr , uid , [ ( ' name ' , ' = ' , new_name ) ] , context = context )
if check :
new_name = new_name + ' _ ' + random . choice ( ' abcdefghij ' ) + random . choice ( ' lmnopqrs ' ) + random . choice ( ' tuvwzyz ' )
default . update ( { ' name ' : new_name } )
return super ( email_template , self ) . copy ( cr , uid , id , default , context )
2010-10-26 08:47:22 +00:00
2011-02-03 10:14:28 +00:00
def build_expression ( self , field_name , sub_field_name , null_value ) :
2010-06-02 14:36:27 +00:00
"""
2010-09-27 14:17:58 +00:00
Returns a template expression based on data provided
@param field_name : field name
@param sub_field_name : sub field name ( M2O )
@param null_value : default value if the target value is empty
2010-06-02 14:36:27 +00:00
@return : computed expression
"""
2010-09-27 14:17:58 +00:00
expression = ' '
2011-02-03 10:14:28 +00:00
if field_name :
expression = " $ { object. " + field_name
if sub_field_name :
expression + = " . " + sub_field_name
if null_value :
expression + = " or ' ' ' %s ' ' ' " % null_value
expression + = " } "
2010-10-26 08:47:22 +00:00
return expression
2011-02-03 10:14:28 +00:00
def onchange_model_object_field ( self , cr , uid , ids , model_object_field , context = None ) :
2010-06-02 14:36:27 +00:00
if not model_object_field :
return { }
result = { }
field_obj = self . pool . get ( ' ir.model.fields ' ) . browse ( cr , uid , model_object_field , context )
#Check if field is relational
if field_obj . ttype in [ ' many2one ' , ' one2many ' , ' many2many ' ] :
res_ids = self . pool . get ( ' ir.model ' ) . search ( cr , uid , [ ( ' model ' , ' = ' , field_obj . relation ) ] , context = context )
if res_ids :
result [ ' sub_object ' ] = res_ids [ 0 ]
2011-02-03 10:14:28 +00:00
result [ ' copyvalue ' ] = self . build_expression ( False , False , False )
2010-06-02 14:36:27 +00:00
result [ ' sub_model_object_field ' ] = False
result [ ' null_value ' ] = False
else :
#Its a simple field... just compute placeholder
result [ ' sub_object ' ] = False
2011-02-03 10:14:28 +00:00
result [ ' copyvalue ' ] = self . build_expression ( field_obj . name , False , False )
2010-06-02 14:36:27 +00:00
result [ ' sub_model_object_field ' ] = False
result [ ' null_value ' ] = False
return { ' value ' : result }
2010-10-26 08:47:22 +00:00
2011-02-03 10:14:28 +00:00
def onchange_sub_model_object_field ( self , cr , uid , ids , model_object_field , sub_model_object_field , context = None ) :
2010-06-02 14:36:27 +00:00
if not model_object_field or not sub_model_object_field :
return { }
result = { }
field_obj = self . pool . get ( ' ir.model.fields ' ) . browse ( cr , uid , model_object_field , context )
if field_obj . ttype in [ ' many2one ' , ' one2many ' , ' many2many ' ] :
res_ids = self . pool . get ( ' ir.model ' ) . search ( cr , uid , [ ( ' model ' , ' = ' , field_obj . relation ) ] , context = context )
sub_field_obj = self . pool . get ( ' ir.model.fields ' ) . browse ( cr , uid , sub_model_object_field , context )
if res_ids :
result [ ' sub_object ' ] = res_ids [ 0 ]
2011-02-03 10:14:28 +00:00
result [ ' copyvalue ' ] = self . build_expression ( field_obj . name , sub_field_obj . name , False )
2010-06-02 14:36:27 +00:00
result [ ' sub_model_object_field ' ] = sub_model_object_field
result [ ' null_value ' ] = False
else :
#Its a simple field... just compute placeholder
result [ ' sub_object ' ] = False
2011-02-03 10:14:28 +00:00
result [ ' copyvalue ' ] = self . build_expression ( field_obj . name , False , False )
2010-06-02 14:36:27 +00:00
result [ ' sub_model_object_field ' ] = False
result [ ' null_value ' ] = False
return { ' value ' : result }
2011-02-03 10:14:28 +00:00
def onchange_null_value ( self , cr , uid , ids , model_object_field , sub_model_object_field , null_value , context = None ) :
2010-06-02 14:36:27 +00:00
if not model_object_field and not null_value :
return { }
result = { }
field_obj = self . pool . get ( ' ir.model.fields ' ) . browse ( cr , uid , model_object_field , context )
if field_obj . ttype in [ ' many2one ' , ' one2many ' , ' many2many ' ] :
res_ids = self . pool . get ( ' ir.model ' ) . search ( cr , uid , [ ( ' model ' , ' = ' , field_obj . relation ) ] , context = context )
sub_field_obj = self . pool . get ( ' ir.model.fields ' ) . browse ( cr , uid , sub_model_object_field , context )
if res_ids :
result [ ' sub_object ' ] = res_ids [ 0 ]
2011-02-03 10:14:28 +00:00
result [ ' copyvalue ' ] = self . build_expression ( field_obj . name , sub_field_obj . name , null_value )
2010-06-02 14:36:27 +00:00
result [ ' sub_model_object_field ' ] = sub_model_object_field
result [ ' null_value ' ] = null_value
else :
#Its a simple field... just compute placeholder
result [ ' sub_object ' ] = False
2011-02-03 10:14:28 +00:00
result [ ' copyvalue ' ] = self . build_expression ( field_obj . name , False , null_value )
2010-06-02 14:36:27 +00:00
result [ ' sub_model_object_field ' ] = False
result [ ' null_value ' ] = null_value
return { ' value ' : result }
2010-08-18 15:22:27 +00:00
def _add_attachment ( self , cursor , user , mailbox_id , name , data , filename , context = None ) :
"""
Add an attachment to a given mailbox entry .
: param data : base64 encoded attachment data to store
"""
attachment_obj = self . pool . get ( ' ir.attachment ' )
attachment_data = {
' name ' : ( name or ' ' ) + _ ( ' (Email Attachment) ' ) ,
' datas ' : data ,
' datas_fname ' : filename ,
' description ' : name or _ ( ' No Description ' ) ,
2011-02-02 08:40:29 +00:00
' res_model ' : ' email.message ' ,
2010-08-18 15:22:27 +00:00
' res_id ' : mailbox_id ,
}
attachment_id = attachment_obj . create ( cursor ,
user ,
attachment_data ,
context )
if attachment_id :
2011-02-02 08:40:29 +00:00
self . pool . get ( ' email.message ' ) . write (
2010-08-18 15:22:27 +00:00
cursor ,
user ,
mailbox_id ,
{
' attachments_ids ' : [ ( 4 , attachment_id ) ] ,
' mail_type ' : ' multipart/mixed '
} ,
context )
2011-02-02 08:40:29 +00:00
def generate_attach_reports ( self , cursor , user , template , record_id , mail , context = None ) :
2010-06-02 14:36:27 +00:00
"""
Generate report to be attached and attach it
2010-08-18 15:22:27 +00:00
to the email , and add any directly attached files as well .
2010-10-26 08:47:22 +00:00
2010-06-02 14:36:27 +00:00
@param cursor : Database Cursor
@param user : ID of User
@param template : Browse record of
template
@param record_id : ID of the target model
for which this mail has
to be generated
2010-10-26 08:47:22 +00:00
@param mail : Browse record of email object
@return : True
2010-06-02 14:36:27 +00:00
"""
2010-08-18 15:22:27 +00:00
if template . report_template :
reportname = ' report. ' + \
self . pool . get ( ' ir.actions.report.xml ' ) . read (
cursor ,
user ,
template . report_template . id ,
[ ' report_name ' ] ,
context ) [ ' report_name ' ]
service = netsvc . LocalService ( reportname )
data = { }
data [ ' model ' ] = template . model_int_name
( result , format ) = service . create ( cursor ,
2010-06-02 14:36:27 +00:00
user ,
2010-08-18 15:22:27 +00:00
[ record_id ] ,
data ,
2010-10-26 08:47:22 +00:00
context )
2010-08-18 15:22:27 +00:00
fname = tools . ustr ( get_value ( cursor , user , record_id ,
template . file_name , template , context )
or ' Report ' )
ext = ' . ' + format
if not fname . endswith ( ext ) :
fname + = ext
self . _add_attachment ( cursor , user , mail . id , mail . subject , base64 . b64encode ( result ) , fname , context )
if template . attachment_ids :
for attachment in template . attachment_ids :
self . _add_attachment ( cursor , user , mail . id , attachment . name , attachment . datas , attachment . datas_fname , context )
2010-06-02 14:36:27 +00:00
return True
2010-10-26 08:47:22 +00:00
2011-02-02 08:40:29 +00:00
def _generate_mailbox_item_from_template ( self , cursor , user , template , record_id , context = None ) :
2010-06-02 14:36:27 +00:00
"""
Generates an email from the template for
record record_id of target object
2010-10-26 08:47:22 +00:00
2010-06-02 14:36:27 +00:00
@param cursor : Database Cursor
@param user : ID of User
@param template : Browse record of
template
@param record_id : ID of the target model
for which this mail has
to be generated
2010-10-26 08:47:22 +00:00
@return : ID of created object
2010-06-02 14:36:27 +00:00
"""
if context is None :
context = { }
#If account to send from is in context select it, else use enforced account
if ' account_id ' in context . keys ( ) :
2011-02-01 11:43:08 +00:00
from_account = self . pool . get ( ' email.smtp_server ' ) . read (
2010-06-02 14:36:27 +00:00
cursor ,
user ,
context . get ( ' account_id ' ) ,
[ ' name ' , ' email_id ' ] ,
context
)
else :
from_account = {
2010-08-18 15:22:27 +00:00
' id ' : template . from_account . id ,
' name ' : template . from_account . name ,
' email_id ' : template . from_account . email_id
2010-06-02 14:36:27 +00:00
}
lang = get_value ( cursor ,
user ,
record_id ,
template . lang ,
template ,
context )
if lang :
ctx = context . copy ( )
ctx . update ( { ' lang ' : lang } )
2010-07-13 16:24:26 +00:00
template = self . browse ( cursor , user , template . id , context = ctx )
2010-10-26 08:47:22 +00:00
# determine name of sender, either it is specified in email_id or we
# use the account name
2010-08-18 15:22:27 +00:00
email_id = from_account [ ' email_id ' ] . strip ( )
email_from = re . findall ( r ' ([^ ,<@]+@[^> ,]+) ' , email_id ) [ 0 ]
if email_from != email_id :
# we should keep it all, name is probably specified in the address
email_from = from_account [ ' email_id ' ]
else :
2011-01-13 13:02:17 +00:00
email_from = tools . ustr ( from_account [ ' name ' ] ) + " < " + tools . ustr ( email_id ) + " > "
2010-08-18 15:22:27 +00:00
# FIXME: should do this in a loop and rename template fields to the corresponding
# mailbox fields. (makes no sense to have different names I think.
2010-06-02 14:36:27 +00:00
mailbox_values = {
2010-08-18 15:22:27 +00:00
' email_from ' : email_from ,
2010-06-02 14:36:27 +00:00
' email_to ' : get_value ( cursor ,
user ,
record_id ,
template . def_to ,
template ,
context ) ,
' email_cc ' : get_value ( cursor ,
user ,
record_id ,
template . def_cc ,
template ,
context ) ,
' email_bcc ' : get_value ( cursor ,
user ,
record_id ,
template . def_bcc ,
template ,
context ) ,
2010-08-18 15:22:27 +00:00
' reply_to ' : get_value ( cursor ,
user ,
record_id ,
template . reply_to ,
template ,
context ) ,
2010-06-02 14:36:27 +00:00
' subject ' : get_value ( cursor ,
user ,
record_id ,
template . def_subject ,
template ,
context ) ,
' body_text ' : get_value ( cursor ,
user ,
record_id ,
template . def_body_text ,
template ,
context ) ,
' body_html ' : get_value ( cursor ,
user ,
record_id ,
template . def_body_html ,
template ,
context ) ,
' account_id ' : from_account [ ' id ' ] ,
#This is a mandatory field when automatic emails are sent
' state ' : ' na ' ,
' folder ' : ' drafts ' ,
2010-08-18 15:22:27 +00:00
' mail_type ' : ' multipart/alternative ' ,
2010-06-02 14:36:27 +00:00
}
2010-08-18 15:22:27 +00:00
2010-08-26 14:06:25 +00:00
if template [ ' message_id ' ] :
# use provided message_id with placeholders
mailbox_values . update ( { ' message_id ' : get_value ( cursor , user , record_id , template [ ' message_id ' ] , template , context ) } )
2010-09-13 00:43:42 +00:00
elif template [ ' track_campaign_item ' ] :
# get appropriate message-id
2010-08-26 14:06:25 +00:00
mailbox_values . update ( { ' message_id ' : tools . misc . generate_tracking_message_id ( record_id ) } )
2010-08-18 15:22:27 +00:00
2010-06-11 07:44:19 +00:00
if not mailbox_values [ ' account_id ' ] :
raise Exception ( " Unable to send the mail. No account linked to the template. " )
2010-06-02 14:36:27 +00:00
#Use signatures if allowed
if template . use_sign :
sign = self . pool . get ( ' res.users ' ) . read ( cursor ,
user ,
user ,
[ ' signature ' ] ,
context ) [ ' signature ' ]
if mailbox_values [ ' body_text ' ] :
mailbox_values [ ' body_text ' ] + = sign
if mailbox_values [ ' body_html ' ] :
mailbox_values [ ' body_html ' ] + = sign
2011-02-02 08:40:29 +00:00
mailbox_id = self . pool . get ( ' email.message ' ) . create (
2010-06-02 14:36:27 +00:00
cursor ,
user ,
mailbox_values ,
context )
2010-06-11 07:44:19 +00:00
2010-06-02 14:36:27 +00:00
return mailbox_id
2010-10-26 08:47:22 +00:00
2010-06-11 07:44:19 +00:00
2011-02-02 08:40:29 +00:00
def generate_mail ( self , cursor , user , template_id , record_ids , context = None ) :
2010-06-02 14:36:27 +00:00
if context is None :
context = { }
template = self . browse ( cursor , user , template_id , context = context )
if not template :
raise Exception ( " The requested template could not be loaded " )
2010-06-24 10:52:26 +00:00
result = True
2011-02-02 08:40:29 +00:00
mailbox_obj = self . pool . get ( ' email.message ' )
2010-06-02 14:36:27 +00:00
for record_id in record_ids :
mailbox_id = self . _generate_mailbox_item_from_template (
cursor ,
user ,
template ,
record_id ,
context )
2010-08-18 15:22:27 +00:00
mail = mailbox_obj . browse (
cursor ,
user ,
mailbox_id ,
context = context
)
if template . report_template or template . attachment_ids :
2010-07-13 16:24:26 +00:00
self . generate_attach_reports (
2010-06-02 14:36:27 +00:00
cursor ,
user ,
template ,
record_id ,
mail ,
context
)
2011-02-02 08:40:29 +00:00
self . pool . get ( ' email.message ' ) . write (
2010-06-02 14:36:27 +00:00
cursor ,
user ,
mailbox_id ,
{ ' folder ' : ' outbox ' } ,
context = context
2010-06-11 07:44:19 +00:00
)
2010-06-24 10:52:26 +00:00
# TODO : manage return value of all the records
2011-02-03 06:59:13 +00:00
result = self . pool . get ( ' email.message ' ) . email_send ( cursor , user , [ mailbox_id ] , context )
2010-06-24 10:52:26 +00:00
return result
2010-06-02 14:36:27 +00:00
email_template ( )
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: