Skip to content

Instantly share code, notes, and snippets.

@anta40
Created September 6, 2023 07:05
Show Gist options
  • Save anta40/6c04227c7a28b781e6210da3f2757d57 to your computer and use it in GitHub Desktop.
Save anta40/6c04227c7a28b781e6210da3f2757d57 to your computer and use it in GitHub Desktop.
Try to fetch attachment...
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"github.com/emersion/go-imap/"
"github.com/emersion/go-imap/v2/imapclient"
)
func generateEmailRange(startIdx, endIdx uint32) []uint32 {
var x uint32
var theSize = endIdx - startIdx
var theSlice = make([]uint32, theSize)
for x = 0; x < theSize; x++ {
theSlice[x] = startIdx + x
}
return theSlice
}
func main() {
client, client_err := imapclient.DialTLS("xxx.xxx.xxx:993", nil)
if client_err != nil {
fmt.Printf("Failed to dial IMAP server: %v", client_err)
} else {
fmt.Printf("Succesfully connected to IMAP server...\n")
}
defer client.Close()
if client_err := client.Login("xxxxx", "xxxxx").Wait(); client_err != nil {
fmt.Printf("Failed to login: %v", client_err)
} else {
fmt.Printf("Succesfully logged in...\n")
}
mailboxes, client_err := client.List("", "%", nil).Collect()
if client_err != nil {
fmt.Printf("Failed to list mailboxes: %v\n", client_err)
} else {
fmt.Printf("Found %v mailboxes\n", len(mailboxes))
for _, mbox := range mailboxes {
fmt.Printf(" - %v\n", mbox.Mailbox)
}
}
selectedMbox, client_err := client.Select("INBOX", nil).Wait()
if client_err != nil {
fmt.Printf("Failed to select INBOX: %v\n", client_err)
} else {
fmt.Printf("INBOX contains %v messages\n", selectedMbox.NumMessages)
}
if selectedMbox.NumMessages > 0 {
seqSet := new(imap.SeqSet)
seqSet.AddNum(selectedMbox.NumMessages)
// Get the whole message body
var section imap.BodySectionName
items := []imap.FetchItem{section.FetchItem()}
messages := make(chan *imap.Message, 1)
go func() {
if err := c.Fetch(seqSet, items, messages); err != nil {
log.Fatal(err)
}
}()
msg := <-messages
if msg == nil {
log.Fatal("Server didn't returned message")
}
r := msg.GetBody(&section)
if r == nil {
log.Fatal("Server didn't returned message body")
}
// Create a new mail reader
mr, err := mail.CreateReader(r)
if err != nil {
log.Fatal(err)
}
// Print some info about the message
header := mr.Header
if date, err := header.Date(); err == nil {
log.Println("Date:", date)
}
if from, err := header.AddressList("From"); err == nil {
log.Println("From:", from)
}
if to, err := header.AddressList("To"); err == nil {
log.Println("To:", to)
}
if subject, err := header.Subject(); err == nil {
log.Println("Subject:", subject)
}
// Process each message's part
for {
p, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
switch h := p.Header.(type) {
case *mail.InlineHeader:
// This is the message's text (can be plain-text or HTML)
b, _ := ioutil.ReadAll(p.Body)
log.Println("Got text: %v", string(b))
case *mail.AttachmentHeader:
// This is an attachment
filename, _ := h.Filename()
log.Println("Got attachment: %v", filename)
}
}
}
if err := client.Logout().Wait(); err != nil {
fmt.Printf("Failed to logout: %v\n", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment