def main(): # get user login user_email = get_login() if not user_email: exit_with_error("Unauthorized") # read stdin and parse to message object input_bytes = sys.stdin.buffer.read() parser = email.parser.BytesParser() try: message = parser.parsebytes(input_bytes) except MessageError as e: exit_with_error("MessageError: %s" % str(e)) # add a header with the user's email message[USER_TRACKING_HEADER] = user_email # add a header with the server hostname # scripts.mit.edu runs multiple machines and load-balances between them message[SCRIPTS_HOSTNAME_HEADER] = platform.node() # add date if missing if "Date" not in message: message["Date"] = email.utils.formatdate() if "--debug" in sys.argv: # print the email without sending print(message.as_string(maxheaderlen=78)) else: # send the email send_email(message)
def main(): # set up stdin/stdout to use utf-8 # otherwise, scripts.mit.edu defaults to ascii encoding # which leads to errors when receiving unicode input sys.stdin = TextIOWrapper(sys.stdin.detach(), encoding="utf8") sys.stdout = TextIOWrapper(sys.stdout.detach(), encoding="utf8") # get user login user_email = get_login() if not user_email: exit_with_error("Unauthorized") # read json input from stdin try: request = json.load(sys.stdin) except Exception as e: # apparently json.JSONDecodeError doesn't exist in python 3.3 # so we use the generic Exception to catch JSON parsing errors exit_with_error("JSON parse error: %s" % str(e)) # create email.message.Message object message = make_email_message(request) # add a header with the user's email message[USER_TRACKING_HEADER] = user_email # add a header with the server hostname # scripts.mit.edu runs multiple machines and load-balances between them message[SCRIPTS_HOSTNAME_HEADER] = platform.node() # add date if missing if "Date" not in message: message["Date"] = email.utils.formatdate() if "--debug" in sys.argv: # print the email without sending print(message.as_string(maxheaderlen=78)) else: host = request.get("host", SMTP_HOST) send_email(message, host)
# apparently json.JSONDecodeError doesn't exist in python 3.3 # so we use the generic Exception to catch JSON parsing errors exit_with_error("JSON parse error: %s" % str(e)) # create email.message.Message object message = make_email_message(request) # add a header with the user's email message[USER_TRACKING_HEADER] = user_email # add a header with the server hostname # scripts.mit.edu runs multiple machines and load-balances between them message[SCRIPTS_HOSTNAME_HEADER] = platform.node() # add date if missing if "Date" not in message: message["Date"] = email.utils.formatdate() if "--debug" in sys.argv: # print the email without sending print(message.as_string(maxheaderlen=78)) else: send_email(message) if __name__ == "__main__": try: main() except Exception as e: exit_with_error(repr(e)) exit_with_success()