aboutsummaryrefslogtreecommitdiff
path: root/bugzilla/sbin/sendmail
diff options
context:
space:
mode:
Diffstat (limited to 'bugzilla/sbin/sendmail')
-rwxr-xr-xbugzilla/sbin/sendmail104
1 files changed, 0 insertions, 104 deletions
diff --git a/bugzilla/sbin/sendmail b/bugzilla/sbin/sendmail
deleted file mode 100755
index db5abbd..0000000
--- a/bugzilla/sbin/sendmail
+++ /dev/null
@@ -1,104 +0,0 @@
1#!/usr/bin/python
2
3import os
4import re
5import sys
6import email
7import boto3
8import socket
9import argparse
10from botocore.exceptions import NoRegionError
11
12# These are all the sendmail options we don't support but have to accept so we
13# can ignore them without messing up the command line.
14#
15# Format is (argument, takes parameters)
16IGNORED = (
17 ("-4", False), ("-6", False), ("-au", True), ("-ap", True),
18 ("-am", True), ("-ba", False), ("-bd", False), ("-bi", False),
19 ("-bm", False), ("-bp", False), ("-bs", False), ("-bt", False),
20 ("-bv", False), ("-bz", False), ("-C", True), ("-d", True),
21 ("-E", False), ("-h", True), ("-m", False), ("-M", True),
22 ("-N", True), ("-n", False), ("-oA", True), ("-oc", False),
23 ("-od", True), ("-oD", False), ("-oe", False), ("-oF", True),
24 ("-of", False), ("-og", True), ("-oH", True), ("-oi", False),
25 ("-oL", True), ("-om", False), ("-oo", False), ("-oQ", True),
26 ("-or", True), ("-oS", True), ("-os", False), ("-oT", True),
27 ("-ot", False), ("-ou", True), ("-q", True), ("-R", True),
28 ("-v", False), ("-F", True), ("-t", True),
29)
30
31# A rough approximation of an email address but should be good enough to pick
32# emails out of a command line
33SORTA_EMAIL = re.compile("\S+@\S+\.\S+")
34
35if os.path.exists("/etc/mailname"):
36 with open("/etc/mailname", "r") as fp:
37 MAIL_DOMAIN = fp.read().strip()
38else:
39 MAIL_DOMAIN = socket.getfqdn()
40
41# Configuration comes from the environment or metadata service
42try:
43 client = boto3.client("ses")
44except NoRegionError:
45 boto3.setup_default_session(region_name="us-west-2")
46 client = boto3.client("ses")
47
48
49
50def parse_args():
51 parser = argparse.ArgumentParser(add_help=False)
52 parser.add_argument("-V", action="store_true", dest="display_version")
53 parser.add_argument("-f", nargs=1, dest="sender_addr")
54 parser.add_argument("-r", nargs=1, dest="sender_addr")
55
56 for arg, nargs in IGNORED:
57 parser.add_argument(arg, nargs="?" if nargs else None)
58
59 opts, args = parser.parse_known_args()
60 addresses = [a for a in args if SORTA_EMAIL.match(a)]
61
62 return opts, addresses
63
64
65def main():
66 opts, addresses = parse_args()
67
68 if opts.display_version:
69 print("SES raw mail sender (definitely not sendmail)")
70 sys.exit(0)
71
72 try:
73 sender = opts.sender_addr[0]
74 except (IndexError, TypeError):
75 sender = None
76
77 msg = email.message_from_string(sys.stdin.read().encode("us-ascii"))
78
79 # Fix up cron emails
80 if 'Cron Daemon' in msg.get("From"):
81 msg.replace_header("From", "cron-no-reply@{}".format(MAIL_DOMAIN))
82
83 ses_args = {"RawMessage": {"Data": msg.as_string()}}
84
85 if sender and not SORTA_EMAIL.match(sender):
86 raise Exception("Sender email does not look like an email")
87
88 if sender:
89 ses_args["Source"] = sender
90
91 if addresses:
92 ses_args["Destinations"] = addresses
93
94 client.send_raw_email(**ses_args)
95
96
97if __name__ == "__main__":
98 try:
99 main()
100 sys.exit(0)
101 except Exception as e:
102 print("Error during sending:")
103 print(e)
104 sys.exit(1)