🔍 Code Extractor

function send_test_email_v1

Maturity: 46

Sends a test email to a local SMTP server (127.0.0.1:2525) to verify email forwarding functionality and service connectivity.

File:
/tf/active/vicechatdev/email-forwarder/test_e2e.py
Lines:
26 - 57
Complexity:
simple

Purpose

This function is designed for testing and validating an email forwarder service. It creates a MIME multipart test email with a timestamp and sends it through a local SMTP server running on port 2525. The function verifies that the SMTP server accepts connections, email parsing works correctly, and the O365 client is functioning (though Microsoft 365 authentication may still fail). It's primarily used for end-to-end testing of email forwarding infrastructure.

Source Code

def send_test_email():
    """Send a test email through the forwarder."""
    try:
        print("\nSending test email...")
        
        # Create message
        msg = MIMEMultipart()
        msg['From'] = 'test-sender@example.com'
        msg['To'] = 'test-recipient@example.com' 
        msg['Subject'] = 'End-to-End Test Email'
        
        body = """
        This is a test email sent through the email forwarder service.
        
        If you receive this email, it means:
        1. The SMTP server is accepting connections
        2. Email parsing is working
        3. The O365 client is functioning (though may fail at MS365 auth)
        
        Test timestamp: """ + str(time.time())
        
        msg.attach(MIMEText(body, 'plain'))
        
        # Send email
        with smtplib.SMTP('127.0.0.1', 2525, timeout=10) as server:
            server.send_message(msg)
            print("✓ Email sent successfully to forwarder")
            return True
            
    except Exception as e:
        print(f"✗ Failed to send email: {e}")
        return False

Return Value

Returns a boolean value: True if the email was sent successfully through the forwarder, False if any exception occurred during the sending process. The function also prints status messages to stdout indicating success (✓) or failure (✗) with error details.

Dependencies

  • smtplib
  • time
  • email.mime.text
  • email.mime.multipart

Required Imports

import smtplib
import time
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

Usage Example

# Ensure your email forwarder service is running on localhost:2525
# Then call the function directly

import smtplib
import time
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

def send_test_email():
    """Send a test email through the forwarder."""
    try:
        print("\nSending test email...")
        msg = MIMEMultipart()
        msg['From'] = 'test-sender@example.com'
        msg['To'] = 'test-recipient@example.com' 
        msg['Subject'] = 'End-to-End Test Email'
        body = """This is a test email sent through the email forwarder service.
        Test timestamp: """ + str(time.time())
        msg.attach(MIMEText(body, 'plain'))
        with smtplib.SMTP('127.0.0.1', 2525, timeout=10) as server:
            server.send_message(msg)
            print("✓ Email sent successfully to forwarder")
            return True
    except Exception as e:
        print(f"✗ Failed to send email: {e}")
        return False

# Execute the test
result = send_test_email()
if result:
    print("Test passed!")
else:
    print("Test failed!")

Best Practices

  • Ensure the email forwarder service is running before calling this function
  • The function uses a 10-second timeout for SMTP connections to prevent hanging
  • This function is intended for testing/development environments, not production use
  • The hardcoded email addresses (test-sender@example.com, test-recipient@example.com) are placeholders and won't route to real recipients
  • Monitor the console output for success/failure indicators (✓/✗)
  • The function catches all exceptions broadly; consider more specific exception handling for production code
  • Port 2525 is a common alternative SMTP port used for testing to avoid conflicts with standard port 25
  • The timestamp in the email body helps identify when each test was run

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function send_test_email 87.9% similar

    Sends a test email via SMTP to verify email forwarding service functionality, creating a MIME multipart message with customizable sender, recipient, subject, and body content.

    From: /tf/active/vicechatdev/email-forwarder/send_test_email.py
  • function test_smtp_connection 84.7% similar

    Tests the SMTP connection to a local email forwarder service running on port 2525 by establishing a connection and performing an EHLO handshake.

    From: /tf/active/vicechatdev/email-forwarder/test_e2e.py
  • function test_smtp_basic 78.3% similar

    Tests basic SMTP server connectivity by attempting to establish a connection to a local SMTP server on port 2525 and performing an EHLO handshake.

    From: /tf/active/vicechatdev/email-forwarder/service_status.py
  • function test_send_email 76.9% similar

    Interactive test function that prompts the user to send a test email through the O365Client to verify email sending functionality.

    From: /tf/active/vicechatdev/email-forwarder/test_service.py
  • function main_v57 72.1% similar

    Performs a comprehensive status check of an email forwarder service, verifying process status, port availability, SMTP communication, and configuration settings.

    From: /tf/active/vicechatdev/email-forwarder/service_status.py
← Back to Browse