odoo-mailgate.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python2
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. #
  4. # odoo-mailgate
  5. #
  6. # This program will read an email from stdin and forward it to odoo. Configure
  7. # a pipe alias in your mail server to use it, postfix uses a syntax that looks
  8. # like:
  9. #
  10. # email@address: "|/home/odoo/src/odoo-mail.py"
  11. #
  12. # while exim uses a syntax that looks like:
  13. #
  14. # *: |/home/odoo/src/odoo-mail.py
  15. #
  16. # Note python2 was chosen on purpose for backward compatibility with old mail
  17. # servers.
  18. #
  19. import optparse
  20. import sys
  21. import traceback
  22. import xmlrpclib
  23. def main():
  24. op = optparse.OptionParser(usage='usage: %prog [options]', version='%prog v1.2')
  25. op.add_option("-d", "--database", dest="database", help="Odoo database name (default: %default)", default='odoo')
  26. op.add_option("-u", "--userid", dest="userid", help="Odoo user id to connect with (default: %default)", default=1, type=int)
  27. op.add_option("-p", "--password", dest="password", help="Odoo user password (default: %default)", default='admin')
  28. op.add_option("--host", dest="host", help="Odoo host (default: %default)", default='localhost')
  29. op.add_option("--port", dest="port", help="Odoo port (default: %default)", default=8069, type=int)
  30. (o, args) = op.parse_args()
  31. try:
  32. msg = sys.stdin.read()
  33. models = xmlrpclib.ServerProxy('http://%s:%s/xmlrpc/2/object' % (o.host, o.port), allow_none=True)
  34. models.execute_kw(o.database, o.userid, o.password, 'mail.thread', 'message_process', [False, xmlrpclib.Binary(msg)], {})
  35. except xmlrpclib.Fault as e:
  36. # reformat xmlrpc faults to print a readable traceback
  37. err = "xmlrpclib.Fault: %s\n%s" % (e.faultCode, e.faultString)
  38. sys.exit(err)
  39. except Exception as e:
  40. traceback.print_exc(None, sys.stderr)
  41. sys.exit(2)
  42. if __name__ == '__main__':
  43. main()