function main_v69
Command-line interface function that parses arguments and sends a test email through an SMTP forwarder service, displaying connection details and returning an exit code based on success.
/tf/active/vicechatdev/email-forwarder/send_test_email.py
40 - 68
simple
Purpose
This function serves as the entry point for a test client application that validates email forwarding functionality. It configures an argument parser to accept SMTP connection parameters, sender/recipient information, and email content, then invokes the send_test_email function with these parameters. The function is designed for testing and debugging email forwarder services by providing a simple CLI tool to send test emails with customizable parameters.
Source Code
def main():
parser = argparse.ArgumentParser(description='Send test email to forwarder service')
parser.add_argument('--host', default='localhost', help='SMTP host (default: localhost)')
parser.add_argument('--port', default=2525, type=int, help='SMTP port (default: 2525)')
parser.add_argument('--from', dest='sender', default='test@example.com', help='Sender email')
parser.add_argument('--to', dest='recipient', required=True, help='Recipient email')
parser.add_argument('--subject', default='Test Email from Forwarder', help='Email subject')
parser.add_argument('--message', default='This is a test email from the email forwarder service.', help='Email message')
args = parser.parse_args()
print("Email Forwarder Test Client")
print("=" * 30)
print(f"SMTP Server: {args.host}:{args.port}")
print(f"From: {args.sender}")
print(f"To: {args.recipient}")
print(f"Subject: {args.subject}")
print()
success = send_test_email(
smtp_host=args.host,
smtp_port=args.port,
sender=args.sender,
recipient=args.recipient,
subject=args.subject,
message=args.message
)
return 0 if success else 1
Return Value
Returns an integer exit code: 0 if the email was sent successfully (when send_test_email returns True), or 1 if the email sending failed (when send_test_email returns False). This follows standard Unix convention for command-line tool exit codes.
Dependencies
argparsesmtplibsysemail.mime.textemail.mime.multipartemail.mime.application
Required Imports
import argparse
import smtplib
import sys
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
Usage Example
# Basic usage with required recipient
if __name__ == '__main__':
exit_code = main()
sys.exit(exit_code)
# Command line examples:
# python script.py --to recipient@example.com
# python script.py --host smtp.example.com --port 587 --from sender@test.com --to recipient@example.com --subject "Custom Subject" --message "Custom message body"
Best Practices
- This function should be called from an if __name__ == '__main__': block to prevent execution when imported as a module
- The function depends on send_test_email being defined elsewhere in the codebase - ensure this dependency is available
- Use sys.exit(main()) to properly propagate the exit code to the operating system
- The --to argument is required; all other arguments have sensible defaults for local testing
- Default SMTP port 2525 is commonly used for testing/development; production systems typically use port 25, 465, or 587
- Consider adding error handling for invalid email addresses or network connectivity issues in production use
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function send_test_email 74.8% similar
-
function main_v9 73.6% similar
-
function send_test_email_v1 71.6% similar
-
function main_v53 71.5% similar
-
function main_v42 71.3% similar