Update README and MicromailClient to enhance documentation and ensure socket closure on quit

This commit is contained in:
Edgar P. Burkhart 2025-05-17 20:46:38 +02:00
parent b9b97040d1
commit 243c65fb49
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
2 changed files with 46 additions and 0 deletions

View file

@ -5,3 +5,48 @@
Tiny SMTP client for Micropython. See [examples](examples) for usage.
Inspired from [shawwwn/uMail](https://github.com/shawwwn/uMail).
## Documentation
### Class MicromailClient
`class micromail.MicromailClient(host: str, port: int, ssl: bool=False, starttls: bool=False)`
: Create a new SMTP client.
`MicromailClient.connect()`
: Establish a connection to the SMTP server. The connection is kept alive until `MicromailClient.quit()` is called.
`MicromailClient.login(username: str, password: str)`
: Login to the SMTP server with the provided username and password. `PLAIN` and `LOGIN` authentication methods are currently supported.
`MicromailClient.write(data: bytearray)`
: Write `data` of type `bytearray` on the server.
`MicromailClient.send_command(*cmd: bytearray) -> code: bytearray, response: list[bytearray]`
: Send command to the SMTP server; returns status code and response.
`MicromailClient.ehlo() -> res: list[bytearray]`
: Send SMTP EHLO command to the server and return response.
`MicromailClient.starttls()`
: Send SMTP STARTTLS command and enable SSL on socket.
`MicromailClient.new_message(to: str|list[str], sender: None|str=None)`
: Create a new email; sender defaults to username.
`MicromailClient.headers(headers:dict={})`
: Define email headers; supported headers are `date` (`str|datetime`, defaults to `datetime.now()`), `from` (`str`, defaults to `new_message` `sender`), `to` (`str|list[str]`, defaults to `new_message` `to`), `subject` (`str`).
`MicromailClient.write_line(content:str|bytearray)`
: Write message line; `\r\n` is appended to `content`.
`MicromailClient.send()`
: Send current email.
`MicromailClient.quit()`
: Quit the SMTP server and disconnect from socket.
### Functions
`format_date(date: datetime)`
: Return date formatted as email date string.

View file

@ -198,6 +198,7 @@ class MicromailClient:
if code != b"221":
print(f"Error: got code {code} on QUIT")
print("Disconnected from server")
self.socket.close()
MONTHS = [