Home >Backend Development >Golang >How to delete IMAP mail using Golang

How to delete IMAP mail using Golang

PHPz
PHPzOriginal
2023-04-05 10:29:261411browse

IMAP is a common network protocol used to send and receive email. In Golang, mails can be deleted easily using IMAP. This article will introduce how to delete IMAP emails using Golang.

Step 1: Import the necessary libraries

First, you need to import the "net/imap" and "fmt" libraries in Golang. The import code is as follows:

import (
    "net/imap"
    "fmt"
)

Step 2: Establish an IMAP connection

Next, you need to establish a connection with the IMAP server. You need to provide information such as server address, username and password. The connection can be established using the "Dial" method of the IMAP library.

conn, err := imap.DialTLS(serverAddr, nil)
if err != nil {
    log.Fatal(err)
}

Step 3: Log in to the IMAP server

After the connection is established, you need to log in to the IMAP server. You need to provide information such as username and password. You can log in to the IMAP server using the "Login" method of the IMAP library.

_, err = conn.Login(username, password)
if err != nil {
    log.Fatal(err)
}

Step 4: Select the email address

After successfully logging in, you need to select the email address from which you want to delete emails. Mailboxes can be selected using the "Select" method of the IMAP library.

_, err = conn.Select("INBOX", false)
if err != nil {
    log.Fatal(err)
}

Step 5: Search for emails

After selecting the mailbox, you need to find the email you want to delete. Mail can be searched using the "Search" method of the IMAP library. You can search based on email subject, sender, etc.

// search for mails with 'subject' in subject and 'from' in sender
criteria := imap.NewSearchCriteria()
criteria.Header.Set("subject", "subject")
criteria.Header.Set("from", "from")

// execute search
uids, err := conn.Search(criteria)
if err != nil {
    log.Fatal(err)
}

Step 6: Mark the message as deleted

After you find the message you want to delete, you need to mark the message as deleted. Messages can be tagged using the "Store" method of the IMAP library. In this method, you need to specify the sequence number of the message to be tagged and the tag type.

// mark mails as deleted
seqSet := new(imap.SeqSet)
seqSet.AddNum(uids...)
delFlags := []interface{}{imap.DeletedFlag}
err = conn.Store(seqSet, "+FLAGS", delFlags, nil)
if err != nil {
    log.Fatal(err)
}

Step Seven: Delete Flagged Messages

Finally, you need to delete the flagged messages. Flagged messages can be deleted using the "Expunge" method of the IMAP library.

// delete mails
if err = conn.Expunge(nil); err != nil {
    log.Fatal(err)
}

Complete code example

The following is a complete example code for deleting IMAP emails using Golang:

package main

import (
    "fmt"
    "log"
    "net/mail"

    "github.com/emersion/go-imap"
    "github.com/emersion/go-imap/client"
    "github.com/emersion/go-message/charset"
)

func main() {
    // Connect to the server
    c, err := client.DialTLS("mail.example.com:993", nil)
    if err != nil {
        log.Fatal(err)
    }
    defer c.Logout()

    // Login
    if err := c.Login("user@example.com", "password"); err != nil {
        log.Fatal(err)
    }

    // Select mailbox
    mbox, err := c.Select("INBOX", false)
    if err != nil {
        log.Fatal(err)
    }

    // Search for messages
    charsetReader := charset.Reader
    msgs := make(chan *imap.Message, 10)
    done := make(chan error, 1)
    go func() {
        done <- c.List("", "INBOX", msgs)
    }()
    for msg := range msgs {
        r := msg.GetBody(&imap.BodySectionName{section})
        if r == nil {
            continue
        }
        if _, err := mail.ReadMessage(charsetReader(r)); err != nil {
            log.Fatal(err)
        }

        // Delete message
        seqSet := new(imap.SeqSet)
        seqSet.AddNum(msg.SeqNum)
        item := imap.FormatFlagsOp(imap.AddFlags, true)
        flags := []interface{}{imap.DeletedFlag}
        if err := c.Store(seqSet, item, flags, nil); err != nil {
            log.Fatal(err)
        }
    }

    // Expunge deleted messages
    if err := c.Expunge(nil); err != nil {
        log.Fatal(err)
    }

    if err := <-done; err != nil {
        log.Fatal(err)
    }

    fmt.Println("Done!")
}

The above is the process and complete code example for deleting IMAP emails using Golang.

The above is the detailed content of How to delete IMAP mail using Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn