[REVIEW+IMP] - emails: added new state in email.message: Sent, Exception

if email sent successfully, set as a Sent otherwise Exception.
- email_template: now email_template.send_wizard inherit from email.template

bzr revid: hmo@tinyerp.com-20110215054805-9llgl3bltx5dzz0y
This commit is contained in:
Harry (OpenERP) 2011-02-15 11:18:05 +05:30
parent f517e94750
commit b7baed1bca
5 changed files with 27 additions and 28 deletions

View File

@ -47,12 +47,10 @@ class email_template(osv.osv):
context = {}
if not template_id:
template_id = context.get('template_id', False)
if not template_id:
return False
template_pool = self.pool.get('email.template')
template = template_pool.browse(cr, uid, int(template_id), context)
template = self.browse(cr, uid, int(template_id), context)
lang = self.get_template_value(cr, uid, template.lang, template.model, record_id, context)
if lang:
# Use translated template if necessary
@ -382,7 +380,7 @@ This is useful for CRM leads for example"),
else:
email_from = tools.ustr(smtp_server.name) + "<" + tools.ustr(email_id) + ">"
model = template.model
model = template.model_id.model
values = {
'email_from': email_from,
'email_to': self.get_template_value(cr, uid, template.email_to, model, record_id, context),
@ -439,7 +437,7 @@ This is useful for CRM leads for example"),
#Send emails
email_id = email_message_pool.email_send(cr, uid, values.get('email_from'), values.get('email_to'), values.get('name'), values.get('description'),
model=template.model, email_cc=values.get('email_cc'), email_bcc=values.get('email_bcc'), reply_to=values.get('reply_to'),
model=model, email_cc=values.get('email_cc'), email_bcc=values.get('email_bcc'), reply_to=values.get('reply_to'),
attach=attachment, openobject_id=record_id, debug=True, subtype='plain', x_headers={}, priority='3', smtp_server_id=smtp_server.id, context=context)
email_message_pool.write(cr, uid, email_id, {'template_id': template.id})
return email_id
@ -454,7 +452,6 @@ This is useful for CRM leads for example"),
for template in self.browse(cr, uid, ids, context=context):
for record_id in record_ids:
email_id = self._generate_email(cr, uid, template.id, record_id, context)
email_message_pool.write(cr, uid, email_id, {'folder':'outbox', 'state': 'waiting'}, context=context)
email_ids.append(email_id)
return email_ids
email_template()

View File

@ -31,17 +31,6 @@ class email_template_preview(osv.osv_memory):
_description = "Email Template Preview"
_rec_name = "subject"
def get_email_template(self, cr, uid, template_id=None, record_id=None, context=None):
if context is None:
context = {}
template_id = context.get('template_id', False)
record_id = context.get('src_rec_ids',[]) and context.get('src_rec_ids')[0]
return super(email_template_preview, self).get_email_template(cr, uid, template_id, record_id, context=context)
def _get_records(self, cr, uid, context=None):
"""
Return Records of particular Email Template's Model
@ -94,7 +83,8 @@ class email_template_preview(osv.osv_memory):
context = self.context
template_pool = self.pool.get('email.template')
template = self.get_email_template(cr, uid, context)
template_id = context.get('template_id', False)
template = template_pool.get_email_template(cr, uid, template_id=template_id, record_id=res_id, context=context)
model = template.model
vals['email_to'] = self.get_template_value(cr, uid, template.email_to, model, res_id, context)
vals['email_cc'] = self.get_template_value(cr, uid, template.email_cc, model, res_id, context)

View File

@ -28,7 +28,7 @@ import tools
class email_template_send_wizard(osv.osv_memory):
_name = 'email_template.send.wizard'
_inherit = 'email_template.preview'
_inherit = 'email.template'
_description = 'This is the wizard for sending mail'
def default_get(self, cr, uid, fields, context=None):
@ -39,7 +39,7 @@ class email_template_send_wizard(osv.osv_memory):
template_pool = self.pool.get('email.template')
model_pool = self.pool.get('ir.model')
template_id=context.get('template_id', False)
template = self.get_email_template(cr, uid, template_id=template_id, context=context)
template = template_pool.get_email_template(cr, uid, template_id=template_id, context=context)
def _get_template_value(field):
if not template:
return False
@ -51,6 +51,9 @@ class email_template_send_wizard(osv.osv_memory):
if 'user_signature' in fields:
result['user_signature'] = template.user_signature
if 'report_template' in fields:
result['report_template'] = template.report_template
if 'template_id' in fields:
result['template_id'] = template.id

View File

@ -186,6 +186,8 @@ class email_message(osv.osv):
('draft', 'Draft'),
('sending', 'Sending'),
('waiting', 'Waiting'),
('sent', 'Sent'),
('exception', 'Exception'),
], 'State', readonly=True),
}
@ -238,13 +240,13 @@ class email_message(osv.osv):
def email_send(self, cr, uid, email_from, email_to, subject, body, model=False, email_cc=None, email_bcc=None, reply_to=False, attach=None,
openobject_id=False, debug=False, subtype='plain', x_headers={}, priority='3', smtp_server_id=False, context=None):
attachment_obj = self.pool.get('ir.attachment')
if type(email_to) != list:
if email_to and type(email_to) != list:
email_to = [email_to]
if type(email_cc) != list:
if email_cc and type(email_cc) != list:
email_cc = [email_cc]
if type(email_bcc) != list:
if email_bcc and type(email_bcc) != list:
email_bcc = [email_bcc]
if type(reply_to) != list:
if reply_to and type(reply_to) != list:
reply_to = [reply_to]
msg_vals = {
'name': subject,
@ -257,7 +259,8 @@ class email_message(osv.osv):
'email_cc': email_cc and ','.join(email_cc) or '',
'email_bcc': email_bcc and ','.join(email_bcc) or '',
'reply_to': reply_to and ','.join(reply_to) or '',
'message_id': openobject_id,
'res_id':openobject_id,
#'message_id': message_id,
'sub_type': subtype or '',
'headers': x_headers or False,
'priority': priority,
@ -307,7 +310,7 @@ class email_message(osv.osv):
smtp_ids = smtp_server_obj.search(cr, uid, [('default','=',True)])
if smtp_ids:
smtp_server = smtp_server_obj.browse(cr, uid, smtp_ids, context)[0]
tools.email_send(message.email_from,
res = tools.email_send(message.email_from,
message.email_to and message.email_to.split(',') or [],
message.name, message.description,
email_cc=message.email_cc and message.email_cc.split(',') or [],
@ -323,9 +326,14 @@ class email_message(osv.osv):
ssl=smtp_account and smtp_account.smtpssl or False,
smtp_user=smtp_account and smtp_account.smtpuname or None,
smtp_password=smtp_account and smtp_account.smtppass or None)
if res:
self.write(cr, uid, [message.id], {'state':'sent'}, context)
else:
self.write(cr, uid, [message.id], {'state':'exception'}, context)
except Exception, error:
logger = netsvc.Logger()
logger.notifyChannel("email-template", netsvc.LOG_ERROR, _("Sending of Mail %s failed. Probable Reason:Could not login to server\nError: %s") % (message.id, error))
self.write(cr, uid, [message.id], {'state':'exception'}, context)
return ids
# OLD Code.

View File

@ -34,11 +34,13 @@
<field name="res_id" readonly="1"/>
<button name="open_document" string="Open Document" type="object" icon="gtk-jump-to"/>
<field name="message_id"/>
<button name="process_queue" string="Process Queue" type="object" icon="gtk-execute"/>
</group>
</group>
<separator string="Description" colspan="4"/>
<field name="description" nolabel="1" colspan="4"/>
<separator string="" colspan="4"/>
<field name="state"/>
<button name="process_queue" string="Send" type="object" icon="gtk-execute" states='draft,waiting'/>
</page>
<page string="Attachments">
<separator string="Attachments" colspan="4"/>
@ -47,7 +49,6 @@
<page string="Advanced">
<group col="4" colspan="4">
<field name="folder"/>
<field name="state"/>
<field name="sub_type"/>
<field name="headers"/>
<field name="priority"/>