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

50
main.go
View File

@@ -1,18 +1,13 @@
package main
import (
"context"
"fmt"
"log"
"net/http"
"net/smtp"
"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"
func main() {
@@ -29,7 +24,7 @@ func main() {
}
func endPointAlive() bool {
res, err := http.Get("http://"+host+":2122/heartbeat")
res, err := http.Get("http://" + host + ":2122/heartbeat")
if err != nil {
log.Println(err)
return false
@@ -44,22 +39,41 @@ func endPointAlive() bool {
}
func sendMail() {
mg := mailgun.NewMailgun(yourDomain, privateAPIKey)
// Sender data.
from := "rodbiren@gmail.com"
password := "Lovers8991"
sender := "robviren@gmail.com"
subject := "Fridge Lost Power!"
body := "Make Rob go fix it!"
recipient := "rcviren@gmail.com"
// Receiver email address.
to := []string{"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)
defer cancel()
// Message.
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 {
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
}