Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 161 lines (125 sloc) 5.441 kb
3e88503 @paltman initial commit
authored
1 import argparse
f293b1b @blentz Add --sanitize flag.
blentz authored
2 import re
3e88503 @paltman initial commit
authored
3 import sys
4
5 import boto
6
7 from postman import __version__
8
1bc8412 @paltman Style nits
authored
9 legal_headers = [ "Accept-Language", "Bcc", "Cc", "Comments", "Content-Type",
10 "Content-Transfer-Encoding", "Content-ID", "Content-Description",
11 "Content-Disposition", "Content-Language", "Date", "DKIM-Signature",
7805fe6 @blentz typo fix in legal_headers
blentz authored
12 "DomainKey-Signature", "From", "In-Reply-To", "Keywords", "List-Archive",
1bc8412 @paltman Style nits
authored
13 "List-Help", "List-Id", "List-Owner", "List-Post", "List-Subscribe",
14 "List-Unsubscribe", "Message-Id", "MIME-Version", "Received", "References",
15 "Reply-To", "Return-Path", "Sender", "Subject", "Thread-Index",
16 "Thread-Topic", "To", "User-Agent" ]
3e88503 @paltman initial commit
authored
17
18 def out(msg, args):
19 if args.verbose:
20 sys.stdout.write("%s\n" % msg)
21 sys.stdout.flush()
22
f293b1b @blentz Add --sanitize flag.
blentz authored
23 def sanitize(args, msg):
1bc8412 @paltman Style nits
authored
24 """
25 Transform any headers SES disallows in to X-Headers.
26
f293b1b @blentz Add --sanitize flag.
blentz authored
27 Sanitize method adapted from this Perl snippet:
28 http://www.evanhoffman.com/evan/2011/05/16/amazon-ses-illegal-header-errors/
1bc8412 @paltman Style nits
authored
29 """
f293b1b @blentz Add --sanitize flag.
blentz authored
30 cleanmsg = ""
31 in_header = True
32
33 # Avoids indented lines.
34 hdr_re = re.compile("^([\w\d\-]+):")
35
36 for line in msg.splitlines(True):
37 # Two line breaks between header and message body.
38 if re.match("^[\n\r]", line):
39 # Do nothing once we've scanned the headers.
40 in_header = False
41 if in_header and hdr_re.match(line):
42 if hdr_re.match(line).group(1) not in legal_headers:
43 line = "X-%s" % line
44 cleanmsg += line
45 return cleanmsg
46
3e88503 @paltman initial commit
authored
47 def cmd_send(args):
48 ses = boto.connect_ses()
49 out("Sending mail to: %s" % ", ".join(args.destinations), args)
50 msg = sys.stdin.read()
f293b1b @blentz Add --sanitize flag.
blentz authored
51 if args.sanitize:
52 msg = sanitize(args, msg)
efb14f6 @madisvain boto 2.0 has changed send_raw_email function arguments order - this fixe...
madisvain authored
53 r = ses.send_raw_email(msg, args.f, args.destinations)
3e88503 @paltman initial commit
authored
54 if r.get("SendRawEmailResponse", {}).get("SendRawEmailResult", {}).get("MessageId"):
55 out("OK", args)
56 else:
57 out("ERROR: %s" % r, args)
58
59
60 def cmd_verify(args):
61 ses = boto.connect_ses()
62 for email in args.email:
63 ses.verify_email_address(email)
64 out("Verification for %s sent." % email, args)
65
66
67 def cmd_list_verified(args):
68 ses = boto.connect_ses()
69 args.verbose = True
70
71 addresses = ses.list_verified_email_addresses()
72 addresses = addresses["ListVerifiedEmailAddressesResponse"]
73 addresses = addresses["ListVerifiedEmailAddressesResult"]
74 addresses = addresses["VerifiedEmailAddresses"]
75
76 if not addresses:
77 out("No addresses are verified on this account.", args)
78 return
79
80 for address in addresses:
81 out(address, args)
82
83
84 def cmd_show_quota(args):
85 ses = boto.connect_ses()
86 args.verbose= True
87
88 data = ses.get_send_quota()["GetSendQuotaResponse"]["GetSendQuotaResult"]
89 out("Max 24 Hour Send: %s" % data["Max24HourSend"], args)
90 out("Sent Last 24 Hours: %s" % data["SentLast24Hours"], args)
91 out("Max Send Rate: %s" % data["MaxSendRate"], args)
92
93
94 def cmd_show_stats(args):
95 ses = boto.connect_ses()
96 args.verbose = True
97
98 data = ses.get_send_statistics()
99 data = data["GetSendStatisticsResponse"]["GetSendStatisticsResult"]
100 for datum in data["SendDataPoints"]:
101 out("Complaints: %s" % datum["Complaints"], args)
102 out("Timestamp: %s" % datum["Timestamp"], args)
103 out("DeliveryAttempts: %s" % datum["DeliveryAttempts"], args)
104 out("Bounces: %s" % datum["Bounces"], args)
105 out("Rejects: %s" % datum["Rejects"], args)
106 out("", args)
107
108
109 def cmd_delete_verified(args):
110 ses = boto.connect_ses()
111 for email in args.email:
112 ses.delete_verified_email_address(email_address=email)
113 out("Deleted %s" % email, args)
114
115
116 def main():
117 parser = argparse.ArgumentParser(prog="postman", description="send an email via Amazon SES")
118 parser.add_argument("--version", action="version", version="%%(prog)s %s" % __version__)
119 parser.add_argument("--verbose", action="store_true")
120
121 command_parsers = parser.add_subparsers(dest="command")
122
123 # cmd: send
124 parser_send = command_parsers.add_parser("send")
125 parser_send.add_argument("-f",
126 help="the address to send the message from, must be validated")
f293b1b @blentz Add --sanitize flag.
blentz authored
127 parser_send.add_argument("--sanitize", action="store_true",
128 help="Sanitize headers. Convert illegal headers to X-Headers.")
3e88503 @paltman initial commit
authored
129 parser_send.add_argument("destinations", metavar="TO", type=str, nargs="+",
130 help="a list of email addresses to deliver message to")
131
132 # cmd: verify
133 parser_send = command_parsers.add_parser("verify")
134 parser_send.add_argument("email", nargs="+",
135 help="an email address to verify for sending from")
136
137 # cmd: list_verified
138 command_parsers.add_parser("list_verified")
139
140 # cmd: show_quota
141 command_parsers.add_parser("show_quota")
142
143 # cmd: show_stats
144 command_parsers.add_parser("show_stats")
145
146 # cmd: delete_verified
147 parser_delete = command_parsers.add_parser("delete_verified")
148 parser_delete.add_argument("email", nargs="+",
149 help="verified email addresses that will be deleted from verification list")
150
151 args = parser.parse_args()
152
153 {
154 "send": cmd_send,
155 "verify": cmd_verify,
156 "list_verified": cmd_list_verified,
157 "show_quota": cmd_show_quota,
158 "show_stats": cmd_show_stats,
159 "delete_verified": cmd_delete_verified
160 }[args.command](args)
Something went wrong with that request. Please try again.