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:dp:",
|
---|
35 | ["from=","force","recipient=","server=","spam","port=","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 | port = 25
|
---|
44 | recipient = False
|
---|
45 | sender = False
|
---|
46 | files = False
|
---|
47 | force = False
|
---|
48 | dry = False
|
---|
49 | message = False
|
---|
50 | subject = False
|
---|
51 | to = False
|
---|
52 | cc = False
|
---|
53 |
|
---|
54 | tests = ""
|
---|
55 |
|
---|
56 | for 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 |
|
---|
88 | error = False
|
---|
89 | if not sender:
|
---|
90 | print "No sender defined"
|
---|
91 | error = True
|
---|
92 | elif not recipient:
|
---|
93 | print "No recipient defined"
|
---|
94 | error = True
|
---|
95 | elif not server:
|
---|
96 | print "No server defined"
|
---|
97 | error = True
|
---|
98 |
|
---|
99 | if error:
|
---|
100 | usage()
|
---|
101 | sys.exit(128)
|
---|
102 |
|
---|
103 | if force:
|
---|
104 | sender = sender_raw
|
---|
105 | recipient = recipient_raw
|
---|
106 |
|
---|
107 |
|
---|
108 | #Subject here, just to keep the settings/layout a bit more constent
|
---|
109 | if not subject:
|
---|
110 | subject = 'Test email send at %a, %d %b %Y %T %z'
|
---|
111 | subject = strftime(subject,gmtime())
|
---|
112 |
|
---|
113 | if not to:
|
---|
114 | to = 'Testing received <' + recipient + '>'
|
---|
115 | to = recipient
|
---|
116 |
|
---|
117 | email = MIMEMultipart()
|
---|
118 | hostname = socket.gethostname()
|
---|
119 |
|
---|
120 | email['From'] = 'Testing sender <' + sender + '>'
|
---|
121 | email['To'] = to
|
---|
122 | if cc:
|
---|
123 | email['Cc'] = cc
|
---|
124 | email['X-Server'] = server
|
---|
125 | email['X-Port'] = port
|
---|
126 | email['X-Hostname'] = hostname
|
---|
127 | email['Message-ID' ] = '<' +''.join(random.sample(string.letters +
|
---|
128 | string.digits,10)) + '@' + email['X-Hostname'] + '>'
|
---|
129 | email['Date'] = strftime('%a, %d %b %Y %T %z',gmtime())
|
---|
130 | email['Subject'] = subject
|
---|
131 | email['X-Version'] = __svnversion__
|
---|
132 | email['X-Author'] = __author__
|
---|
133 | email['X-Tests'] = tests
|
---|
134 | #email['Reply-To'] = "Rick vd Zwet <rick@joost.com>, Wouter Simons <wouter@joost.com>"
|
---|
135 | if spam:
|
---|
136 | email['X-Spam'] = 'XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X'
|
---|
137 |
|
---|
138 |
|
---|
139 | def defaultmessage():
|
---|
140 | msg = ''
|
---|
141 | msg += \
|
---|
142 | '''
|
---|
143 | Hi,
|
---|
144 |
|
---|
145 | Sorry to bother you...
|
---|
146 |
|
---|
147 | Please ignore this message as it is used to test
|
---|
148 | and verify systems to make sure everything works
|
---|
149 | flawless.
|
---|
150 |
|
---|
151 | %(virus)s
|
---|
152 |
|
---|
153 | Technical 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 | '''
|
---|
162 | Best regards,
|
---|
163 | Your system administrator
|
---|
164 | --
|
---|
165 | BSD Licensed Source Code for this tool at:
|
---|
166 | https://rickvanderzwet.nl/svn/personal/foomailer
|
---|
167 | '''
|
---|
168 | return(msg)
|
---|
169 |
|
---|
170 | if message:
|
---|
171 | f = open(message,'r')
|
---|
172 | msg = f.read()
|
---|
173 | f.close()
|
---|
174 | else:
|
---|
175 | msg = defaultmessage()
|
---|
176 |
|
---|
177 | email.attach(MIMEText(msg))
|
---|
178 |
|
---|
179 | if 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 |
|
---|
186 | if 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 |
|
---|
197 | print email.as_string()
|
---|
198 |
|
---|
199 | if 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)
|
---|