[IMP] improve email_send method and pass the smtp related all argument in email module. add column in email.message object and related improvement.

bzr revid: ysa@tinyerp.com-20110203065913-q33pek0p4puxvbyy
This commit is contained in:
YSA (Openerp) 2011-02-03 12:29:13 +05:30
parent 22058cbe69
commit 492bd0eea5
4 changed files with 22 additions and 8 deletions

View File

@ -151,6 +151,7 @@ class email_message(osv.osv):
'sub_type': fields.char('Sub Type', size=32, readonly=True),
'x_headers': fields.char('x_headers',size=256, readonly=True),
'priority':fields.integer('Priority', readonly=True),
'debug':fields.boolean('Debug', readonly=True),
#I like GMAIL which allows putting same mail in many folders
#Lets plan it for 0.9
'folder':fields.selection([
@ -215,19 +216,30 @@ class email_message(osv.osv):
context = {}
attachment_obj = self.pool.get('ir.attachment')
account_obj = self.pool.get('email.smtp_server')
filters = [('folder', '=', 'outbox'), ('state', '!=', 'sending')]
if 'filters' in context:
filters.extend(context['filters'])
ids = self.search(cr, uid, filters, context=context)
self.write(cr, uid, ids, {'state':'sending', 'folder':'sent'}, context)
if not ids:
filters = [('folder', '=', 'outbox'), ('state', '!=', 'sending')]
if 'filters' in context:
filters.extend(context['filters'])
ids = self.search(cr, uid, filters, context=context)
self.write(cr, uid, ids, {'state':'sending', 'folder':'sent'}, context)
for message in self.browse(cr, uid, ids, context):
try:
attachments = []
for attach in message.attachment_ids:
attachments.append((attach.datas_fname ,attach.datas))
smtp_account = False
if message.account_id:
account_id = smtp_account
else:
smtp_ids = account_obj.search(cr, uid, [('default','=',True)])
if smtp_ids:
smtp_account = account_obj.browse(cr, uid, smtp_ids, context)[0]
tools.email_send(message.email_from, message.email_to, message.name, message.message, email_cc=message.email_cc,
email_bcc=message.email_bcc, reply_to=message.reply_to, attach=attachments, openobject_id=message.message_id,
subtype=message.sub_type, x_headers=message.x_headers, priority=message.priority)
subtype=message.sub_type, x_headers=message.x_headers, priority=message.priority, debug=message.debug,
smtp_email_from=smtp_account and smtp_account.email_id or None, smtp_server=smtp_account and smtp_account.smtpserver or None,
smtp_port=smtp_account and smtp_account.smtpport or None, smtp_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)
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))

View File

@ -48,6 +48,7 @@
<field name="sub_type"/>
<field name="x_headers"/>
<field name="priority"/>
<field name="debug"/>
</page>
</notebook>
</form>

View File

@ -157,7 +157,7 @@ unless it is already specified in the From Email, e.g: John Doe <john@doe.com>",
return [(a["id"], "%s (%s)" % (a['email_id'], a['name'])) for a in self.read(cr, uid, ids, ['name', 'email_id'], context=context)]
def email_send(cr, uid, email_from, email_to, subject, body, model=False, email_cc=None, email_bcc=None, reply_to=False, attach=None,
openobject_id=False, subtype='plain', x_headers=None, priority='3', smtp_id=False):
openobject_id=False, debug=False, subtype='plain', x_headers=None, priority='3', smtp_id=False):
attachment_obj = self.pool.get('ir.attachment')
email_msg_obj = self.pool.get('email.message')
msg_vals = {
@ -176,6 +176,7 @@ unless it is already specified in the From Email, e.g: John Doe <john@doe.com>",
'sub_type': subtype or '',
'x_headers': x_headers or '',
'priority': priority,
'debug': debug,
'folder': 'outbox',
'state': 'waiting',
}

View File

@ -658,7 +658,7 @@ This is useful for CRM leads for example"),
context=context
)
# TODO : manage return value of all the records
result = self.pool.get('email.message').send_this_mail(cursor, user, [mailbox_id], context)
result = self.pool.get('email.message').email_send(cursor, user, [mailbox_id], context)
return result
email_template()