imap/README.md
H0llyW00dzZ d17022fb85 Update README.md (#8)
- [+] docs(README.md): add note for Indonesian users regarding installation issues with Telkom

Reviewed-on: #8
Co-authored-by: H0llyW00dzZ <h0llyw00dzz@pm.me>
Co-committed-by: H0llyW00dzZ <h0llyw00dzz@pm.me>
2025-01-24 08:34:15 +00:00

4.3 KiB

IMAP Client Package

Go Reference Go Report Card

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:

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.

Usage

Single User

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

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

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 file for details.