mailt/smtpt

254 lines
7.2 KiB
Python
Executable File

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# smtpt -- Simple command-line tool to test SMTP server
# By: Benjamin RENARD <brenard@easter-eggs.com>
#
# Copyright (C) 2011 Easter-eggs
# http://git.zionetrix.net/mailt
#
# 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 <http://www.gnu.org/licenses/>.
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 getpass
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="The attached file contain the EICAR standard antivirus test string."
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['Date'] = email.utils.formatdate(None,True)
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.virus:
part = MIMEBase('application', "octet-stream")
part.set_payload("X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*")
part.add_header('Content-Disposition', 'attachment; filename="eicar.bin"')
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
if not options.password:
options.password=getpass.getpass()
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()