Python Snippet: Send Email via SMTP
#!/usr/bin/env python
import smtplib
import argparse
from datetime import datetime, timedelta
from email.message import EmailMessage
parser = argparse.ArgumentParser(description='Send email')
parser.add_argument(
'--smtp-host', dest='smtp_host',
type=str, help="SMTP host and port", required=True)
parser.add_argument(
'--smtp-user', dest='smtp_user',
type=str, help="SMTP user for login", required=True)
parser.add_argument(
'--smtp-pass', dest='smtp_pass',
type=str, help="SMTP password for login", required=True)
parser.add_argument(
'--to', dest='to_address',
type=str, help="TO address", required=True)
parser.add_argument(
'--from', dest='from_address',
type=str, help="FROM address", required=True)
parser.add_argument(
'--title', dest='title',
type=str, help="Mail title", required=True)
parser.add_argument(
'--body', dest='body',
type=str, help="Mail body", required=True)
args = parser.parse_args()
msg = EmailMessage()
msg.set_content(args.body)
msg['Subject'] = args.title
msg['From'] = args.from_address
msg['To'] = args.to_address
service = smtplib.SMTP_SSL(args.smtp_host)
# service.starttls()
service.login(args.smtp_user, args.smtp_pass)
service.send_message(msg)
service.close()
| Written by Alexandru Nedelcu