fix external mailing

vilmibm 2024-04-16 00:44:55 +00:00
parent 051326330a
commit 65c9f89f75
2 changed files with 45 additions and 24 deletions

View File

@ -4,12 +4,14 @@ import (
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"net"
"net/mail"
"net/smtp" "net/smtp"
"os/exec" "os/exec"
) )
const ( const (
from = "root@tilde.town"
SMTPHost = "smtp.migadu.com" SMTPHost = "smtp.migadu.com"
SMTPPort = 465 SMTPPort = 465
) )
@ -38,61 +40,79 @@ func NewExternalMailer(pw string) *ExternalMailer {
} }
} }
func (m *ExternalMailer) Send(address, subject, body string) error { func (m *ExternalMailer) Send(address, subj, body string) error {
headers := map[string]string{ from := mail.Address{"Tilde Town Admins", "root@tilde.town"}
"From": from, to := mail.Address{"", address}
"To": address,
"Subject": subject,
}
// Setup headers
headers := make(map[string]string)
headers["From"] = from.String()
headers["To"] = to.String()
headers["Subject"] = subj
// Setup message
message := "" message := ""
for k, v := range headers { for k, v := range headers {
message += fmt.Sprintf("%s: %s\r\n", k, v) message += fmt.Sprintf("%s: %s\r\n", k, v)
} }
message += "\r\n" + body 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, 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 { 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 { if err != nil {
return err return fmt.Errorf("failed to make smtp client: %w", err)
} }
// Auth
if err = c.Auth(auth); err != nil { 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 { // To && From
return err if err = c.Mail(from.Address); err != nil {
return fmt.Errorf("failed to create mail: %w", err)
} }
if err = c.Rcpt(address); err != nil { if err = c.Rcpt(to.Address); err != nil {
return err return fmt.Errorf("failed to add rcpt: %w", err)
} }
// Data
w, err := c.Data() w, err := c.Data()
if err != nil { if err != nil {
return err return fmt.Errorf("failed to send data: %w", err)
} }
_, err = w.Write([]byte(message)) _, err = w.Write([]byte(message))
if err != nil { 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() c.Quit()
return nil return nil

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"strings"
"git.tilde.town/tildetown/town/email" "git.tilde.town/tildetown/town/email"
) )
@ -41,7 +42,7 @@ func loadPassword() (string, error) {
return "", errors.New("smtp password file was empty") 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 { func sendAuthCodeEmail(ac AuthCode) error {