-
Notifications
You must be signed in to change notification settings - Fork 1
/
quickstart.go
61 lines (50 loc) · 1.43 KB
/
quickstart.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"context"
"encoding/base64"
"io/ioutil"
"log"
"strings"
"golang.org/x/oauth2/google"
"google.golang.org/api/gmail/v1"
"google.golang.org/api/option"
)
func sendmail(srv gmail.Service, frommail string) {
temp := []byte("From: 'me'\r\n" +
"reply-to: blobcorpciso@gmail.com\r\n" +
"To: blobcorpciso@gmail.com\r\n" +
"Subject: Feed Spot \r\n" +
"remember to feed spot")
var message gmail.Message
message.Raw = base64.StdEncoding.EncodeToString(temp)
message.Raw = strings.Replace(message.Raw, "/", "_", -1)
message.Raw = strings.Replace(message.Raw, "+", "-", -1)
message.Raw = strings.Replace(message.Raw, "=", "", -1)
_, err := srv.Users.Messages.Send("me", &message).Do()
if err != nil {
log.Fatalf("Unable to send. %v", err)
}
}
func main() {
ctx := context.Background()
b, err := ioutil.ReadFile("credentials.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
// If modifying these scopes, delete your previously saved token.json.
config, err := google.ConfigFromJSON(b, gmail.MailGoogleComScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getClient(config)
srv, err := gmail.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to retrieve Gmail client: %v", err)
}
user := "me"
c := cron.New()
c.AddFunc("0 3 * * * *", func() sendmail(*srv)})
c.Start()
for {
}
}