fix external mailing

trunk
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"
"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

View File

@ -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 {