From 243c65fb49051bfff5bba9196edd1d25ae52b720 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Sat, 17 May 2025 20:46:38 +0200 Subject: [PATCH] Update README and MicromailClient to enhance documentation and ensure socket closure on quit --- README.md | 45 +++++++++++++++++++++++++++++++++++++++++++ micromail/__init__.py | 1 + 2 files changed, 46 insertions(+) diff --git a/README.md b/README.md index 5c7cff7..754c2f7 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/micromail/__init__.py b/micromail/__init__.py index fa8a8be..0a5973b 100644 --- a/micromail/__init__.py +++ b/micromail/__init__.py @@ -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 = [