Last active
October 28, 2019 18:28
-
-
Save 3lpsy/f9e2f9f6790885668dda6f14bb6981f6 to your computer and use it in GitHub Desktop.
smtpc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Import smtplib for the actual sending function | |
import smtplib | |
import sys | |
import argparse | |
# Import the email modules we'll need | |
from email.message import EmailMessage | |
def send(args): | |
msg = EmailMessage() | |
if args.body_file: | |
with open(args.text_file) as fp: | |
msg.set_content(fp.read()) | |
elif args.body: | |
msg.set_content(args.body) | |
else: | |
print("Please set either a body (string) or body_file (file containing text)") | |
sys.exit(1) | |
# me == the sender's email address | |
# you == the recipient's email address | |
msg["Subject"] = args.subject | |
msg["From"] = args.from_ | |
msg["To"] = args.to | |
# Send the message via our own SMTP server. | |
print("Connecting to SMTP Server:", str(args.smtp_host) + ":" + str(args.smtp_port)) | |
s = smtplib.SMTP(host=args.smtp_host, port=int(args.smtp_port)) | |
print("Sending Message to:", args.to) | |
s.send_message(msg) | |
print("Message sent!") | |
s.quit() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
"-s", "--subject", type=str, required=False, help="The subject of the email" | |
) | |
parser.add_argument( | |
"-b", "--body", type=str, required=False, help="The body of the email" | |
) | |
parser.add_argument( | |
"-B", "--body-file", type=str, required=False, help="The body of the email" | |
) | |
parser.add_argument( | |
"-t", "--to", type=str, required=True, help="Who to send the email to" | |
) | |
parser.add_argument( | |
"-f", | |
"--from", | |
type=str, | |
dest="from_", | |
required=True, | |
help="Who to send the email from", | |
) | |
parser.add_argument( | |
"-S", "--smtp-host", type=str, required=True, help="The SMTP Connection Host" | |
) | |
parser.add_argument( | |
"-P", "--smtp-port", type=str, required=True, help="The SMTP Connection Port" | |
) | |
args = parser.parse_args() | |
send(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment