Refactor sendmail command to enhance recipient handling and error reporting
continuous-integration/drone/push Build is passing

This commit is contained in:
gyurix
2026-04-14 07:38:08 +02:00
parent 806a85a871
commit 8a765b2ab0
+36 -10
View File
@@ -6,21 +6,21 @@ RUN { \
echo 'package main'; \
echo ''; \
echo 'import ('; \
echo ' "flag"'; \
echo ' "fmt"'; \
echo ' "io"'; \
echo ' "net/mail"'; \
echo ' "net/smtp"'; \
echo ' "os"'; \
echo ' "strings"'; \
echo ')'; \
echo ''; \
echo 'func main() {'; \
echo ' from := flag.String("f", "minitor@localhost", "envelope sender")'; \
echo ' flag.Parse()'; \
echo ' recipients := flag.Args()'; \
echo ''; \
echo ' if len(recipients) == 0 {'; \
echo ' fmt.Fprintln(os.Stderr, "usage: sendmail [-f sender] recipient...")'; \
echo ' os.Exit(1)'; \
echo ' readHeaders := strings.Contains(strings.Join(os.Args[1:], " "), "-t")'; \
echo ' recipients := []string{}'; \
echo ' for _, arg := range os.Args[1:] {'; \
echo ' if !strings.HasPrefix(arg, "-") {'; \
echo ' recipients = append(recipients, arg)'; \
echo ' }'; \
echo ' }'; \
echo ''; \
echo ' body, err := io.ReadAll(os.Stdin)'; \
@@ -29,6 +29,31 @@ RUN { \
echo ' os.Exit(1)'; \
echo ' }'; \
echo ''; \
echo ' if readHeaders {'; \
echo ' msg, parseErr := mail.ReadMessage(strings.NewReader(string(body)))'; \
echo ' if parseErr != nil {'; \
echo ' fmt.Fprintln(os.Stderr, parseErr)'; \
echo ' os.Exit(1)'; \
echo ' }'; \
echo ' for _, hdr := range []string{"To", "Cc", "Bcc"} {'; \
echo ' if val := msg.Header.Get(hdr); val != "" {'; \
echo ' addrs, addrErr := mail.ParseAddressList(val)'; \
echo ' if addrErr != nil {'; \
echo ' fmt.Fprintln(os.Stderr, addrErr)'; \
echo ' os.Exit(1)'; \
echo ' }'; \
echo ' for _, addr := range addrs {'; \
echo ' recipients = append(recipients, addr.Address)'; \
echo ' }'; \
echo ' }'; \
echo ' }'; \
echo ' }'; \
echo ''; \
echo ' if len(recipients) == 0 {'; \
echo ' fmt.Fprintln(os.Stderr, "usage: sendmail [-t] recipient...")'; \
echo ' os.Exit(1)'; \
echo ' }'; \
echo ''; \
echo ' relay := os.Getenv("SMTP_RELAY")'; \
echo ' if relay == "" {'; \
echo ' relay = "172.17.0.2"'; \
@@ -39,9 +64,10 @@ RUN { \
echo ' port = "25"'; \
echo ' }'; \
echo ''; \
echo ' sender := os.Getenv("SMTP_SENDER")'; \
echo ' sender := os.Getenv("EMAIL_FROM")'; \
echo ' if sender == "" {'; \
echo ' sender = *from'; \
echo ' fmt.Fprintln(os.Stderr, "[sendmail] EMAIL_FROM is not set, skipping")'; \
echo ' os.Exit(0)'; \
echo ' }'; \
echo ''; \
echo ' debug := os.Getenv("DEBUG") != ""'; \