wiz_send_email.py.svn-base 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
  6. # Copyright (C) 2011-2012 Serpent Consulting Services (<http://www.serpentcs.com>)
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  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 Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from osv import fields, osv
  23. import email
  24. from email.header import decode_header
  25. from email.message import Message
  26. class email_template(osv.osv):
  27. _inherit="email.template"
  28. def generate_email(self, cr, uid, template_id, res_id, context=None):
  29. if context is None:
  30. context = {}
  31. ret= super(email_template,self).generate_email(cr, uid, template_id, res_id, context=context)
  32. if context.get('body_text',False) or context.get('subject',False) or context.get('email_to',False):
  33. ret['body_text']=context['body_text']
  34. ret['subject']=context['subject']
  35. ret['email_to']=context['email_to']
  36. return ret
  37. else:
  38. return ret
  39. #
  40. email_template()
  41. class send_email(osv.osv):
  42. _name = "send.email"
  43. _columns = {
  44. 'note':fields.text('Text',required=True),
  45. }
  46. def send_email(self, cr, uid, ids, context=None):
  47. subject = 'Emergency mail'
  48. body=''
  49. email_template=self.pool.get('email.template')
  50. template_id=email_template.search(cr,uid,[('model_id.model','=','student.student')],context=context)
  51. email_template_brw = email_template.browse(cr,uid,template_id[0])
  52. for i in self.browse(cr,uid,ids):
  53. body = i.note
  54. for student_brw in self.pool.get('student.student').browse(cr,uid,context['active_ids']):
  55. for emergency_contect_brw in student_brw.emergency_contact_ids:
  56. context['body_text']=body
  57. context['subject']=subject
  58. context['email_to']=emergency_contect_brw.email
  59. email_template.send_mail(cr, uid ,email_template_brw.id,ids[0],force_send=True,context=context)
  60. return {'type': 'ir.actions.act_window_close'}
  61. send_email()