func (e *Email) DeleteMany(uids []uint32) (err error) { seqSet := new(imap.SeqSet) for i := 0; i < len(uids); i++ { seqSet.AddNum(uids[i]) } if err != nil { return err } flags := imap.NewFlagSet(`\Deleted`, `\Seen`) cmd, err := imap.Wait(e.conn.UIDStore(seqSet, "+FLAGS", flags)) fmt.Println(cmd) if err != nil { return err } cmd, err = imap.Wait(e.conn.Expunge(nil)) fmt.Println(cmd) if err != nil { return err } cmd, err = imap.Wait(e.conn.Close(true)) fmt.Println(cmd) if err != nil { return err } return nil }
func fetchMessage(c *imap.Client, id uint32) { seq_set, err := imap.NewSeqSet(fmt.Sprintf("%d", id)) if err != nil { panic(err) } cmd, err := imap.Wait(c.Fetch(seq_set, "ALL")) if err != nil { panic(err) } res, err := cmd.Result(0) if err != nil { panic(err) } //fmt.Println(reflect.TypeOf(cmd.Data[0].Fields[2])) fmt.Println(cmd.Data[0].Fields[2]) fmt.Println(res.MessageInfo()) }
func NewEmail(server string, user string, pw string, user_id int) (e *Email) { e = new(Email) e.User_Id = user_id var err error e.conn, err = e.login(server, user, pw) if err != nil { panic(err) } _, err = imap.Wait(e.conn.Select("INBOX", false)) if err != nil { panic(err) } return e }
func main() { var ( server string user string pw string ) flag.StringVar(&server, "server", "", "IMAP server hostname") flag.StringVar(&user, "user", "", "IMAP username") flag.StringVar(&pw, "pw", "", "IMAP pw") flag.Parse() c, err := login(server, user, pw) if err != nil { panic(err) } _, err = imap.Wait(c.Select("INBOX", true)) if err != nil { panic(err) } ids := fetchIds(c) for _, id := range ids { fetchMessage(c, id) } /* res, err := cmd.Result(0) fmt.Println(res.String()) */ /* res, err := cmd.Result(0) fmt.Println(res.String()) for row := range(res) */ }
func (e *Email) getCountsOneLoop(minUid uint32, edw EmailDataWriter) (lastUid uint32, err error) { seqSet, err := imap.NewSeqSet(fmt.Sprintf("%d:*", minUid)) if err != nil { return 0, err } cmd, err := imap.Wait(e.conn.UIDFetch(seqSet, "ALL")) if err != nil { return 0, err } for _, resp := range cmd.Data { ed, _ := e.ParseFetchResp(resp) edw.WriteEmailData(ed) lastUid = ed.Uid } return lastUid, nil }
func fetchIds(c *imap.Client) (ids []uint32) { seq_set, err := imap.NewSeqSet("4500:*") if err != nil { panic(err) } cmd, err := imap.Wait(c.Fetch(seq_set, "FLAGS")) if err != nil { panic(err) } for _, row := range cmd.Data { v, ok := row.Fields[0].(uint32) if ok { ids = append(ids, v) } } return ids }
func ExampleClient() { // // Note: most of error handling code is omitted for brevity // var ( c *imap.Client cmd *imap.Command rsp *imap.Response ) // Connect to the server c, _ = imap.Dial("imap.example.com") // Remember to log out and close the connection when finished defer c.Logout(30 * time.Second) // Print server greeting (first response in the unilateral server data queue) fmt.Println("Server says hello:", c.Data[0].Info) c.Data = nil // Enable encryption, if supported by the server if c.Caps["STARTTLS"] { c.StartTLS(nil) } // Authenticate if c.State() == imap.Login { c.Login("*****@*****.**", "mysupersecretpassword") } // List all top-level mailboxes, wait for the command to finish cmd, _ = imap.Wait(c.List("", "%")) // Print mailbox information fmt.Println("\nTop-level mailboxes:") for _, rsp = range cmd.Data { fmt.Println("|--", rsp.MailboxInfo()) } // Check for new unilateral server data responses for _, rsp = range c.Data { fmt.Println("Server data:", rsp) } c.Data = nil // Open a mailbox (synchronous command - no need for imap.Wait) c.Select("INBOX", true) fmt.Print("\nMailbox status:\n", c.Mailbox) // Fetch the headers of the 10 most recent messages set, _ := imap.NewSeqSet("") if c.Mailbox.Messages >= 10 { set.AddRange(c.Mailbox.Messages-9, c.Mailbox.Messages) } else { set.Add("1:*") } cmd, _ = c.Fetch(set, "RFC822.HEADER") // Process responses while the command is running fmt.Println("\nMost recent messages:") for cmd.InProgress() { // Wait for the next response (no timeout) c.Recv(-1) // Process command data for _, rsp = range cmd.Data { header := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.HEADER"]) if msg, _ := mail.ReadMessage(bytes.NewReader(header)); msg != nil { fmt.Println("|--", msg.Header.Get("Subject")) } } cmd.Data = nil // Process unilateral server data for _, rsp = range c.Data { fmt.Println("Server data:", rsp) } c.Data = nil } // Check command completion status if rsp, err := cmd.Result(imap.OK); err != nil { if err == imap.ErrAborted { fmt.Println("Fetch command aborted") } else { fmt.Println("Fetch error:", rsp.Info) } } }