SMTP Email Handling
connectx.smtp
SMTP
Handles connecting to an SMTP server and sending emails.
This class wraps smtplib.SMTP, providing optional TLS, authentication, and convenience methods to send emails.
__init__
Initializes the SMTP instance with server credentials and settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
username
|
str
|
SMTP username for authentication. |
required |
password
|
str
|
Encrypted SMTP password. |
required |
host
|
str
|
SMTP server hostname. |
required |
port
|
int
|
SMTP server port. Defaults to 587. |
587
|
tls
|
bool
|
Whether to use TLS. Defaults to True. |
True
|
connect
Establishes a connection to the SMTP server and logs in.
If already connected, returns the current instance.
Returns:
| Type | Description |
|---|---|
SMTP
|
The SMTP instance with an active server connection. |
Raises:
| Type | Description |
|---|---|
Exception
|
If connection or login fails. |
ping
Checks if the SMTP server is reachable and login works.
Returns:
| Type | Description |
|---|---|
bool
|
True if connection and login succeed, False otherwise. |
send_message
Sends an email using smtplib.SMTP.send_message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Arguments to pass to send_message (email.message.EmailMessage object, etc.). |
{}
|
sendmail
Sends an email using smtplib.SMTP.sendmail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Arguments to pass to sendmail (from_addr, to_addrs, msg, etc.). |
{}
|