Skip to content

Commit

Permalink
added email functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Edd King committed Mar 1, 2022
1 parent 975b903 commit e76f938
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions quickstart.go
@@ -1,10 +1,59 @@
package main

import "fmt"
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() { fmt.Println("Feed spot") })
c.AddFunc("0 3 * * * *", func() sendmail(*srv)})
c.Start()
for {
}
Expand Down

0 comments on commit e76f938

Please sign in to comment.