Skip to content

Instantly share code, notes, and snippets.

@shemul
Created June 13, 2020 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shemul/537cbcdef78019273089267d2bb10630 to your computer and use it in GitHub Desktop.
Save shemul/537cbcdef78019273089267d2bb10630 to your computer and use it in GitHub Desktop.
go-machinery-task.go
package tasks
import (
"encoding/base64"
"encoding/json"
"log"
"net/smtp"
)
type Payload struct {
Email string `json:"email"`
Subject string `json:"subject"`
Body string `json:"body"`
}
func DecodeToTask(msg string, task interface{}) (err error) {
decodedstg, err := base64.StdEncoding.DecodeString(msg)
if err != nil {
return
}
msgBytes := []byte(decodedstg)
err = json.Unmarshal(msgBytes, task)
if err != nil {
return
}
return
}
func SendMail(b64payload string) (bool, error) {
payload := Payload{}
DecodeToTask(b64payload, &payload)
from := "<your-google-email>@gmail.com"
pass := "<your-google-email-app-password>"
to := payload.Email
msg := "From: " + from + "\n" +
"To: " + to + "\n" +
"Subject: "+ payload.Subject +"\n\n" +
payload.Body
err := smtp.SendMail("smtp.gmail.com:587",
smtp.PlainAuth("", from, pass, "smtp.gmail.com"),
from, []string{to}, []byte(msg))
if err != nil {
log.Printf("smtp error: %s", err)
return false, nil
}
return true, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment