diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..171f0dc --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module lxsameer.com/woodpecker_mailer + +go 1.20 diff --git a/main.go b/main.go new file mode 100644 index 0000000..f52118a --- /dev/null +++ b/main.go @@ -0,0 +1,65 @@ +package main + +import ( + "crypto/tls" + "fmt" + "html/template" + "net/mail" + "net/smtp" + "os" +) + +// "Send As" information + +func main() { + var ( + username = os.Getenv("PLUGIN_USER") + password = os.Getenv("PLUGIN_PASSWORD") + to = os.Getenv("PLUGIN_TO") + subject = os.Getenv("PLUGIN_SUBJECT") + body = os.Getenv("PLUGIN_TEXT") + from_addr = os.Getenv("PLUGIN_FROM") + from_name = os.Getenv("PLUGIN_FROM_NAME") + ) + + const gmail = "smtp.gmail.com:587" + + sanitizedBody := template.HTMLEscapeString(body) + + fmt.Println("Authenticateing...") + auth := smtp.PlainAuth("", username, password, "smtp.gmail.com") + smtpConnection, err := smtp.Dial(gmail) + if err != nil { + panic(err) + } + if err := smtpConnection.StartTLS(&tls.Config{ServerName: "smtp.gmail.com"}); err != nil { + + panic(err) + } + if err := smtpConnection.Auth(auth); err != nil { + panic(err) + } + + from := mail.Address{Name: from_name, Address: from_addr} + rcpt := mail.Address{Name: "", Address: to} + + header := make(map[string]string) + header["From"] = from.String() + header["To"] = rcpt.String() + header["Subject"] = subject + + // Convert the header map to a string and attach the body to it + var message string + for key, value := range header { + message += fmt.Sprintf("%s: %s\r\n", key, value) + } + message += "\r\n" + sanitizedBody + "\r\n" + + fmt.Println("Senting an email to user") + err = smtp.SendMail(gmail, auth, from_addr, []string{rcpt.Address}, []byte(message)) + if err != nil { + panic(err) + } + + smtpConnection.Quit() +}