Add IMAP settings to configuration and enhance email processing logic
This commit is contained in:
12
README.md
12
README.md
@@ -42,6 +42,16 @@ go build -o paraclub-ai-mailer ./cmd/paraclub-ai-mailer
|
|||||||
|
|
||||||
The application uses a YAML configuration file (`config.yaml`) with the following structure:
|
The application uses a YAML configuration file (`config.yaml`) with the following structure:
|
||||||
|
|
||||||
|
### IMAP Settings
|
||||||
|
- `server`: IMAP server hostname (e.g., "imap.gmail.com")
|
||||||
|
- `port`: IMAP port (typically 993 for TLS)
|
||||||
|
- `username`: Your email address
|
||||||
|
- `password`: Email password (use ${IMAP_PASSWORD} to load from environment)
|
||||||
|
- `mailbox_in`: Mailbox to check for new emails (e.g., "INBOX")
|
||||||
|
- `draft_box`: Folder to save AI-generated drafts (e.g., "Drafts")
|
||||||
|
- `processed_box`: Folder to move processed emails to (they will also be marked as read)
|
||||||
|
- `use_tls`: Whether to use TLS for connection (recommended: true)
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
imap:
|
imap:
|
||||||
server: "imap.example.com"
|
server: "imap.example.com"
|
||||||
@@ -111,7 +121,7 @@ Or specify a custom config file path:
|
|||||||
- Retrieves HTML content from configured URLs
|
- Retrieves HTML content from configured URLs
|
||||||
- Sends combined content to OpenRouter AI for response generation
|
- Sends combined content to OpenRouter AI for response generation
|
||||||
- Saves the AI-generated response as a draft email
|
- Saves the AI-generated response as a draft email
|
||||||
- Marks the original email as processed
|
- Marks the original email as read and moves it to the configured processed folder
|
||||||
|
|
||||||
## Logging
|
## Logging
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ imap:
|
|||||||
password: "${IMAP_PASSWORD}" # Will be read from environment variable
|
password: "${IMAP_PASSWORD}" # Will be read from environment variable
|
||||||
mailbox_in: "INBOX"
|
mailbox_in: "INBOX"
|
||||||
draft_box: "Drafts"
|
draft_box: "Drafts"
|
||||||
|
processed_box: "Processed" # Folder where processed emails will be moved
|
||||||
use_tls: true
|
use_tls: true
|
||||||
|
|
||||||
ai:
|
ai:
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ type IMAPConfig struct {
|
|||||||
Password string `yaml:"password"`
|
Password string `yaml:"password"`
|
||||||
MailboxIn string `yaml:"mailbox_in"`
|
MailboxIn string `yaml:"mailbox_in"`
|
||||||
DraftBox string `yaml:"draft_box"`
|
DraftBox string `yaml:"draft_box"`
|
||||||
|
ProcessedBox string `yaml:"processed_box"`
|
||||||
UseTLS bool `yaml:"use_tls"`
|
UseTLS bool `yaml:"use_tls"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import (
|
|||||||
type IMAPClient struct {
|
type IMAPClient struct {
|
||||||
client *client.Client
|
client *client.Client
|
||||||
config config.IMAPConfig
|
config config.IMAPConfig
|
||||||
processed map[string]bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MessageLiteral implements imap.Literal for draft messages
|
// MessageLiteral implements imap.Literal for draft messages
|
||||||
@@ -67,7 +66,6 @@ func New(cfg config.IMAPConfig) (*IMAPClient, error) {
|
|||||||
return &IMAPClient{
|
return &IMAPClient{
|
||||||
client: c,
|
client: c,
|
||||||
config: cfg,
|
config: cfg,
|
||||||
processed: make(map[string]bool),
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,8 +135,9 @@ func (ic *IMAPClient) FetchUnprocessedEmails() ([]Email, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get all messages in the inbox that haven't been seen yet
|
||||||
criteria := imap.NewSearchCriteria()
|
criteria := imap.NewSearchCriteria()
|
||||||
criteria.WithoutFlags = []string{"\\Seen", "Processed"}
|
criteria.WithoutFlags = []string{"\\Seen"}
|
||||||
|
|
||||||
uids, err := ic.client.Search(criteria)
|
uids, err := ic.client.Search(criteria)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -170,11 +169,6 @@ func (ic *IMAPClient) FetchUnprocessedEmails() ([]Email, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip if already processed
|
|
||||||
if ic.processed[msg.Envelope.MessageId] {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip if no message ID
|
// Skip if no message ID
|
||||||
if msg.Envelope.MessageId == "" {
|
if msg.Envelope.MessageId == "" {
|
||||||
continue
|
continue
|
||||||
@@ -202,8 +196,6 @@ func (ic *IMAPClient) FetchUnprocessedEmails() ([]Email, error) {
|
|||||||
From: from,
|
From: from,
|
||||||
Body: bodyBuilder.String(),
|
Body: bodyBuilder.String(),
|
||||||
})
|
})
|
||||||
|
|
||||||
ic.processed[msg.Envelope.MessageId] = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := <-done; err != nil {
|
if err := <-done; err != nil {
|
||||||
@@ -218,7 +210,13 @@ func (ic *IMAPClient) SaveDraft(email Email, response string) error {
|
|||||||
return fmt.Errorf("failed to ensure IMAP connection: %v", err)
|
return fmt.Errorf("failed to ensure IMAP connection: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := ic.client.Select(ic.config.DraftBox, false)
|
// Get proper folder path and ensure it exists
|
||||||
|
draftFolder, err := ic.ensureFolder(ic.config.DraftBox)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to ensure draft folder exists: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = ic.client.Select(draftFolder, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to select draft box: %v", err)
|
return fmt.Errorf("failed to select draft box: %v", err)
|
||||||
}
|
}
|
||||||
@@ -255,9 +253,9 @@ func (ic *IMAPClient) SaveDraft(email Email, response string) error {
|
|||||||
pos: 0,
|
pos: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the draft
|
// Save the draft to the proper folder path
|
||||||
flags := []string{"\\Draft"}
|
flags := []string{"\\Draft"}
|
||||||
if err := ic.client.Append(ic.config.DraftBox, flags, time.Now(), literal); err != nil {
|
if err := ic.client.Append(draftFolder, flags, time.Now(), literal); err != nil {
|
||||||
return fmt.Errorf("failed to append draft: %v", err)
|
return fmt.Errorf("failed to append draft: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -420,32 +418,106 @@ func fallbackExtractContent(body string) string {
|
|||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ensureFolder makes sure a folder exists and returns its full path using proper delimiters
|
||||||
|
func (ic *IMAPClient) ensureFolder(folderName string) (string, error) {
|
||||||
|
// List all mailboxes to get the delimiter
|
||||||
|
mailboxes := make(chan *imap.MailboxInfo, 10)
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
done <- ic.client.List("", "*", mailboxes)
|
||||||
|
}()
|
||||||
|
|
||||||
|
var delimiter string
|
||||||
|
for m := range mailboxes {
|
||||||
|
delimiter = m.Delimiter
|
||||||
|
break // We just need the first one to get the delimiter
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := <-done; err != nil {
|
||||||
|
return "", fmt.Errorf("failed to list mailboxes: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if delimiter == "" {
|
||||||
|
delimiter = "/" // fallback to common delimiter
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace any forward slashes with the server's delimiter
|
||||||
|
folderPath := strings.ReplaceAll(folderName, "/", delimiter)
|
||||||
|
|
||||||
|
// Try to create the folder if it doesn't exist
|
||||||
|
if err := ic.client.Create(folderPath); err != nil {
|
||||||
|
// Ignore errors if the folder already exists
|
||||||
|
logger.WithFields(logrus.Fields{
|
||||||
|
"folder": folderPath,
|
||||||
|
"error": err,
|
||||||
|
}).Debug("Folder creation failed (might already exist)")
|
||||||
|
}
|
||||||
|
|
||||||
|
return folderPath, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (ic *IMAPClient) MarkAsProcessed(email Email) error {
|
func (ic *IMAPClient) MarkAsProcessed(email Email) error {
|
||||||
if err := ic.ensureConnection(); err != nil {
|
if err := ic.ensureConnection(); err != nil {
|
||||||
return fmt.Errorf("failed to ensure IMAP connection: %v", err)
|
return fmt.Errorf("failed to ensure IMAP connection: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := ic.client.Select(ic.config.MailboxIn, false)
|
if ic.config.ProcessedBox == "" {
|
||||||
if err != nil {
|
return fmt.Errorf("processed_box not configured")
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get proper folder path and ensure it exists
|
||||||
|
processedFolder, err := ic.ensureFolder(ic.config.ProcessedBox)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to ensure processed folder exists: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select source mailbox
|
||||||
|
_, err = ic.client.Select(ic.config.MailboxIn, false)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to select source mailbox: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the email by Message-Id
|
||||||
criteria := imap.NewSearchCriteria()
|
criteria := imap.NewSearchCriteria()
|
||||||
criteria.Header.Set("Message-Id", email.ID)
|
criteria.Header.Set("Message-Id", email.ID)
|
||||||
|
|
||||||
uids, err := ic.client.Search(criteria)
|
uids, err := ic.client.Search(criteria)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("search failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(uids) == 0 {
|
if len(uids) == 0 {
|
||||||
return fmt.Errorf("email not found")
|
return fmt.Errorf("email not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Move the message to processed folder
|
||||||
seqSet := new(imap.SeqSet)
|
seqSet := new(imap.SeqSet)
|
||||||
seqSet.AddNum(uids...)
|
seqSet.AddNum(uids...)
|
||||||
|
|
||||||
return ic.client.Store(seqSet, imap.FormatFlagsOp(imap.AddFlags, true), []interface{}{"Processed"}, nil)
|
// Mark as read before moving
|
||||||
|
item := imap.FormatFlagsOp(imap.AddFlags, true)
|
||||||
|
flags := []interface{}{imap.SeenFlag}
|
||||||
|
if err := ic.client.Store(seqSet, item, flags, nil); err != nil {
|
||||||
|
return fmt.Errorf("failed to mark message as read: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy to processed folder using the proper path
|
||||||
|
if err := ic.client.Copy(seqSet, processedFolder); err != nil {
|
||||||
|
return fmt.Errorf("failed to copy to processed folder: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete from source folder
|
||||||
|
item = imap.FormatFlagsOp(imap.AddFlags, true)
|
||||||
|
flags = []interface{}{imap.DeletedFlag}
|
||||||
|
if err := ic.client.Store(seqSet, item, flags, nil); err != nil {
|
||||||
|
return fmt.Errorf("failed to mark message as deleted: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ic.client.Expunge(nil); err != nil {
|
||||||
|
return fmt.Errorf("failed to expunge message: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ic *IMAPClient) Close() error {
|
func (ic *IMAPClient) Close() error {
|
||||||
|
|||||||
Reference in New Issue
Block a user