· 7 years ago · Aug 15, 2018, 06:10 PM
1package main
2
3import (
4 "bufio"
5 "log"
6 "os"
7 "io/ioutil"
8 "github.com/mikaa123/imapmq"
9)
10
11func main() {
12 // Create a new IMAPMQ client
13 mq, err := imapmq.New(imapmq.Config{
14 Login: "@gmail.com",
15 Passwd: "app specific pw",
16 URL: "imap.gmail.com",
17 })
18 if err != nil {
19 log.Panic(err)
20 }
21 defer mq.Close()
22
23 // Create a queue based on INBOX
24 q, err := mq.Queue("INBOX")
25 if err != nil {
26 log.Panic(err)
27 }
28
29 go func() {
30 // Subscribe to messages with the "chat" subject
31 c := q.Sub("*")
32 for msg := range c { // msg is a mail.Message instance.
33 log.Printf("%s", msg.Header.Get("Subject"))
34 body, err := ioutil.ReadAll(msg.Body)
35 if err != nil {
36 log.Fatal(err)
37 }
38 log.Printf("%s", body)
39 }
40 }()
41
42 // We scan stdin for user input
43 scanner := bufio.NewScanner(os.Stdin)
44 for scanner.Scan() {
45 // Publish a message with the "chat" subject in the INBOX queue
46 q.Pub("chat", []byte(scanner.Text()))
47 }
48}