smtp_per_user.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Odoo, Open Source Management Solution
  5. #
  6. # Author: Andrius Laukavičius. Copyright: JSC Boolit
  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 openerp import models, fields, api
  23. from email.utils import parseaddr, formataddr
  24. class ir_mail_server(models.Model):
  25. _inherit = "ir.mail_server"
  26. user_id = fields.Many2one('res.users', string="Owner")
  27. email_name = fields.Char('Email Name', help="Overrides default email name")
  28. force_use = fields.Boolean('Force Use', help="If checked and this server is chosen to send mail message, It will ignore owners mail server")
  29. @api.model
  30. def replace_email_name(self, old_email):
  31. """
  32. Replaces email name if new one is provided
  33. """
  34. if self.email_name:
  35. old_name, email = parseaddr(old_email)
  36. return formataddr((self.email_name, email))
  37. else:
  38. return old_email
  39. class mail_mail(models.Model):
  40. _inherit = 'mail.mail'
  41. @api.multi
  42. def send(self, auto_commit=False, raise_exception=False):
  43. ir_mail_server_obj = self.env['ir.mail_server']
  44. res_user_obj = self.env['res.users']
  45. for email in self:
  46. if not email.mail_server_id.force_use:
  47. user = res_user_obj.search([('partner_id', '=', email.author_id.id)], limit=1)
  48. if user:
  49. mail_server = ir_mail_server_obj.search([('user_id', '=', user.id)], limit=1)
  50. if mail_server:
  51. email.mail_server_id = mail_server.id
  52. email.email_from = email.mail_server_id.replace_email_name(email.email_from)
  53. return super(mail_mail, self).send(auto_commit=False, raise_exception=False)