fetchmail_server.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # -*- coding: utf-8 -*-
  2. from imaplib import IMAP4, IMAP4_SSL
  3. from poplib import POP3, POP3_SSL
  4. from openerp import tools, models, fields, api
  5. MAIL_TIMEOUT = 60
  6. class vieterp_fetchmail_server(models.Model):
  7. _inherit = 'fetchmail.server'
  8. _name = 'fetchmail.server.inbox'
  9. @api.model
  10. def _def_object_id(self):
  11. model_obj = self.env['ir.model']
  12. model = model_obj.search([('model', '=', 'mail.inbox')], limit = 1)
  13. if model:
  14. return model[0].id
  15. return False
  16. source_id = fields.Many2one('mail.server.source', string='Mail Source')
  17. object_id = fields.Many2one('ir.model', string="Create a New Record", default=_def_object_id)
  18. @api.multi
  19. def fetch_mail(self):
  20. result = super(vieterp_fetchmail_server, self).fetch_mail()
  21. return True
  22. @api.multi
  23. def connect(self):
  24. if self.source_id and self.source_id.id:
  25. self.ensure_one()
  26. if self.source_id.type == 'imap':
  27. if self.source_id.is_ssl:
  28. connection = IMAP4_SSL(self.source_id.server, int(self.source_id.port))
  29. else:
  30. connection = IMAP4(self.source_id.server, int(self.source_id.port))
  31. connection.login(self.user, self.password)
  32. elif self.type == 'pop':
  33. if self.source_id.is_ssl:
  34. connection = POP3_SSL(self.source_id.server, int(self.source_id.port))
  35. else:
  36. connection = POP3(self.source_id.server, int(self.source_id.port))
  37. # TODO: use this to remove only unread messages
  38. # connection.user("recent:"+server.user)
  39. connection.user(self.user)
  40. connection.pass_(self.password)
  41. # Add timeout on socket
  42. connection.sock.settimeout(MAIL_TIMEOUT)
  43. return connection
  44. return super(vieterp_fetchmail_server, self).connect()