Fixed email

This commit is contained in:
2021-11-08 09:55:18 -06:00
parent e8d5b00e73
commit e931148720
3 changed files with 32 additions and 48 deletions

9
go.mod
View File

@@ -1,12 +1,3 @@
module rodbiren.ddns.net/git/robviren/milkwatch-server module rodbiren.ddns.net/git/robviren/milkwatch-server
go 1.17 go 1.17
require (
github.com/gorilla/mux v1.8.0 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/mailgun/mailgun-go/v4 v4.5.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/pkg/errors v0.8.1 // indirect
)

21
go.sum
View File

@@ -1,21 +0,0 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64=
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg=
github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/mailgun/mailgun-go/v4 v4.5.3 h1:Cc4IRTYZVSdDRD7H/wBJRYAwM9DBuFDsbBtsSwqTjCM=
github.com/mailgun/mailgun-go/v4 v4.5.3/go.mod h1:FJlF9rI5cQT+mrwujtJjPMbIVy3Ebor9bKTVsJ0QU40=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

48
main.go
View File

@@ -1,18 +1,13 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"net/smtp"
"time" "time"
"github.com/mailgun/mailgun-go/v4"
) )
const yourDomain string = "sandbox403bedf0d09d4e539131fabf0ab7c11f.mailgun.org" // e.g. mg.yourcompany.com
const privateAPIKey string = "key-2b58bc1193c549df83d8d5ed54e1c982"
const host = "rodberrypi" const host = "rodberrypi"
func main() { func main() {
@@ -44,22 +39,41 @@ func endPointAlive() bool {
} }
func sendMail() { func sendMail() {
mg := mailgun.NewMailgun(yourDomain, privateAPIKey) // Sender data.
from := "rodbiren@gmail.com"
password := "Lovers8991"
sender := "robviren@gmail.com" // Receiver email address.
subject := "Fridge Lost Power!" to := []string{"rcviren@gmail.com"}
body := "Make Rob go fix it!"
recipient := "rcviren@gmail.com"
message := mg.NewMessage(sender, subject, body, recipient) // smtp server configuration.
smtpServer := smtpServer{host: "smtp.gmail.com", port: "587"}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) // Message.
defer cancel() mime := "MIME-version: 1.0;\nContent-Type: text/plain; charset=\"UTF-8\";\n\n"
subject := "Subject: " + "Garage Power is Out" + "!\n"
message := []byte(subject + mime + "\n" + "Make Rob Fix It!")
resp, id, err := mg.Send(ctx, message) // Authentication.
auth := smtp.PlainAuth("", from, password, smtpServer.host)
// Sending email.
err := smtp.SendMail(smtpServer.Address(), auth, from, to, message)
if err != nil { if err != nil {
log.Fatal(err) fmt.Println(err)
return
} }
fmt.Printf("ID: %s Resp: %s\n", id, resp)
fmt.Println("Email Sent!")
}
// smtpServer data to smtp server.
type smtpServer struct {
host string
port string
}
// Address URI to smtp server.
func (s *smtpServer) Address() string {
return s.host + ":" + s.port
} }