send auth code email

vilmibm 2023-10-24 18:58:34 +00:00
parent a3b13d21b3
commit 6950ba7109
3 changed files with 28 additions and 25 deletions

View File

@ -172,13 +172,16 @@ func collectEmail(db *sql.DB, cs colorScheme, tty *tty.TTY) error {
Email: email, Email: email,
} }
err = ac.Insert(db) if err = ac.Insert(db); err != nil {
if err != nil { // TODO log
return err
}
if err = sendAuthCodeEmail(*ac); err != nil {
// TODO log // TODO log
return err return err
} }
// TODO send email
return nil return nil
} }

View File

@ -1,9 +1,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"os"
"git.tilde.town/tildetown/town/email" "git.tilde.town/tildetown/town/email"
"git.tilde.town/tildetown/town/invites" "git.tilde.town/tildetown/town/invites"
@ -28,27 +26,8 @@ If you end up stuck, e-mail root@tilde.town with any questions.
See you on the server, See you on the server,
~vilmibm` ~vilmibm`
func loadPassword() (string, error) {
f, err := os.Open("/town/docs/smtp.pw")
if err != nil {
return "", err
}
pw := make([]byte, 100)
n, err := f.Read(pw)
if err != nil {
return "", err
}
if n == 0 {
return "", errors.New("read nothing")
}
return string(pw[0:n]), nil
}
func sendInviteEmail(invite invites.Invite) error { func sendInviteEmail(invite invites.Invite) error {
pw, err := loadPassword() pw, err := email.LoadPassword()
if err != nil { if err != nil {
return fmt.Errorf("could not read password: %w", err) return fmt.Errorf("could not read password: %w", err)
} }

View File

@ -3,8 +3,10 @@ package email
import ( import (
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"errors"
"fmt" "fmt"
"net/smtp" "net/smtp"
"os"
"os/exec" "os/exec"
) )
@ -14,6 +16,25 @@ const (
SMTPPort = 465 SMTPPort = 465
) )
func LoadPassword() (string, error) {
f, err := os.Open("/town/docs/smtp.pw")
if err != nil {
return "", fmt.Errorf("could not open smtp password file: %w", err)
}
pw := make([]byte, 100)
n, err := f.Read(pw)
if err != nil {
return "", fmt.Errorf("could not read smtp password file: %w", err)
}
if n == 0 {
return "", errors.New("smtp password file was empty")
}
return string(pw[0:n]), nil
}
func SendLocalEmail(username, subject, body string) error { func SendLocalEmail(username, subject, body string) error {
cmd := exec.Command("/usr/sbin/sendmail", username) cmd := exec.Command("/usr/sbin/sendmail", username)
cmd.Stdin = bytes.NewBufferString(fmt.Sprintf("Subject: %s\n\n%s", subject, body)) cmd.Stdin = bytes.NewBufferString(fmt.Sprintf("Subject: %s\n\n%s", subject, body))