-
Notifications
You must be signed in to change notification settings - Fork 10
/
relaysend.py
56 lines (45 loc) · 1.68 KB
/
relaysend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
# Test if an SMTP service supports open relay
import argparse, sys, smtplib, datetime, re
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
parser = argparse.ArgumentParser(description="Test if an SMTP service supports open relay")
parser.add_argument("-t", "--toheader", help="To address")
parser.add_argument("-f", "--fromheader", help="From address")
parser.add_argument("-c", "--ccheader", help="CC address")
parser.add_argument("-g", "--host", help="SMTP host")
parser.add_argument("-p", "--port", help="SMTP port")
parser.add_argument("-m", "--message", help="An optional message to append to the generated email")
args = parser.parse_args()
if not args.toheader or not args.fromheader or not args.host:
parser.print_usage()
sys.exit(2)
print('From: ', args.fromheader)
print('To: ', args.toheader)
print('CC: ', args.ccheader)
print('Host: ', args.host)
if args.message:
print('Message: ', args.message)
msg = MIMEMultipart()
# Compile header
msg["From"] = args.fromheader
msg["To"] = args.toheader
msg["Cc"] = args.ccheader
msg["Subject"] = "Relayed mail from " + args.host
# Compile body
html = "<p>This email has been sent via an open relay at <strong>"+args.host+"</strong></p>"
if args.message:
html += "<p>" + args.message + "</p>"
msgText = MIMEText( html, "html" )
msg.attach(msgText)
# Send email
server = smtplib.SMTP(args.host,args.port)
server.set_debuglevel(1)
try:
server.starttls()
except:
print('Server doesn\'t support STARTTLS')
server.sendmail( args.fromheader, args.toheader, msg.as_string() )
server.quit()
print('Done')