WIP on sending external email

trunk
vilmibm 2023-03-07 01:06:46 +00:00
parent 09269126d8
commit cee8b75bad
4 changed files with 87 additions and 3 deletions

View File

@ -0,0 +1,8 @@
package main
// TODO function for reading smtp.pw from /town/docs
func sendInviteEmail() error {
// TODO
return nil
}

View File

@ -263,13 +263,18 @@ func _main() error {
updateCount()
render()
if decision == models.SignupAccepted {
err = invites.InsertInvite(inviteDB, currSignup.CleanEmail)
if err != nil {
if err = invites.InsertInvite(inviteDB, currSignup.CleanEmail); err != nil {
errorModal.SetText(fmt.Sprintf("error! failed to create invite: %s", err.Error()))
pages.SwitchToPage("error")
}
// TODO send invite email
// TODO need to get an invite back from InsertInvite so we can send it to
// the clean email using sendInviteEmail
if err = sendInviteEmail(); err != nil {
errorModal.SetText(fmt.Sprintf("error! failed to send welcome email: %s", err.Error()))
pages.SwitchToPage("error")
}
}
pages.SwitchToPage("main")
})

View File

@ -16,6 +16,7 @@ import (
_ "embed"
)
// TODO mark on user table what signup id led to the account for forensics
// TODO add logging like the signup tool has
// TODO consider merging adduser, usermod, and createkeyfile into single createuser helper to limit sudoers list
// TODO move magic key machine to static page

View File

@ -2,10 +2,18 @@ package email
import (
"bytes"
"crypto/tls"
"fmt"
"net/smtp"
"os/exec"
)
const (
SMTPlogin = "root@tilde.town"
SMTPHost = "smtp.zoho.com"
SMTPPort = 465
)
func SendLocalEmail(username, subject, body string) error {
cmd := exec.Command("/usr/sbin/sendmail", username)
cmd.Stdin = bytes.NewBufferString(fmt.Sprintf("Subject: %s\n\n%s", subject, body))
@ -16,3 +24,65 @@ func SendLocalEmail(username, subject, body string) error {
return nil
}
type ExternalMailer struct {
Password string
}
func NewExternalMailer(pw string) *ExternalMailer {
if pw == "" {
panic("why?")
}
return &ExternalMailer{
Password: pw,
}
}
func (m *ExternalMailer) Send(address, subject, body string) error {
// TODO need to add headers to prepare message
auth := smtp.PlainAuth("", SMTPlogin, m.Password, SMTPHost)
server := fmt.Sprintf("%s:%d", SMTPHost, SMTPPort)
tlsconf := &tls.Config{
InsecureSkipVerify: true,
ServerName: server,
}
conn, err := tls.Dial("tcp", server, tlsconf)
if err != nil {
return err
}
c, err := smtp.NewClient(conn, SMTPHost)
if err != nil {
return err
}
if err = c.Auth(auth); err != nil {
return fmt.Errorf("auth failed for smtp: %w", err)
}
if err = c.Mail("root@tilde.town"); err != nil {
return err
}
if err = c.Rcpt(address); err != nil {
return err
}
w, err := c.Data()
if err != nil {
return err
}
_, err = w.Write([]byte(body))
if err != nil {
return err
}
w.Close()
c.Quit()
return nil
}