smtp is not necessary
This commit is contained in:
parent
a0228b4692
commit
08a149acec
|
@ -4,21 +4,18 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"git.sr.ht/~ewintr/gte/pkg/mstore"
|
||||
)
|
||||
|
||||
func main() {
|
||||
iPort, _ := strconv.Atoi(os.Getenv("IMAP_PORT"))
|
||||
//sPort, _ := strconv.Atoi(os.Getenv("SMTP_PORT"))
|
||||
config := &mstore.EMailStoreConfiguration{
|
||||
IMAPURL: os.Getenv("IMAP_URL"),
|
||||
IMAPPort: iPort,
|
||||
IMAPUsername: os.Getenv("MAIL_USER"),
|
||||
IMAPPassword: os.Getenv("MAIL_PASSWORD"),
|
||||
IMAPUsername: os.Getenv("IMAP_USERNAME"),
|
||||
IMAPPassword: os.Getenv("IMAP_PASSWORD"),
|
||||
}
|
||||
if !config.Valid() {
|
||||
fmt.Printf("conf: %v\n", config)
|
||||
log.Fatal("please set MAIL_USER, MAIL_PASSWORD, etc environment variables")
|
||||
}
|
||||
//fmt.Printf("conf: %+v\n", config)
|
||||
|
@ -36,56 +33,13 @@ func main() {
|
|||
for _, f := range folders {
|
||||
fmt.Println(f.Name)
|
||||
}
|
||||
/*
|
||||
|
||||
// List mailboxes
|
||||
mailboxes := make(chan *imap.MailboxInfo, 10)
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- c.List("", "*", mailboxes)
|
||||
}()
|
||||
|
||||
log.Println("Mailboxes:")
|
||||
for m := range mailboxes {
|
||||
log.Println("* " + m.Name)
|
||||
}
|
||||
|
||||
if err := <-done; err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Select INBOX
|
||||
mbox, err := c.Select("INBOX", false)
|
||||
messages, err := mailStore.Inbox()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Flags for INBOX:", mbox.Flags)
|
||||
|
||||
// Get the last 4 messages
|
||||
from := uint32(1)
|
||||
to := mbox.Messages
|
||||
if mbox.Messages > 3 {
|
||||
// We're using unsigned integers here, only subtract if the result is > 0
|
||||
from = mbox.Messages - 3
|
||||
}
|
||||
seqset := new(imap.SeqSet)
|
||||
seqset.AddRange(from, to)
|
||||
|
||||
messages := make(chan *imap.Message, 10)
|
||||
done = make(chan error, 1)
|
||||
go func() {
|
||||
done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope}, messages)
|
||||
}()
|
||||
|
||||
log.Println("Last 4 messages:")
|
||||
for msg := range messages {
|
||||
log.Println("* " + msg.Envelope.Subject)
|
||||
for _, m := range messages {
|
||||
fmt.Println(m.Subject)
|
||||
}
|
||||
|
||||
if err := <-done; err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("Done!")
|
||||
*/
|
||||
}
|
||||
|
|
6
go.mod
6
go.mod
|
@ -2,4 +2,8 @@ module git.sr.ht/~ewintr/gte
|
|||
|
||||
go 1.14
|
||||
|
||||
require github.com/emersion/go-imap v1.0.6
|
||||
require (
|
||||
github.com/emersion/go-imap v1.0.6
|
||||
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21
|
||||
github.com/emersion/go-smtp v0.14.0
|
||||
)
|
||||
|
|
4
go.sum
4
go.sum
|
@ -5,6 +5,10 @@ github.com/emersion/go-imap v1.0.6/go.mod h1:yKASt+C3ZiDAiCSssxg9caIckWF/JG7ZQTO
|
|||
github.com/emersion/go-message v0.11.1/go.mod h1:C4jnca5HOTo4bGN9YdqNQM9sITuT3Y0K6bSUw9RklvY=
|
||||
github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b h1:uhWtEWBHgop1rqEk2klKaxPAkVDCXexai6hSuRQ7Nvs=
|
||||
github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k=
|
||||
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=
|
||||
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
|
||||
github.com/emersion/go-smtp v0.14.0 h1:RYW203p+EcPjL8Z/ZpT9lZ6iOc8MG1MQzEx1UKEkXlA=
|
||||
github.com/emersion/go-smtp v0.14.0/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
|
||||
github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
|
||||
github.com/martinlindhe/base36 v1.0.0/go.mod h1:+AtEs8xrBpCeYgSLoY/aJ6Wf37jtBuR0s35750M27+8=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
|
|
|
@ -9,17 +9,12 @@ import (
|
|||
|
||||
type EMailStoreConfiguration struct {
|
||||
IMAPURL string
|
||||
IMAPPort int
|
||||
IMAPUsername string
|
||||
IMAPPassword string
|
||||
SMTPURL string
|
||||
SMTPPort int
|
||||
SMTPUsername string
|
||||
SMTPPassword string
|
||||
}
|
||||
|
||||
func (esc *EMailStoreConfiguration) Valid() bool {
|
||||
if esc.IMAPURL == "" || esc.IMAPPort == 0 {
|
||||
if esc.IMAPURL == "" {
|
||||
return false
|
||||
}
|
||||
if esc.IMAPUsername == "" || esc.IMAPPassword == "" {
|
||||
|
@ -29,32 +24,37 @@ func (esc *EMailStoreConfiguration) Valid() bool {
|
|||
return true
|
||||
}
|
||||
|
||||
type smtpConf struct {
|
||||
url string
|
||||
to string
|
||||
}
|
||||
|
||||
type EMailStore struct {
|
||||
imapClient *client.Client
|
||||
imap *client.Client
|
||||
}
|
||||
|
||||
func EMailConnect(conf *EMailStoreConfiguration) (*EMailStore, error) {
|
||||
imapClient, err := client.DialTLS(fmt.Sprintf("%s:%d", conf.IMAPURL, conf.IMAPPort), nil)
|
||||
imap, err := client.DialTLS(conf.IMAPURL, nil)
|
||||
if err != nil {
|
||||
return &EMailStore{}, err
|
||||
}
|
||||
if err := imapClient.Login(conf.IMAPUsername, conf.IMAPPassword); err != nil {
|
||||
if err := imap.Login(conf.IMAPUsername, conf.IMAPPassword); err != nil {
|
||||
return &EMailStore{}, err
|
||||
}
|
||||
|
||||
return &EMailStore{
|
||||
imapClient: imapClient,
|
||||
imap: imap,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (es EMailStore) Disconnect() {
|
||||
es.imapClient.Logout()
|
||||
func (es *EMailStore) Disconnect() {
|
||||
es.imap.Logout()
|
||||
}
|
||||
|
||||
func (es EMailStore) Folders() ([]*Folder, error) {
|
||||
func (es *EMailStore) Folders() ([]*Folder, error) {
|
||||
boxes, done := make(chan *imap.MailboxInfo), make(chan error)
|
||||
go func() {
|
||||
done <- es.imapClient.List("", "*", boxes)
|
||||
done <- es.imap.List("", "*", boxes)
|
||||
}()
|
||||
|
||||
folders := []*Folder{}
|
||||
|
@ -70,3 +70,34 @@ func (es EMailStore) Folders() ([]*Folder, error) {
|
|||
|
||||
return folders, nil
|
||||
}
|
||||
|
||||
func (es *EMailStore) Inbox() ([]*Message, error) {
|
||||
mbox, err := es.imap.Select("INBOX", false)
|
||||
if err != nil {
|
||||
return []*Message{}, err
|
||||
}
|
||||
fmt.Println("Flags for INBOX:", mbox.Flags)
|
||||
|
||||
fmt.Println("Messages: ", mbox.Messages)
|
||||
|
||||
seqset := new(imap.SeqSet)
|
||||
seqset.AddRange(uint32(1), mbox.Messages)
|
||||
|
||||
imsg, done := make(chan *imap.Message), make(chan error)
|
||||
go func() {
|
||||
done <- es.imap.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope}, imsg)
|
||||
}()
|
||||
|
||||
messages := []*Message{}
|
||||
for m := range imsg {
|
||||
messages = append(messages, &Message{
|
||||
Subject: m.Envelope.Subject,
|
||||
})
|
||||
}
|
||||
|
||||
if err := <-done; err != nil {
|
||||
return []*Message{}, err
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue