Send Email
Send emails via SMTP with support for a JSON configuration file or command-line arguments.
Usage
Create a python file and name it send-email.py. Copy and paste the following code into the file.
import smtplib
import json
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# SMTP server configuration
import argparse
parser = argparse.ArgumentParser(description="Send an email via SMTP")
parser.add_argument('--config-file', default='send-email-config.json', help='Path to JSON configuration file (default: send-email-config.json)')
parser.add_argument('--smtp-server', help='SMTP server address')
parser.add_argument('--smtp-port', type=int, default=587, help='SMTP server port (default: 587)')
parser.add_argument('--username', help='SMTP username')
parser.add_argument('--password', help='SMTP password')
parser.add_argument('--from-address', help='Email address of the sender')
parser.add_argument('--to-address', help='Email address of the recipient')
parser.add_argument('--subject', default='Test Email', help='Subject of the email')
parser.add_argument('--body', default='This is a Test Email sent via Python script', help='Body of the email')
args = parser.parse_args()
# Load config from file if provided
config = {}
if args.config_file:
if not os.path.exists(args.config_file):
print(f"Error: Config file '{args.config_file}' not found")
exit(1)
with open(args.config_file, 'r') as f:
config = json.load(f)
# Use config file values as defaults, but allow command-line args to override
smtp_server = args.smtp_server or config.get('smtp_server')
smtp_port = args.smtp_port if args.smtp_port is not None else config.get('smtp_port', 587)
username = args.username or config.get('username')
password = args.password or config.get('password')
from_address = args.from_address or config.get('from_address')
to_address = args.to_address or config.get('to_address')
subject = args.subject if args.subject != 'Test Email' else config.get('subject', 'Test Email')
body = args.body if args.body != 'This is a Test Email sent via Python script' else config.get('body', 'This is a Test Email sent via Python script')
# Validate required parameters
if not smtp_server:
parser.error("--smtp-server is required (or provide it in config file)")
if not username:
parser.error("--username is required (or provide it in config file)")
if not password:
parser.error("--password is required (or provide it in config file)")
if not from_address:
parser.error("--from-address is required (or provide it in config file)")
if not to_address:
parser.error("--to-address is required (or provide it in config file)")
# Create a multipart message
msg = MIMEMultipart()
msg['From'] = from_address
msg['To'] = to_address
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
# Establish a secure connection with the SMTP server using STARTTLS
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
# Login to the SMTP server
server.login(username, password)
# Send the email
server.send_message(msg)
# Close the connection
server.quit()With config file
Create a send-email-config.json:
{
"smtp_server": "smtp.example.com",
"smtp_port": 587,
"username": "your-email@example.com",
"password": "your-password",
"from_address": "your-email@example.com",
"to_address": "recipient@example.com",
"subject": "Test Email",
"body": "This is a Test Email sent via Python script"
}Then run:
python send-email.pyWith command-line arguments
python send-email.py \
--smtp-server smtp.example.com \
--username user@example.com \
--password yourpassword \
--from-address user@example.com \
--to-address recipient@example.com \
--subject "Hello" \
--body "Email body text"Command-line arguments override config file values.
Parameters
--config-file— Path to JSON config (default:send-email-config.json)--smtp-server— SMTP server address--smtp-port— SMTP port (default: 587)--username/--password— SMTP credentials--from-address/--to-address— Sender and recipient--subject/--body— Email content
Last updated on