Created
January 14, 2013 14:38
-
-
Save kryskool/4530492 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
############################################################################## | |
# | |
# smtp_tools module for OpenERP, SMTP to file for test | |
# Copyright (C) 2010 SYLEAM (<http://www.syleam.fr/>) | |
# Christophe CHAUVET <[email protected]> | |
# | |
# This file is a part of smtp_tools | |
# | |
# smtp_tools is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# smtp_tools is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
############################################################################## | |
from datetime import datetime | |
from smtpd import SMTPServer | |
import logging | |
import asyncore | |
import subprocess | |
import os | |
smtp_port = 10025 | |
cur_path = os.getcwd() | |
logging.basicConfig(level=logging.WARNING) | |
logger = logging.getLogger('smtp.simulator') | |
logger.setLevel(logging.INFO) | |
class EmlServer(SMTPServer): | |
""" | |
Emulate a SMTP Server, and store email file in the current directory | |
""" | |
no = 0 | |
def process_message(self, peer, mailfrom, rcpttos, data): | |
filename = '%s-%d.eml' % (datetime.now().strftime('%Y%m%d%H%M%S'), | |
self.no) | |
f = open(filename, 'w') | |
f.write(data) | |
f.close() | |
logger.info('%s saved.' % filename) | |
cmd = ['thunderbird', '%s' % os.path.join(cur_path, filename)] | |
retcode = subprocess.call(' '.join(cmd), shell=True, cwd='.') | |
if not retcode: | |
logger.error('[ERROR] Return code %d' % retcode) | |
self.no += 1 | |
def run(): | |
logger.info('Start SMTP Server on localhost (port %s)' % str(smtp_port)) | |
foo = EmlServer(('localhost', smtp_port), None) | |
try: | |
asyncore.loop() | |
except KeyboardInterrupt: | |
pass | |
if __name__ == '__main__': | |
run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment