H0llyW00dzZ
ab122177c2
- [+] feat(export): add message exporting functionality with JSON support - [+] feat(readme): update documentation to include message export feature - [+] refactor(.gitignore): change ignored files from emails.csv to test.csv and add test.json - [+] refactor(client): update message handling to use map structure instead of MessageDetails struct Reviewed-on: #4 Co-authored-by: H0llyW00dzZ <h0llyw00dzz@pm.me> Co-committed-by: H0llyW00dzZ <h0llyw00dzz@pm.me>
174 lines
4.0 KiB
Markdown
174 lines
4.0 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, and manage multiple user accounts.
|
|
|
|
## 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
|
|
|
|
## Installation
|
|
|
|
To install the package, use `go get`:
|
|
|
|
```bash
|
|
go get git.b0zal.io/H0llyW00dzZ/imap
|
|
```
|
|
|
|
## 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()
|
|
|
|
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")
|
|
|
|
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)
|
|
}
|
|
}
|
|
```
|
|
|
|
## 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.
|