scripts/send_mail
2020-06-14 21:48:19 -04:00

56 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import smtplib
import subprocess
import sys
import time
from email.message import Message
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from pathlib import Path
parser = argparse.ArgumentParser(description="mail")
parser.add_argument("--subject", "-s",
action="store", type=str, required=True,
help="Message subject")
parser.add_argument("--message", "-m",
action="store", type=str, required=True,
help="Message body")
parser.add_argument("--sender", "-f",
action="store", type=str, default="me@simon987.net",
help="Source address")
parser.add_argument("--to", "-t",
action="store", type=str, required=True,
help="Destination address")
parser.add_argument("--smtp_server",
action="store", type=str, default="mail.simon987.net",
help="SMTP server")
parser.add_argument("--smtp_password", "-p",
action="store", type=str, required=True,
help="SMTP password")
args = parser.parse_args()
HOME = str(Path.home())
if args.message == "-":
text = sys.stdin.read()
else:
text = args.message
msg = MIMEText(text).as_string().replace("\n", "\r\n")
msg["From"] = args.sender
msg["Subject"] = args.subject
msg["To"] = args.to
msg["Date"] = formatdate(time.time(), True)
s = smtplib.SMTP(args.smtp_server, port=587, )
s.starttls()
s.login(args.sender, args.smtp_password)
s.sendmail(args.sender, args.to, msg.as_string())
s.quit()