source: foomailer/foomail.py@ 386

Last change on this file since 386 was 383, checked in by Rick van der Zwet, 10 years ago

Add some credits :-)

  • Property svn:executable set to *
File size: 4.9 KB
RevLine 
[2]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()
[183]115hostname = socket.gethostname()
[2]116
117email['From'] = 'Testing sender <' + sender + '>'
118email['To'] = to
119if cc:
120 email['Cc'] = cc
121email['X-Server'] = server
[183]122email['X-Hostname'] = hostname
[2]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
[383]160--
161BSD Licensed Source Code for this tool at:
162https://rickvanderzwet.nl/svn/personal/foomailer
[2]163 '''
164 return(msg)
165
166if message:
167 f = open(message,'r')
168 msg = f.read()
169 f.close()
170else:
171 msg = defaultmessage()
172
173email.attach(MIMEText(msg))
174
175if virus:
176 part = MIMEBase('application', "octet-stream")
177 part.set_payload('X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*')
178 Encoders.encode_base64(part)
179 part.add_header('Content-Disposition', 'attachment; filename="eicar.txt"')
180 email.attach(part)
181
182if files:
183 for file in files.split(':'):
184 mimetype = mimetypes.guess_type(file)[0].split('/')
185 part = MIMEBase(mimetype[0],mimetype[1])
186 f = open(file,'r')
187 part.set_payload(f.read())
188 f.close()
189 Encoders.encode_base64(part)
190 part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
191 email.attach(part)
192
193print email.as_string()
194
195if not dry:
196 mark = time.time()
197 server = smtplib.SMTP(server)
[183]198 server.helo(hostname)
199 server.ehlo(hostname)
[184]200 server.set_debuglevel(0)
[2]201 server.sendmail(sender, recipient, email.as_string())
202 server.quit()
203 print "Sending took sec: %.2f" % (time.time() - mark)
Note: See TracBrowser for help on using the repository browser.