source: foomailer/foomail.py@ 400

Last change on this file since 400 was 400, checked in by Rick van der Zwet, 9 years ago

Add port to testing submission for example

  • Property svn:executable set to *
File size: 5.0 KB
Line 
1#!/usr/bin/env python
2# Rick van der Zwet
3# TODO: May need to implement some RFC checkers
4#
5#
6import sys,smtplib,re,socket,random,string,os,getopt
7import mimetypes
8import time
9from time import gmtime,strftime
10from email.MIMEMultipart import MIMEMultipart
11from email.MIMEBase import MIMEBase
12from email.MIMEText import MIMEText
13from email.Utils import COMMASPACE, formatdate
14from email import Encoders
15
16__svnversion__ = "$Id: foomail.py 633 2008-06-12 19:29:56Z rick $"
17__author__ = "Rick van der Zwet"
18
19
20def usage():
21 print "usage %s -f <from> -r <recipient> -s <server> [--spam]" % sys.argv[0]
22 print "[--virus] [--file <size in kB>] [--message <file>]"
23 print "[--subject <string>] [--to <string>] [--cc <string>]"
24 print "[--force]"
25
26default_domain = 'example.com'
27def emailparse(email):
28 if re.compile('.*@.*').match(email):
29 return(email)
30 else:
31 return(email + '@' + default_domain)
32
33try:
34 opts, args = getopt.getopt(sys.argv[1:], "f:r:s:dp:",
35 ["from=","force","recipient=","server=","spam","port=","virus","files=","dry-run","message=","subject=","to=","cc="])
36except getopt.GetoptError:
37 usage()
38 sys.exit(128)
39
40spam = False
41virus = False
42server = False
43port = 25
44recipient = False
45sender = False
46files = False
47force = False
48dry = False
49message = False
50subject = False
51to = False
52cc = False
53
54tests = ""
55
56for o, a in opts:
57 if o in ("-f","--from"):
58 sender_raw = a
59 sender = emailparse(a)
60 elif o in ( "-r", "--recipient"):
61 recipient_raw = a
62 recipient = emailparse(a)
63 elif o in ("-s", "--server"):
64 server = a
65 elif o in ("-p", "--portr"):
66 port = a
67 elif o == "--spam":
68 spam = True
69 tests += " Spam"
70 elif o == "--virus":
71 virus = True
72 tests += " Virus"
73 elif o == "--files":
74 files = a
75 elif o in ("-d","--dry-run"):
76 dry = True
77 elif o in ("--message"):
78 message = a
79 elif o in ("--subject"):
80 subject = a
81 elif o in ("--to"):
82 to = a
83 elif o in ("--cc"):
84 cc = a
85 elif o in ("--force"):
86 force = True;
87
88error = False
89if not sender:
90 print "No sender defined"
91 error = True
92elif not recipient:
93 print "No recipient defined"
94 error = True
95elif not server:
96 print "No server defined"
97 error = True
98
99if error:
100 usage()
101 sys.exit(128)
102
103if force:
104 sender = sender_raw
105 recipient = recipient_raw
106
107
108#Subject here, just to keep the settings/layout a bit more constent
109if not subject:
110 subject = 'Test email send at %a, %d %b %Y %T %z'
111subject = strftime(subject,gmtime())
112
113if not to:
114 to = 'Testing received <' + recipient + '>'
115 to = recipient
116
117email = MIMEMultipart()
118hostname = socket.gethostname()
119
120email['From'] = 'Testing sender <' + sender + '>'
121email['To'] = to
122if cc:
123 email['Cc'] = cc
124email['X-Server'] = server
125email['X-Port'] = port
126email['X-Hostname'] = hostname
127email['Message-ID' ] = '<' +''.join(random.sample(string.letters +
128 string.digits,10)) + '@' + email['X-Hostname'] + '>'
129email['Date'] = strftime('%a, %d %b %Y %T %z',gmtime())
130email['Subject'] = subject
131email['X-Version'] = __svnversion__
132email['X-Author'] = __author__
133email['X-Tests'] = tests
134#email['Reply-To'] = "Rick vd Zwet <rick@joost.com>, Wouter Simons <wouter@joost.com>"
135if spam:
136 email['X-Spam'] = 'XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X'
137
138
139def defaultmessage():
140 msg = ''
141 msg += \
142 '''
143Hi,
144
145 Sorry to bother you...
146
147Please ignore this message as it is used to test
148and verify systems to make sure everything works
149flawless.
150
151%(virus)s
152
153Technical details:\n''' % email
154
155 for k in email.keys():
156 v = email[k]
157 msg += "* %-15s : %s\n" % (k,v)
158
159
160 msg = msg + \
161 '''
162Best regards,
163Your system administrator
164--
165BSD Licensed Source Code for this tool at:
166https://rickvanderzwet.nl/svn/personal/foomailer
167 '''
168 return(msg)
169
170if message:
171 f = open(message,'r')
172 msg = f.read()
173 f.close()
174else:
175 msg = defaultmessage()
176
177email.attach(MIMEText(msg))
178
179if virus:
180 part = MIMEBase('application', "octet-stream")
181 part.set_payload('X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*')
182 Encoders.encode_base64(part)
183 part.add_header('Content-Disposition', 'attachment; filename="eicar.txt"')
184 email.attach(part)
185
186if files:
187 for file in files.split(':'):
188 mimetype = mimetypes.guess_type(file)[0].split('/')
189 part = MIMEBase(mimetype[0],mimetype[1])
190 f = open(file,'r')
191 part.set_payload(f.read())
192 f.close()
193 Encoders.encode_base64(part)
194 part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
195 email.attach(part)
196
197print email.as_string()
198
199if not dry:
200 mark = time.time()
201 server = smtplib.SMTP(server, port)
202 server.helo(hostname)
203 server.ehlo(hostname)
204 server.set_debuglevel(0)
205 server.sendmail(sender, recipient, email.as_string())
206 server.quit()
207 print "Sending took sec: %.2f" % (time.time() - mark)
Note: See TracBrowser for help on using the repository browser.