2025-05-16 19:23:27 +02:00
# micromail
2025-05-19 14:13:13 +02:00
[](https://code.edgarpierre.fr/edpibu/micromail/actions?workflow=release.yaml)
[](https://code.edgarpierre.fr/edpibu/micromail/releases/latest)
2025-05-19 14:11:44 +02:00
2025-05-16 19:25:00 +02:00

2025-05-17 20:10:14 +02:00
Tiny SMTP client for Micropython. See [examples ](examples ) for usage.
2025-05-16 23:02:56 +02:00
Inspired from [shawwwn/uMail ](https://github.com/shawwwn/uMail ).
2025-05-17 20:46:38 +02:00
2025-05-17 20:53:53 +02:00
## Installation
### Manual
2025-05-17 21:08:03 +02:00
Copy the `micromail.py` file to your device. You will need to install the `datetime` library as well.
2025-05-17 20:53:53 +02:00
### MIP
```python
>>> import mip
>>> mip.install("https://code.edgarpierre.fr/edpibu/micromail/releases/download/latest/")
```
2025-05-17 20:46:38 +02:00
## Documentation
### Class MicromailClient
2025-05-19 13:55:34 +02:00
- `class micromail.MicromailClient(host: str, port: int = 0, ssl: bool = False, starttls: bool = False)`
2025-05-17 20:46:38 +02:00
2025-05-19 13:55:34 +02:00
Create a new SMTP client.
2025-05-17 20:46:38 +02:00
2025-05-19 13:55:34 +02:00
- `MicromailClient.connect()`
2025-05-17 20:46:38 +02:00
2025-05-19 13:55:34 +02:00
Establish a connection to the SMTP server. The connection is kept alive until `MicromailClient.quit()` is called.
2025-05-17 20:46:38 +02:00
2025-05-19 13:55:34 +02:00
- `MicromailClient.login(username: str, password: str)`
2025-05-17 20:46:38 +02:00
2025-05-19 13:55:34 +02:00
Login to the SMTP server with the provided username and password. `PLAIN` and `LOGIN` authentication methods are currently supported.
2025-05-17 20:46:38 +02:00
2025-05-19 13:55:34 +02:00
- `MicromailClient.write(data: str | bytes)`
2025-05-17 20:46:38 +02:00
2025-05-19 13:55:34 +02:00
Write `data` of type `bytearray` on the server.
2025-05-17 20:46:38 +02:00
2025-05-19 13:55:34 +02:00
- `MicromailClient.send_command(*cmd: str | bytes, code: int | list[int] = []) -> tuple[int, list[str]]`
2025-05-17 20:46:38 +02:00
2025-05-19 13:55:34 +02:00
Send command to the SMTP server; returns status code and list of response lines.
2025-05-17 20:46:38 +02:00
2025-05-19 13:55:34 +02:00
- `MicromailClient.read_res(cmd: str = "", code: int | list[int] = []) -> tuple[int, list[str]]`
Read response from the SMTP server; returns status code and list of response lines.
- `MicromailClient.ehlo() -> list[str]`
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: str | None = None)`
Create a new email; sender defaults to username.
- `MicromailClient.headers(headers: dict[str, str])`
Define email headers; supported headers are `date` (`str` , defaults to current date), `from` (defaults to `new_message` `sender` ), `to` (defaults to `new_message` `to` ), `subject` (`str` ).
- `MicromailClient.write_line(content: str)`
Write message line; `\r\n` is appended to `content` .
- `MicromailClient.send()`
Send current email.
- `MicromailClient.quit()`
Quit the SMTP server and disconnect from socket.
2025-05-17 20:46:38 +02:00
### Functions
2025-05-19 13:55:34 +02:00
- `format_date(date: datetime) -> str`
Return date formatted as email date string.
### Exceptions
- `Error`
Standard module error.
- `NoSocketError`
Raised when an operation on the socket is asked but the client is not connected to any socket.
- `SMTPError(cmd: str, code: int, message: str)`
Raised when an unexpected SMTP response code is received.