source: foomailer/foomail.py@ 209

Last change on this file since 209 was 184, checked in by Rick van der Zwet, 14 years ago

Debug is a 'bit' to verbose.

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