Initial commit

This commit is contained in:
Sameer Rahmani 2023-05-13 16:09:50 +01:00
parent 39396ac0bf
commit c01f050e15
Signed by: lxsameer
GPG Key ID: B0A4AF28AB9FD90B
2 changed files with 68 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module lxsameer.com/woodpecker_mailer
go 1.20

65
main.go Normal file
View File

@ -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()
}