From 65c9f89f750976fd15bbf282839715444eef1e6e Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 16 Apr 2024 00:44:55 +0000 Subject: [PATCH] fix external mailing --- email/email.go | 66 +++++++++++++++++++++++++------------- external/cmd/help/email.go | 3 +- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/email/email.go b/email/email.go index c1cd187..933497c 100644 --- a/email/email.go +++ b/email/email.go @@ -4,12 +4,14 @@ import ( "bytes" "crypto/tls" "fmt" + + "net" + "net/mail" "net/smtp" "os/exec" ) const ( - from = "root@tilde.town" SMTPHost = "smtp.migadu.com" SMTPPort = 465 ) @@ -38,61 +40,79 @@ func NewExternalMailer(pw string) *ExternalMailer { } } -func (m *ExternalMailer) Send(address, subject, body string) error { - headers := map[string]string{ - "From": from, - "To": address, - "Subject": subject, - } +func (m *ExternalMailer) Send(address, subj, body string) error { + from := mail.Address{"Tilde Town Admins", "root@tilde.town"} + to := mail.Address{"", address} + // Setup headers + headers := make(map[string]string) + headers["From"] = from.String() + headers["To"] = to.String() + headers["Subject"] = subj + + // Setup message message := "" for k, v := range headers { message += fmt.Sprintf("%s: %s\r\n", k, v) } message += "\r\n" + body - auth := smtp.PlainAuth("", from, m.Password, SMTPHost) + // Connect to the SMTP Server + servername := fmt.Sprintf("%s:%d", SMTPHost, SMTPPort) - server := fmt.Sprintf("%s:%d", SMTPHost, SMTPPort) + host, _, _ := net.SplitHostPort(servername) - tlsconf := &tls.Config{ + auth := smtp.PlainAuth("", "root@tilde.town", m.Password, host) + + // TLS config + tlsconfig := &tls.Config{ InsecureSkipVerify: true, - ServerName: server, + ServerName: host, } - conn, err := tls.Dial("tcp", server, tlsconf) + // Here is the key, you need to call tls.Dial instead of smtp.Dial + // for smtp servers running on 465 that require an ssl connection + // from the very beginning (no starttls) + conn, err := tls.Dial("tcp", servername, tlsconfig) if err != nil { - return err + return fmt.Errorf("failed dial: %w", err) } - c, err := smtp.NewClient(conn, SMTPHost) + c, err := smtp.NewClient(conn, host) if err != nil { - return err + return fmt.Errorf("failed to make smtp client: %w", err) } + // Auth if err = c.Auth(auth); err != nil { - return fmt.Errorf("auth failed for smtp: %w", err) + return fmt.Errorf("failed to make auth: %w", err) } - if err = c.Mail(from); err != nil { - return err + // To && From + if err = c.Mail(from.Address); err != nil { + return fmt.Errorf("failed to create mail: %w", err) } - if err = c.Rcpt(address); err != nil { - return err + if err = c.Rcpt(to.Address); err != nil { + return fmt.Errorf("failed to add rcpt: %w", err) } + // Data w, err := c.Data() if err != nil { - return err + return fmt.Errorf("failed to send data: %w", err) } _, err = w.Write([]byte(message)) if err != nil { - return err + return fmt.Errorf("failed to write: %w", err) + } + + err = w.Close() + if err != nil { + return fmt.Errorf("failed to close: %w", err) } - w.Close() c.Quit() return nil diff --git a/external/cmd/help/email.go b/external/cmd/help/email.go index 7e79738..92b444e 100644 --- a/external/cmd/help/email.go +++ b/external/cmd/help/email.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "strings" "git.tilde.town/tildetown/town/email" ) @@ -41,7 +42,7 @@ func loadPassword() (string, error) { return "", errors.New("smtp password file was empty") } - return string(pw[0:n]), nil + return strings.TrimSpace(string(pw[0:n])), nil } func sendAuthCodeEmail(ac AuthCode) error {