emersion / go-imap

📥 An IMAP library for clients and servers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

v1: move doesn't move emails in Gmail IMAP

captv89 opened this issue · comments

I have the below function to move the mails to a Destination Folder/mailbox

// moveEmail moves an email to the specified mailbox folder.
func moveEmail(c *client.Client, msg *imap.Message, destMailbox string) error {
	// Check if the destination mailbox exists else create it
	if _, err := c.Select(destMailbox, false); err != nil {
		log.Printf("Destination mailbox not found: %v", err)
		log.Printf("Creating destination mailbox: %s", destMailbox)
		if err := c.Create(destMailbox); err != nil {
			log.Printf("Error creating destination mailbox: %v", err)
			return err
		}
	}

	// Move the email from inbox to destination mailbox
	seqSet := new(imap.SeqSet)
	seqSet.AddNum(msg.Uid)
	log.Printf("Moving email with UID: %d to %s", msg.Uid, destMailbox)

	err := c.Move(seqSet, destMailbox)
	if err != nil {
		log.Printf("Error moving email: %v", err)
		return err
	}
	return nil
}

However, the c.Move doesn't perform the action, the mails remain in the same Label (as they say in Gmail) / mailbox.

moveEmail function is called from another function as below:

// Move to the "Recognised" mailbox folder
if err := moveEmail(client, msg, "Recognised"); err != nil {
  log.Printf("Error moving email: %v\n", err)
  return err
}

Here msg is *imap.Message obtained as below

seqset := new(imap.SeqSet)
seqset.AddRange(1, inbox.Messages)

items := []imap.FetchItem{imap.FetchEnvelope, imap.FetchUid}

messages := make(chan *imap.Message, inbox.Messages)

if err := client.Fetch(seqset, items, messages); err != nil {
log.Fatal(err)
}

// Iterate through the list of messages and move them based on the subject
for msg := range messages {
// Contains some checks and then finally the call to moveEmails
}