secure-smtplib (Python 2)
secure-smtplib is an abandoned Python 2 library (last released in 2015) that provides subclasses for the standard `smtplib` module to handle secure SMTP connections. Its functionality has been largely superseded by built-in improvements to the standard `smtplib` module in Python 3, which offers robust SSL/TLS support through `SMTP_SSL` and `starttls()` methods, often in conjunction with the `ssl` module. This library is not recommended for new projects or Python 3 environments due to its age and lack of maintenance.
Warnings
- breaking This library is designed for Python 2 and is not compatible with Python 3. Attempting to use it in a Python 3 environment will likely result in import errors or unexpected behavior.
- deprecated The functionality provided by `secure-smtplib` for secure SMTP connections (SSL/TLS) has been integrated into and significantly improved within Python's standard `smtplib` module since Python 3.3. The standard library now offers `smtplib.SMTP_SSL` for implicit TLS and `smtplib.SMTP().starttls()` for explicit TLS, along with `ssl.create_default_context()` for robust security configuration.
- gotcha The library is unmaintained (last updated in 2015) and specifically targets Python 2. Using unmaintained software, especially for network communication involving credentials, can introduce significant security vulnerabilities.
Install
-
pip install secure-smtplib
Imports
- SecureSMTP
from secure_smtplib import SecureSMTP
- SecureSMTP_SSL
from secure_smtplib import SecureSMTP_SSL
Quickstart
import smtplib
import ssl
import os
from email.message import EmailMessage
# NOTE: This quickstart uses Python 3's built-in smtplib and ssl modules,
# which are the recommended and secure way to send emails. The `secure-smtplib`
# library itself is deprecated and not suitable for modern Python or secure applications.
SMTP_SERVER = os.environ.get('SMTP_HOST', 'smtp.example.com')
SMTP_PORT = int(os.environ.get('SMTP_PORT', '465')) # Use 587 for STARTTLS
EMAIL_ADDRESS = os.environ.get('SMTP_USERNAME', 'your_email@example.com')
EMAIL_PASSWORD = os.environ.get('SMTP_PASSWORD', 'your_app_password') # Use app passwords if 2FA is enabled
msg = EmailMessage()
msg['Subject'] = 'Test Email from Python'
msg['From'] = EMAIL_ADDRESS
msg['To'] = 'recipient@example.com'
msg.set_content('This is a test email sent securely using Python 3 smtplib.')
context = ssl.create_default_context()
try:
with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT, context=context) as server:
server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
server.send_message(msg)
print("Email sent successfully!")
except smtplib.SMTPAuthenticationError as e:
print(f"Authentication error: {e}. Check your username and password/app password.")
except smtplib.SMTPServerDisconnected as e:
print(f"Server disconnected unexpectedly: {e}. Check host and port.")
except Exception as e:
print(f"An error occurred: {e}")