function send_test_email_v1
Sends a test email to a local SMTP server (127.0.0.1:2525) to verify email forwarding functionality and service connectivity.
/tf/active/vicechatdev/email-forwarder/test_e2e.py
26 - 57
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
smtplibtimeemail.mime.textemail.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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function send_test_email 87.9% similar
-
function test_smtp_connection 84.7% similar
-
function test_smtp_basic 78.3% similar
-
function test_send_email 76.9% similar
-
function main_v57 72.1% similar