commit bb969b9c9c5547f5f3e3df5a42c8ee0ca443a0f5 Author: bn8 Date: Wed Apr 10 21:29:01 2013 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bdc5af0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*~ +build diff --git a/debian/.gitignore b/debian/.gitignore new file mode 100644 index 0000000..d6bb4c6 --- /dev/null +++ b/debian/.gitignore @@ -0,0 +1,4 @@ +mailt.*debhelper* +mailt.substvars +mailt +files diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..7a8d4c3 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,6 @@ +mailt (1.1-1) unstable; urgency=low + + * Initial release + + -- Benjamin Renard Wed, 10 Apr 2013 21:17:35 +0200 + diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..7f8f011 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +7 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..93ca67e --- /dev/null +++ b/debian/control @@ -0,0 +1,12 @@ +Source: mailt +Priority: extra +Section: net +Maintainer: Benjamin Renard +Build-Depends: debhelper (>= 7.0.50~), python-all-dev, python-support (>= 0.90.0~), python-setuptools +Homepage: http://git.zionetrix.net/mailt +Vcs-git: http://git.zionetrix.net/mailt + +Package: mailt +Architecture: all +Depends: ${python:Depends}, ${misc:Depends} +Description: Simple command-line tool to test SMTP/IMAP mail server diff --git a/debian/copyright b/debian/copyright new file mode 100644 index 0000000..5778c40 --- /dev/null +++ b/debian/copyright @@ -0,0 +1,41 @@ +This work was packaged for Debian by: + + Benjamin Renard on Fri, 06 Apr 2012 18:22:16 +0200 + +It was downloaded from Git repository of project : + + http://git.zionetrix.net/mailt + +Upstream Author: + + Benjamin Renard + +Copyright: + + Copyright (C) 2013 Benjamin Renard + +License: + + This program 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. + + This program 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, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +On Debian systems, the complete text of the GNU General Public license version 3.0 +can be found in "/usr/share/common-licenses/GPL-3". + +The Debian packaging is: + + Copyright (C) 2013 Benjamin Renard + +and is licensed under the GNU General Public License version 3.0 +see "/usr/share/common-licenses/GPL-3". diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..6af83a7 --- /dev/null +++ b/debian/rules @@ -0,0 +1,5 @@ +#!/usr/bin/make -f + +%: + dh $@ + diff --git a/imapt b/imapt new file mode 100755 index 0000000..31ed1d9 --- /dev/null +++ b/imapt @@ -0,0 +1,154 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +# imapt -- Simple command-line tool to test IMAP server +# By: Benjamin RENARD +# +# Copyright (C) 2013 Easter-eggs +# http://git.zionetrix.net/pyimapt +# +# This file is part of MailT. +# +# MailT 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. +# +# MailT 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 Affero 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 . + +from optparse import OptionParser + +import os +import imaplib + +import logging +import sys + +parser = OptionParser() + + +parser.add_option('-H', + '--host', + action="store", + type="string", + dest="host", + help="The IMAP host", + default="127.0.0.1") + +parser.add_option('-p', + '--port', + action="store", + type="string", + dest="port", + help="The IMAP port", + default=None) + +parser.add_option('-S', + '--ssl', + action="store_true", + dest="ssl", + help="IMAP Over SSL (IMAPS)", + default=False) + +parser.add_option('-U', + '--user', + action="store", + type="string", + dest="user", + help="User login", + default=None) + +parser.add_option('-P', + '--password', + action="store", + type="string", + dest="password", + help="User password", + default=None) + +parser.add_option('--md5', + action="store_true", + dest="md5", + help="Use CRAM-MD5 password for authentication", + default=False) + +parser.add_option('-m', + '--mailbox', + action="store", + type="string", + dest="mailbox", + help="The mailbox directory to select", + default='INBOX') + +parser.add_option('-v', + '--verbose', + action="store_true", + dest="verbose") + +(options, args) = parser.parse_args() + +if options.verbose: + loglevel=logging.DEBUG +else: + loglevel=logging.INFO + +logging.basicConfig(level=loglevel,format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') + +if not options.user or not options.password: + logging.fatal('You must provide the user login and password') + sys.exit(1) + +if options.port is None: + if options.ssl: + options.port=993 + else: + options.port=143 + +if options.ssl: + logging.debug('Establish SSL connexion to server') + try: + server = imaplib.IMAP4_SSL(options.host,options.port) + logging.debug('Connected') + except Exception, e: + logging.critical("IMAP connection error : %s" % e) + sys.exit(2) +else: + logging.debug("Establish connexion to server") + try: + server = imaplib.IMAP4(options.host,options.port) + logging.debug('Connected') + except Exception, e: + logging.critical("IMAP connection error : %s" % e) + sys.exit(2) + +if options.user: + try: + if options.md5: + logging.debug('Login into server as %s (with CRAM-MD5 password)' % options.user) + server.login_cram_md5(options.user,options.password) + else: + logging.debug('Login into server as %s' % options.user) + server.login(options.user,options.password) + logging.debug('Logged') + except Exception as e: + logging.critical("IMAP Auth error : %s" % e) + sys.exit(3) + +try: + logging.debug("Select mailbox") + ret = server.select(options.mailbox) + if len(ret)>0 and ret[0]=='OK': + logging.info("Mailbox %s content %s message(s)" % (options.mailbox,ret[1][0])) + else: + logging.error("Selecting return incorrected : %s" % ret) +except Exception as e: + logging.critical('Error selecting mailbox %s : %s' % (option.mailbox,e)) +finally: + server.close() + server.logout() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..d8c58ff --- /dev/null +++ b/setup.py @@ -0,0 +1,39 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +# MailT -- Simple command-line tool to test SMTP/IMAP mail server +# By: Benjamin RENARD +# +# Copyright (C) 2011 Easter-eggs +# http://git.zionetrix.net/pysmtpt +# +# This file is part of MailT. +# +# MailT 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. +# +# MailT 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 Affero 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 . + +import os +import imp +from distutils.core import setup + +setup( + name = "MailT", + version = "1.1", + license = "http://www.gnu.org/licenses/gpl.html", + description = "MailT is a simple command-line tool to test SMTP/IMAP mail server.", + author = "Benjamin Renard", + author_email = "brenard@easter-eggs.com", + url = "http://git.zionetrix.net/mailt", + keywords = "command-line smtp imap tool test", + scripts = ["smtpt","imapt"] +) diff --git a/smtpt b/smtpt new file mode 100755 index 0000000..a8e1fcc --- /dev/null +++ b/smtpt @@ -0,0 +1,245 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +# smtpt -- Simple command-line tool to test SMTP server +# By: Benjamin RENARD +# +# Copyright (C) 2011 Easter-eggs +# http://git.zionetrix.net/pysmtpt +# +# This file is part of MailT. +# +# MailT 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. +# +# MailT 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 Affero 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 . + +from optparse import OptionParser + +import os +import smtplib +import email.utils +from email.MIMEText import MIMEText +from email.MIMEMultipart import MIMEMultipart +from email.MIMEBase import MIMEBase +from email import Encoders + +import logging +import sys + +parser = OptionParser() + +default_subject="Simple test message" + +parser.add_option('-s', + '--subject', + action="store", + type="string", + dest="subject", + help="The subject", + default=default_subject) + +parser.add_option('-f', + '--from', + action="store", + type="string", + dest="author", + help="The sender email", + default="author@example.com") + +parser.add_option('-t', + '--to', + action="store", + type="string", + dest="to", + help="The recipient email", + default="recipient@example.com") + +parser.add_option('-c', + '--content', + action="store", + type="string", + dest="content", + help="The content", + default="This is the body of the message.") + +parser.add_option('-H', + '--host', + action="store", + type="string", + dest="host", + help="The SMTP host", + default="127.0.0.1") + +parser.add_option('-p', + '--port', + action="store", + type="string", + dest="port", + help="The SMTP port", + default=None) + +parser.add_option('-S', + '--ssl', + action="store_true", + dest="ssl", + help="SMTP Over SSL (SMTPS)", + default=False) + +parser.add_option('-T', + '--starttls', + action="store_true", + dest="starttls", + help="Use STARTTLS", + default=False) + +parser.add_option('-U', + '--user', + action="store", + type="string", + dest="user", + help="SMTP Auth - User login", + default=None) + +parser.add_option('-P', + '--password', + action="store", + type="string", + dest="password", + help="SMTP Auth - User password", + default=None) + +parser.add_option('-a', + '--attachement', + action="append", + type="string", + dest="attach", + help="The attachment(s) file(s) path", + default=None) + +parser.add_option('-d', + '--debug', + action="store_true", + dest="debug") + +parser.add_option('-v', + '--verbose', + action="store_true", + dest="verbose") + +parser.add_option('--spam', + action="store_true", + dest="spam") + +parser.add_option('--virus', + action="store_true", + dest="virus") + + +(options, args) = parser.parse_args() + +if options.spam: + if options.subject == default_subject: + options.subject = "Test spam message" + options.content="""This is the GTUBE, the + Generic + Test for + Unsolicited + Bulk + Email + +If your spam filter supports it, the GTUBE provides a test by which you +can verify that the filter is installed correctly and is detecting incoming +spam. You can send yourself a test mail containing the following string of +characters (in upper case and with no white spaces and line breaks): + +XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X + +You should send this test mail from an account outside of your network.""" +elif options.virus: + if options.subject == default_subject: + options.subject = "Test virus message" + options.content="""This is the EICAR standard antivirus test string : + +X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*""" + +if not options.port: + if options.ssl: + options.port=465 + else: + options.port=25 + +if options.debug: + loglevel=logging.DEBUG +elif options.verbose: + loglevel=logging.INFO +else: + loglevel=logging.WARNING + +logging.basicConfig(level=loglevel,format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') + +# Create the message +msg = MIMEMultipart() +msg['To'] = email.utils.formataddr(('Recipient', options.to)) +logging.info("To : %s" % options.to) +msg['From'] = email.utils.formataddr(('Author', options.author)) +logging.info("From : %s" % options.author) +msg['Subject'] = options.subject +logging.info("Subject : %s" % options.subject) + + +msg.attach( MIMEText(options.content) ) + +if options.attach: + for filepath in options.attach: + logging.info("Attache file %s" % filepath) + part = MIMEBase('application', "octet-stream") + part.set_payload( open(filepath,"rb").read() ) + Encoders.encode_base64(part) + part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filepath)) + msg.attach(part) + +if options.ssl: + logging.info("Establish SSL connexion to server") + server = smtplib.SMTP_SSL(options.host,options.port) +else: + logging.info("Establish connexion to server") + server = smtplib.SMTP(options.host,options.port) + + if options.starttls: + try: + logging.info("Start TLS") + server.starttls() + except Exception as e: + logging.critical('Error using STARTTLS : %s' % e) + server.quit() + sys.exit(2) + +if options.debug: + logging.info("Establish connexion to server") + server.set_debuglevel(True) + +if options.user: + error = None + try: + server.login(options.user,options.password) + except Exception as e: + logging.critical("SMTP Auth error : %s" % error) + server.quit() + sys.exit(3) + +try: + logging.info("Sending email") + server.sendmail(options.author, [options.to], msg.as_string()) +except Exception as e: + logging.critical('Error sending email : %s' % e) +finally: + server.quit()