H0llyW00dzZ
5cab29c344
- [+] feat(README.md): add functionality to list available mailboxes in the package description and usage examples - [+] feat(client): implement ListMailboxes method to retrieve all available mailboxes for the connected user - [+] test(client): add unit test for ListMailboxes method to validate mailbox retrieval functionality Reviewed-on: #14 Co-authored-by: H0llyW00dzZ <h0llyw00dzz@pm.me> Co-committed-by: H0llyW00dzZ <h0llyw00dzz@pm.me>
232 lines
5.5 KiB
Markdown
232 lines
5.5 KiB
Markdown
# IMAP Client Package
|
|
|
|
[![Go Reference](https://pkg.go.dev/badge/git.b0zal.io/H0llyW00dzZ/imap.svg)](https://pkg.go.dev/git.b0zal.io/H0llyW00dzZ/imap) [![Go Report Card](https://goreportcard.com/badge/git.b0zal.io/H0llyW00dzZ/imap)](https://goreportcard.com/report/git.b0zal.io/H0llyW00dzZ/imap)
|
|
|
|
This package provides a simple interface to manage IMAP connections for single or multiple users. It allows you to connect to an IMAP server, list messages, export messages, manage multiple user accounts, and list available mailboxes.
|
|
|
|
## Features
|
|
|
|
- Connect and disconnect from an IMAP server
|
|
- List messages in a specified mailbox
|
|
- Export messages to various formats
|
|
- Manage multiple users with separate IMAP clients
|
|
- **New**: List all available mailboxes
|
|
|
|
## Installation
|
|
|
|
To install the package, use `go get`:
|
|
|
|
```bash
|
|
go get git.b0zal.io/H0llyW00dzZ/imap
|
|
```
|
|
> [!NOTE]
|
|
> If you're in `Indonesia` and using an internet provider like `Telkom (known as Indihome)`, and you encounter issues installing the package, try using a [VPN](https://en.wikipedia.org/wiki/Virtual_private_network).
|
|
|
|
## Usage
|
|
|
|
### Single User
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"git.b0zal.io/H0llyW00dzZ/imap/client"
|
|
)
|
|
|
|
func main() {
|
|
config := &client.Config{
|
|
Username: "user@example.com",
|
|
Password: "password",
|
|
Server: "imap.example.com:993",
|
|
InsecureSkipVerify: true,
|
|
}
|
|
|
|
imapClient := client.NewIMAP(config)
|
|
|
|
err := imapClient.Connect()
|
|
if err != nil {
|
|
log.Fatalf("Failed to connect: %v", err)
|
|
}
|
|
defer imapClient.Disconnect()
|
|
|
|
// New: List all available mailboxes
|
|
mailboxes, err := imapClient.ListMailboxes()
|
|
if err != nil {
|
|
log.Fatalf("Failed to list mailboxes: %v", err)
|
|
}
|
|
fmt.Println("Mailboxes:", mailboxes)
|
|
|
|
messageConfig := client.MessageConfig{
|
|
GrabID: true,
|
|
GrabFrom: true,
|
|
GrabSubject: true,
|
|
GrabBody: true,
|
|
}
|
|
|
|
messages, err := imapClient.ListMessages("INBOX", 10, messageConfig)
|
|
if err != nil {
|
|
log.Fatalf("Failed to list messages: %v", err)
|
|
}
|
|
|
|
for _, msg := range messages {
|
|
fmt.Printf("ID: %s, From: %v, Subject: %s, Body: %s\n", msg[client.KeyID], msg[client.KeyFrom], msg[client.KeySubject], msg[client.KeyBody])
|
|
}
|
|
}
|
|
```
|
|
|
|
### Multiple Users
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"git.b0zal.io/H0llyW00dzZ/imap/client"
|
|
)
|
|
|
|
func main() {
|
|
multiUserIMAP := client.NewMultiUserIMAP()
|
|
|
|
multiUserIMAP.AddUser("user1@example.com", "password1", "imap.example.com:993", true)
|
|
multiUserIMAP.AddUser("user2@example.com", "password2", "imap.example.com:993", true)
|
|
|
|
err := multiUserIMAP.ConnectUser("user1@example.com")
|
|
if err != nil {
|
|
log.Fatalf("Failed to connect user1: %v", err)
|
|
}
|
|
defer multiUserIMAP.DisconnectUser("user1@example.com")
|
|
|
|
// New: List all available mailboxes for user1
|
|
client, err := multiUserIMAP.GetClient("user1@example.com")
|
|
if err != nil {
|
|
log.Fatalf("Failed to get client for user1: %v", err)
|
|
}
|
|
mailboxes, err := client.ListMailboxes()
|
|
if err != nil {
|
|
log.Fatalf("Failed to list mailboxes for user1: %v", err)
|
|
}
|
|
fmt.Println("User1 Mailboxes:", mailboxes)
|
|
|
|
messageConfig := client.MessageConfig{
|
|
GrabID: true,
|
|
GrabFrom: true,
|
|
GrabSubject: true,
|
|
GrabBody: true,
|
|
}
|
|
|
|
messages, err := multiUserIMAP.ListUserMessages("user1@example.com", "INBOX", 10, messageConfig)
|
|
if err != nil {
|
|
log.Fatalf("Failed to list messages for user1: %v", err)
|
|
}
|
|
|
|
for _, msg := range messages {
|
|
fmt.Printf("ID: %s, From: %v, Subject: %s, Body: %s\n", msg[client.KeyID], msg[client.KeyFrom], msg[client.KeySubject], msg[client.KeyBody])
|
|
}
|
|
}
|
|
```
|
|
|
|
### Exporting Messages
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
|
|
"git.b0zal.io/H0llyW00dzZ/imap/client"
|
|
"git.b0zal.io/H0llyW00dzZ/imap/export"
|
|
)
|
|
|
|
func main() {
|
|
config := &client.Config{
|
|
Username: "user@example.com",
|
|
Password: "password",
|
|
Server: "imap.example.com:993",
|
|
InsecureSkipVerify: true,
|
|
}
|
|
|
|
imapClient := client.NewIMAP(config)
|
|
|
|
err := imapClient.Connect()
|
|
if err != nil {
|
|
log.Fatalf("Failed to connect: %v", err)
|
|
}
|
|
defer imapClient.Disconnect()
|
|
|
|
messageConfig := client.MessageConfig{
|
|
GrabID: true,
|
|
GrabFrom: true,
|
|
GrabSubject: true,
|
|
GrabBody: true,
|
|
}
|
|
|
|
file, err := os.Create("messages.json")
|
|
if err != nil {
|
|
log.Fatalf("Failed to create file: %v", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
exporter := &export.JSONExporter{Encoder: export.DefaultJSONEncoder}
|
|
err = imapClient.ExportMessagesTo(file, exporter, "INBOX", 10, messageConfig)
|
|
if err != nil {
|
|
log.Fatalf("Failed to export messages: %v", err)
|
|
}
|
|
}
|
|
```
|
|
|
|
### List All Available Mailboxes
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"git.b0zal.io/H0llyW00dzZ/imap/client"
|
|
)
|
|
|
|
func main() {
|
|
config := &client.Config{
|
|
Username: "user@example.com",
|
|
Password: "password",
|
|
Server: "imap.example.com:993",
|
|
InsecureSkipVerify: true,
|
|
}
|
|
|
|
imapClient := client.NewIMAP(config)
|
|
|
|
err := imapClient.Connect()
|
|
if err != nil {
|
|
log.Fatalf("Failed to connect: %v", err)
|
|
}
|
|
defer imapClient.Disconnect()
|
|
|
|
// List all available mailboxes
|
|
mailboxes, err := imapClient.ListMailboxes()
|
|
if err != nil {
|
|
log.Fatalf("Failed to list mailboxes: %v", err)
|
|
}
|
|
fmt.Println("Mailboxes:", mailboxes)
|
|
}
|
|
```
|
|
|
|
## TODO
|
|
|
|
- Implement functionality to send emails.
|
|
- Add support for folder management (create, delete, rename).
|
|
- Enhance error handling and logging.
|
|
- Support for message search and filtering.
|
|
- Implement message deletion and flagging.
|
|
- Add more tests for edge cases and concurrent access.
|
|
|
|
## License
|
|
|
|
This project is licensed under the BSD 3-Clause License. See the [LICENSE](LICENSE) file for details.
|