1 | #!/usr/bin/env python
|
---|
2 | # Rick van der Zwet
|
---|
3 | # TODO: May need to implement some RFC checkers
|
---|
4 | #
|
---|
5 | #
|
---|
6 | import sys,smtplib,re,socket,random,string,os,getopt
|
---|
7 | import mimetypes
|
---|
8 | import time
|
---|
9 | from time import gmtime,strftime
|
---|
10 | from email.MIMEMultipart import MIMEMultipart
|
---|
11 | from email.MIMEBase import MIMEBase
|
---|
12 | from email.MIMEText import MIMEText
|
---|
13 | from email.Utils import COMMASPACE, formatdate
|
---|
14 | from 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 |
|
---|
20 | def 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 |
|
---|
26 | default_domain = 'example.com'
|
---|
27 | def emailparse(email):
|
---|
28 | if re.compile('.*@.*').match(email):
|
---|
29 | return(email)
|
---|
30 | else:
|
---|
31 | return(email + '@' + default_domain)
|
---|
32 |
|
---|
33 | try:
|
---|
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="])
|
---|
36 | except getopt.GetoptError:
|
---|
37 | usage()
|
---|
38 | sys.exit(128)
|
---|
39 |
|
---|
40 | spam = False
|
---|
41 | virus = False
|
---|
42 | server = False
|
---|
43 | recipient = False
|
---|
44 | sender = False
|
---|
45 | files = False
|
---|
46 | force = False
|
---|
47 | dry = False
|
---|
48 | message = False
|
---|
49 | subject = False
|
---|
50 | to = False
|
---|
51 | cc = False
|
---|
52 |
|
---|
53 | tests = ""
|
---|
54 |
|
---|
55 | for 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 |
|
---|
85 | error = False
|
---|
86 | if not sender:
|
---|
87 | print "No sender defined"
|
---|
88 | error = True
|
---|
89 | elif not recipient:
|
---|
90 | print "No recipient defined"
|
---|
91 | error = True
|
---|
92 | elif not server:
|
---|
93 | print "No server defined"
|
---|
94 | error = True
|
---|
95 |
|
---|
96 | if error:
|
---|
97 | usage()
|
---|
98 | sys.exit(128)
|
---|
99 |
|
---|
100 | if force:
|
---|
101 | sender = sender_raw
|
---|
102 | recipient = recipient_raw
|
---|
103 |
|
---|
104 |
|
---|
105 | #Subject here, just to keep the settings/layout a bit more constent
|
---|
106 | if not subject:
|
---|
107 | subject = 'Test email send at %a, %d %b %Y %T %z'
|
---|
108 | subject = strftime(subject,gmtime())
|
---|
109 |
|
---|
110 | if not to:
|
---|
111 | to = 'Testing received <' + recipient + '>'
|
---|
112 | to = recipient
|
---|
113 |
|
---|
114 | email = MIMEMultipart()
|
---|
115 | hostname = socket.gethostname()
|
---|
116 |
|
---|
117 | email['From'] = 'Testing sender <' + sender + '>'
|
---|
118 | email['To'] = to
|
---|
119 | if cc:
|
---|
120 | email['Cc'] = cc
|
---|
121 | email['X-Server'] = server
|
---|
122 | email['X-Hostname'] = hostname
|
---|
123 | email['Message-ID' ] = '<' +''.join(random.sample(string.letters +
|
---|
124 | string.digits,10)) + '@' + email['X-Hostname'] + '>'
|
---|
125 | email['Date'] = strftime('%a, %d %b %Y %T %z',gmtime())
|
---|
126 | email['Subject'] = subject
|
---|
127 | email['X-Version'] = __svnversion__
|
---|
128 | email['X-Author'] = __author__
|
---|
129 | email['X-Tests'] = tests
|
---|
130 | #email['Reply-To'] = "Rick vd Zwet <rick@joost.com>, Wouter Simons <wouter@joost.com>"
|
---|
131 | if spam:
|
---|
132 | email['X-Spam'] = 'XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X'
|
---|
133 |
|
---|
134 |
|
---|
135 | def defaultmessage():
|
---|
136 | msg = ''
|
---|
137 | msg += \
|
---|
138 | '''
|
---|
139 | Hi,
|
---|
140 |
|
---|
141 | Sorry to bother you...
|
---|
142 |
|
---|
143 | Please ignore this message as it is used to test
|
---|
144 | and verify systems to make sure everything works
|
---|
145 | flawless.
|
---|
146 |
|
---|
147 | %(virus)s
|
---|
148 |
|
---|
149 | Technical 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 | '''
|
---|
158 | Best regards,
|
---|
159 | Your system administrator
|
---|
160 | '''
|
---|
161 | return(msg)
|
---|
162 |
|
---|
163 | if message:
|
---|
164 | f = open(message,'r')
|
---|
165 | msg = f.read()
|
---|
166 | f.close()
|
---|
167 | else:
|
---|
168 | msg = defaultmessage()
|
---|
169 |
|
---|
170 | email.attach(MIMEText(msg))
|
---|
171 |
|
---|
172 | if 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 |
|
---|
179 | if 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 |
|
---|
190 | print email.as_string()
|
---|
191 |
|
---|
192 | if not dry:
|
---|
193 | mark = time.time()
|
---|
194 | server = smtplib.SMTP(server)
|
---|
195 | server.helo(hostname)
|
---|
196 | server.ehlo(hostname)
|
---|
197 | server.set_debuglevel(1)
|
---|
198 | server.sendmail(sender, recipient, email.as_string())
|
---|
199 | server.quit()
|
---|
200 | print "Sending took sec: %.2f" % (time.time() - mark)
|
---|