Answer the question
In order to leave comments, you need to log in
How to download email and attachment using golang imap?
Good afternoon, can anyone tell me, maybe there is a one that does what you need.
You need to get the last n letters from the mailbox - from each letter, pick up the header, recipient, sender, time and date and all attachments (save attachments to the server).
Or who made the save investment? There is no problem with reading letters and data from them, but for the time being it is not possible to save the attachment.
Answer the question
In order to leave comments, you need to log in
"github.com/emersion/go-imap"
switch h := p.Header.(type) {
//case mail.TextHeader:
//// 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)
for _, checkFileName := range checkFileNames {
//if the email attachment name matches, then download it to the price
list if strings.Contains(filename, checkFileName) == true {
log.Println("Downloading file: ", filename)
//create file
output, err := os.Create(filename)
if err != nil {
fmt.Println("Error while creating", "-", err)
}
defer output.Close()
//-------- ------
//write bytes to file
_, err = io.Copy(output, p.Body)
if err != nil {
fmt.Println("Error while downloading", "-", err)
}
/ /--------------
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question