bdb3eca543d2d089268d60c6c3fa52748c435631
bn8 Initial commit

bn8 authored 11 years ago

1) #! /usr/bin/env python
2) # -*- coding: utf-8 -*-
3) 
4) # smtpt -- Simple command-line tool to test SMTP server
5) # By: Benjamin RENARD <brenard@easter-eggs.com>
6) #
7) # Copyright (C) 2011 Easter-eggs
Benjamin Renard Fixed project URL

Benjamin Renard authored 11 years ago

8) # http://git.zionetrix.net/mailt
bn8 Initial commit

bn8 authored 11 years ago

9) #
10) # This file is part of MailT.
11) #
12) # MailT is free software; you can redistribute it and/or modify
13) # it under the terms of the GNU General Public License as published
14) # by the Free Software Foundation, either version 3 of the License,
15) # or (at your option) any later version.
16) #
17) # MailT is distributed in the hope that it will be useful,
18) # but WITHOUT ANY WARRANTY; without even the implied warranty of
19) # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20) # GNU Affero General Public License for more details.
21) #
22) # You should have received a copy of the GNU General Public License
23) # along with this program.  If not, see <http://www.gnu.org/licenses/>.
24) 
25) from optparse import OptionParser
26) 
27) import os
28) import smtplib
29) import email.utils
30) from email.MIMEText import MIMEText
31) from email.MIMEMultipart import MIMEMultipart
32) from email.MIMEBase import MIMEBase
33) from email import Encoders
34) 
35) import logging
36) import sys
37) 
38) parser = OptionParser()
39) 
40) default_subject="Simple test message"
41) 
42) parser.add_option('-s',
43)                   '--subject',
44)                   action="store",
45)                   type="string",
46)                   dest="subject",
47)                   help="The subject",
48)                   default=default_subject)
49) 
50) parser.add_option('-f',
51)                   '--from',
52)                   action="store",
53)                   type="string",
54)                   dest="author",
55)                   help="The sender email",
56)                   default="author@example.com")
57) 
58) parser.add_option('-t',
59)                   '--to',
60)                   action="store",
61)                   type="string",
62)                   dest="to",
63)                   help="The recipient email",
64)                   default="recipient@example.com")
65) 
66) parser.add_option('-c',
67)                   '--content',
68)                   action="store",
69)                   type="string",
70)                   dest="content",
71)                   help="The content",
72)                   default="This is the body of the message.")
73) 
74) parser.add_option('-H',
75)                   '--host',
76)                   action="store",
77)                   type="string",
78)                   dest="host",
79)                   help="The SMTP host",
80)                   default="127.0.0.1")
81) 
82) parser.add_option('-p',
83)                   '--port',
84)                   action="store",
85)                   type="string",
86)                   dest="port",
87)                   help="The SMTP port",
88)                   default=None)
89) 
90) parser.add_option('-S',
91)                   '--ssl',
92)                   action="store_true",
93)                   dest="ssl",
94)                   help="SMTP Over SSL (SMTPS)",
95)                   default=False)
96) 
97) parser.add_option('-T',
98)                   '--starttls',
99)                   action="store_true",
100)                   dest="starttls",
101)                   help="Use STARTTLS",
102)                   default=False)
103) 
104) parser.add_option('-U',
105)                   '--user',
106)                   action="store",
107)                   type="string",
108)                   dest="user",
109)                   help="SMTP Auth - User login",
110)                   default=None)
111) 
112) parser.add_option('-P',
113)                   '--password',
114)                   action="store",
115)                   type="string",
116)                   dest="password",
117)                   help="SMTP Auth - User password",
118)                   default=None)
119) 
120) parser.add_option('-a',
121)                   '--attachement',
122)                   action="append",
123)                   type="string",
124)                   dest="attach",
125)                   help="The attachment(s) file(s) path",
126)                   default=None)
127) 
128) parser.add_option('-d',
129)                   '--debug',
130)                   action="store_true",
131)                   dest="debug")
132) 
133) parser.add_option('-v',
134)                   '--verbose',
135)                   action="store_true",
136)                   dest="verbose")
137) 
138) parser.add_option('--spam',
139)                   action="store_true",
140)                   dest="spam")
141) 
142) parser.add_option('--virus',
143)                   action="store_true",
144)                   dest="virus")
145) 
146) 
147) (options, args) = parser.parse_args()
148) 
149) if options.spam:
150) 	if options.subject == default_subject:
151) 		options.subject = "Test spam message"
152) 	options.content="""This is the GTUBE, the
153) 	Generic
154) 	Test for
155) 	Unsolicited
156) 	Bulk
157) 	Email
158) 
159) If your spam filter supports it, the GTUBE provides a test by which you
160) can verify that the filter is installed correctly and is detecting incoming
161) spam. You can send yourself a test mail containing the following string of
162) characters (in upper case and with no white spaces and line breaks):
163) 
164) XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X
165) 
166) You should send this test mail from an account outside of your network."""
167) elif options.virus:
168) 	if options.subject == default_subject:
169) 		options.subject = "Test virus message"
Benjamin Renard Fixed smtpt virus message

Benjamin Renard authored 11 years ago

170) 	options.content="The attached file contain the EICAR standard antivirus test string."
bn8 Initial commit

bn8 authored 11 years ago

171) 
172) if not options.port:
173) 	if options.ssl:
174) 		options.port=465
175) 	else:
176) 		options.port=25
177) 
178) if options.debug:
179) 	loglevel=logging.DEBUG
180) elif options.verbose:
181) 	loglevel=logging.INFO
182) else:
183) 	loglevel=logging.WARNING
184) 
185) logging.basicConfig(level=loglevel,format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
186) 
187) # Create the message
188) msg = MIMEMultipart()
189) msg['To'] = email.utils.formataddr(('Recipient', options.to))
190) logging.info("To : %s" % options.to)
191) msg['From'] = email.utils.formataddr(('Author', options.author))
192) logging.info("From : %s" % options.author)
193) msg['Subject'] = options.subject
194) logging.info("Subject : %s" % options.subject)
Benjamin Renard Add required Date header

Benjamin Renard authored 8 years ago

195) msg['Date'] = email.utils.formatdate(None,True)
bn8 Initial commit

bn8 authored 11 years ago

196) 
197) 
198) msg.attach( MIMEText(options.content) )
199) 
200) if options.attach:
201) 	for filepath in options.attach:
202) 		logging.info("Attache file %s" % filepath)
203) 		part = MIMEBase('application', "octet-stream")
204) 		part.set_payload( open(filepath,"rb").read() )
205) 		Encoders.encode_base64(part)
206) 		part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filepath))
207) 		msg.attach(part)
208) 
Benjamin Renard Fixed smtpt virus message

Benjamin Renard authored 11 years ago

209) if options.virus:
210) 	part = MIMEBase('application', "octet-stream")
211) 	part.set_payload("X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*")
212) 	part.add_header('Content-Disposition', 'attachment; filename="eicar.bin"')
213) 	msg.attach(part)
214)