From 056ccf3c36d1f1cf349b37f086e40f1823b22902 Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Sun, 15 Oct 2023 16:32:59 -0400 Subject: [PATCH] sendmail script --- configs/scripts/ewp-sendmail.py | 66 +++++++++++++++++++++++++++++++++ install.conf.yaml | 1 + 2 files changed, 67 insertions(+) create mode 100644 configs/scripts/ewp-sendmail.py diff --git a/configs/scripts/ewp-sendmail.py b/configs/scripts/ewp-sendmail.py new file mode 100644 index 0000000..8bdf55a --- /dev/null +++ b/configs/scripts/ewp-sendmail.py @@ -0,0 +1,66 @@ +#! /usr/bin/env python3 +import smtplib +import getpass +from email.message import EmailMessage +import argparse +import sys + + +def main() -> int: + # Handle program arguments + ap = argparse.ArgumentParser( + prog="ewp-sendmail", description="Easily send real emails" + ) + ap.add_argument( + "--recipient", + "--to", + help="Email address of the recipient", + default="evan@ewpratten.com", + ) + ap.add_argument("--cc", help="Email addresses to CC", nargs="+") + ap.add_argument("--subject", "-s", help="Subject of the email") + ap.add_argument( + "--from", + help="Sender of the email", + default="system-reports@ewpratten.com", + dest="sender", + ) + ap.add_argument( + "--password", + help="Password to use for sending the email. Overrides the password in ~/.netrc", + ) + args = ap.parse_args() + + # Read the body from stdin + print("Enter the body of the email. Press Ctrl+D when done.") + body = sys.stdin.read() + + # Read the password + password = args.password or getpass.getpass(f"Password for {args.sender}: ") + + # Log in to the SMTP server + print("Connecting to SMTP server...") + smtp = smtplib.SMTP("smtp.ewpratten.com", 587) + smtp.ehlo() + smtp.starttls() + print("Authenticating...") + smtp.login(args.sender, password) + print("Sending email...") + # Create the email + msg = EmailMessage() + msg.set_content(body) + msg["Subject"] = args.subject + msg["From"] = args.sender + msg["To"] = args.recipient + if args.cc: + msg["Cc"] = ",".join(args.cc) + # Send the email + smtp.send_message(msg) + print("Done.") + smtp.quit() + + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/install.conf.yaml b/install.conf.yaml index ffe62ef..03558af 100644 --- a/install.conf.yaml +++ b/install.conf.yaml @@ -55,6 +55,7 @@ ~/bin/guru-vpn: configs/scripts/guru-vpn.py ~/bin/wg-handshakes: configs/scripts/wg-handshakes.py ~/bin/wg-genzone: configs/scripts/wg-genzone.py + ~/bin/ewp-sendmail: configs/scripts/ewp-sendmail.py # Nautilus right-click scripts ~/.local/share/nautilus/scripts/Copy to web: