function test_smtp_basic
Tests basic SMTP server connectivity by attempting to establish a connection to a local SMTP server on port 2525 and performing an EHLO handshake.
/tf/active/vicechatdev/email-forwarder/service_status.py
34 - 42
simple
Purpose
This function is designed to verify that an SMTP server is running and accessible on localhost (127.0.0.1) at port 2525. It's typically used in testing environments to validate SMTP service availability before attempting to send emails. The function performs a basic EHLO command to ensure the server responds correctly to SMTP protocol commands.
Source Code
def test_smtp_basic():
"""Test basic SMTP communication."""
try:
import smtplib
with smtplib.SMTP('127.0.0.1', 2525, timeout=5) as server:
server.ehlo()
return True
except Exception as e:
return False, str(e)
Return Value
Returns True if the SMTP connection and EHLO handshake succeed. Returns a tuple (False, error_message) if any exception occurs during the connection attempt, where error_message is the string representation of the exception.
Dependencies
smtplib
Required Imports
import smtplib
Conditional/Optional Imports
These imports are only needed under specific conditions:
import smtplib
Condition: imported inside try block for error handling, but required for function execution
Required (conditional)Usage Example
# Example 1: Basic usage
result = test_smtp_basic()
if result is True:
print("SMTP server is running and accessible")
else:
success, error = result
print(f"SMTP test failed: {error}")
# Example 2: In a test suite
def setup_email_tests():
smtp_status = test_smtp_basic()
if smtp_status is not True:
raise RuntimeError(f"SMTP server not available: {smtp_status[1]}")
print("SMTP server ready for testing")
# Example 3: Health check
def check_services():
services = {'smtp': test_smtp_basic()}
return all(status is True for status in services.values())
Best Practices
- This function assumes SMTP server is on localhost port 2525, which is non-standard (standard SMTP port is 25). Ensure your test environment uses this port.
- The 5-second timeout prevents indefinite hanging if the server is unresponsive.
- The function returns inconsistent types (True vs tuple), which can lead to errors. Always check the return type before using the result.
- Consider using isinstance() or checking if result is True explicitly rather than truthy evaluation.
- The function silently catches all exceptions. In production, you may want to log specific exception types differently.
- This is suitable for local development/testing but should not be used for production SMTP validation without modifications.
- The context manager (with statement) ensures the SMTP connection is properly closed even if an error occurs.
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_smtp_connection 90.5% similar
-
function send_test_email_v1 78.3% similar
-
function check_port_listening 72.7% similar
-
class TestSmtpServer 70.6% similar
-
function send_test_email 69.6% similar