function test_send_email
Interactive test function that prompts the user to send a test email through the O365Client to verify email sending functionality.
/tf/active/vicechatdev/email-forwarder/test_service.py
63 - 94
simple
Purpose
This function serves as a manual testing utility for the Email Forwarder service. It provides an interactive command-line interface to verify that the O365 email client is properly configured and can successfully send emails. The function prompts for user confirmation before sending and allows the user to specify a recipient email address, making it useful for development, debugging, and deployment verification.
Source Code
def test_send_email():
"""Test sending a simple email."""
print("Testing email sending...")
if not input("Send a test email? (y/N): ").lower().startswith('y'):
print("Skipping email send test")
return True
recipient = input("Enter recipient email: ").strip()
if not recipient:
print("No recipient provided, skipping test")
return True
try:
client = O365Client()
success = client.send_email(
to_addresses=[recipient],
subject="Email Forwarder Test",
body_html="<p>This is a test email from the Email Forwarder service.</p>",
body_text="This is a test email from the Email Forwarder service."
)
if success:
print("✓ Test email sent successfully")
return True
else:
print("✗ Failed to send test email")
return False
except Exception as e:
print(f"✗ Email send error: {e}")
return False
Return Value
Returns a boolean value: True if the test was skipped by user choice, no recipient was provided, or the email was sent successfully; False if an error occurred during email sending or the send operation failed.
Dependencies
O365Client (custom module)config (custom module)utils.logger (custom module)forwarder.o365_client (custom module)forwarder.email_handler (custom module)
Required Imports
from forwarder.o365_client import O365Client
Usage Example
# Run the test function interactively
from forwarder.o365_client import O365Client
def test_send_email():
"""Test sending a simple email."""
print("Testing email sending...")
if not input("Send a test email? (y/N): ").lower().startswith('y'):
print("Skipping email send test")
return True
recipient = input("Enter recipient email: ").strip()
if not recipient:
print("No recipient provided, skipping test")
return True
try:
client = O365Client()
success = client.send_email(
to_addresses=[recipient],
subject="Email Forwarder Test",
body_html="<p>This is a test email from the Email Forwarder service.</p>",
body_text="This is a test email from the Email Forwarder service."
)
if success:
print("✓ Test email sent successfully")
return True
else:
print("✗ Failed to send test email")
return False
except Exception as e:
print(f"✗ Email send error: {e}")
return False
# Execute the test
result = test_send_email()
print(f"Test result: {'Passed' if result else 'Failed'}")
Best Practices
- This function is intended for manual testing and should not be used in automated test suites due to its interactive nature
- Ensure O365Client is properly configured with valid credentials before running this test
- Use a test email address as the recipient to avoid sending test emails to real users
- The function returns True even when tests are skipped, so check console output for actual test results
- Handle the boolean return value appropriately in calling code to determine if the test passed or failed
- This function uses print statements for output; consider using a logging framework for production code
- The function catches all exceptions broadly; in production code, consider more specific exception handling
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function send_test_email_v1 76.9% similar
-
function send_test_email 74.5% similar
-
function main_v40 67.0% similar
-
class TestEmailHandler 66.6% similar
-
function test_o365_connection 66.4% similar