Added adminmailer program

This commit is contained in:
Asaf Gartner 2021-09-01 23:41:45 +03:00
parent d78a2e8e82
commit 77debffc1f
3 changed files with 94 additions and 0 deletions

2
.gitignore vendored
View File

@ -9,3 +9,5 @@ cinera/cinera.conf
annotations/
/hmn
deploy.conf
adminmailer/config.go
adminmailer/adminmailer

View File

@ -0,0 +1,9 @@
package main
const RecvAddress = "admin@example.com"
const RecvName = "Admin"
const FromName = "From Name"
const FromAddress = "from@address.com"
const FromAddressPassword = "password"
const ServerAddress = "server.address"
const ServerPort = 587

83
adminmailer/main.go Normal file
View File

@ -0,0 +1,83 @@
package main
import (
"fmt"
"mime"
"mime/quotedprintable"
"net/smtp"
"os"
"strings"
"time"
)
func main() {
if len(os.Args) == 1 {
fmt.Printf(`The source code for this program can be found at git.handmade.network/hmn/hmn/adminmailer
The config data is compiled into the program. If you need to change the config,
find the adminmailer package in the hmn repo, modify config.go, and recompile.`)
fmt.Printf("\n\nUsage:\n")
fmt.Printf("%s [subject] [message]\n\n", os.Args[0])
os.Exit(1)
}
if len(os.Args) < 3 {
fmt.Printf("You must provide a subject and message to send\n\n")
fmt.Printf("%s [subject] [message]\n\n", os.Args[0])
os.Exit(1)
}
subject := os.Args[1]
message := strings.Join(os.Args[2:], " ")
err := sendMail(RecvAddress, RecvName, subject, message)
if err != nil {
fmt.Printf("Failed to send email:\n%v\n\n", err)
os.Exit(1)
}
}
func sendMail(toAddress, toName, subject, contentHtml string) error {
contents := prepMailContents(
makeHeaderAddress(toAddress, toName),
makeHeaderAddress(FromAddress, FromName),
subject,
contentHtml,
)
return smtp.SendMail(
fmt.Sprintf("%s:%d", ServerAddress, ServerPort),
smtp.PlainAuth("", FromAddress, FromAddressPassword, ServerAddress),
FromAddress,
[]string{toAddress},
contents,
)
}
func makeHeaderAddress(email, fullname string) string {
if fullname != "" {
encoded := mime.BEncoding.Encode("utf-8", fullname)
if encoded == fullname {
encoded = strings.ReplaceAll(encoded, `"`, `\"`)
encoded = fmt.Sprintf("\"%s\"", encoded)
}
return fmt.Sprintf("%s <%s>", encoded, email)
} else {
return email
}
}
func prepMailContents(toLine string, fromLine string, subject string, contentHtml string) []byte {
var builder strings.Builder
builder.WriteString(fmt.Sprintf("To: %s\r\n", toLine))
builder.WriteString(fmt.Sprintf("From: %s\r\n", fromLine))
builder.WriteString(fmt.Sprintf("Date: %s\r\n", time.Now().UTC().Format(time.RFC1123Z)))
builder.WriteString(fmt.Sprintf("Subject: %s\r\n", mime.QEncoding.Encode("utf-8", subject)))
builder.WriteString("Content-Type: text/html; charset=UTF-8\r\n")
builder.WriteString("Content-Transfer-Encoding: quoted-printable\r\n")
builder.WriteString("\r\n")
writer := quotedprintable.NewWriter(&builder)
writer.Write([]byte(contentHtml))
writer.Close()
builder.WriteString("\r\n")
return []byte(builder.String())
}