# SMTP on Serv00 Python script
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import logging
# Configure logging
logging.basicConfig(level=logging.DEBUG)
# SMTP server configuration
smtp_server = 'mail3.serv00.com'
smtp_port = 587
username = 'your_username' # Replace with your username
password = 'your_password' # Replace with your password
# Create email content
msg = MIMEMultipart()
msg['From'] = '[email protected]' # Replace with sender's email address
msg['To'] = '[email protected]' # Replace with recipient's email address
msg['Subject'] = 'Test Email'
body = 'This is a test email.'
msg.attach(MIMEText(body, 'plain'))
try:
server = smtplib.SMTP(smtp_server, smtp_port)
server.set_debuglevel(1) # Enable debug output
server.starttls() # Enable TLS encryption
# Log in to SMTP server
server.login(username, password)
# Send email
text = msg.as_string()
server.sendmail(msg['From'], msg['To'], text)
print("Email sent successfully")
except Exception as e:
print(f"Failed to send email: {e}")
finally:
# Disconnect from the server
server.quit()
Explanation:
- SMTP server configuration: Replace
smtp_server
,username
, andpassword
with your SMTP server address, username, and password. - Email content: Replace
msg['From']
andmsg['To']
with actual sender and recipient email addresses. - Exception handling: Uses a
try-except-finally
block to handle potential exceptions, ensuring graceful closure of the SMTP connection even if sending the email fails.
This Python script connects to the specified SMTP server, sends a test email using TLS encryption, and handles exceptions that may occur during the process.