Send emails asyncronously

This commit is contained in:
Drew DeVault 2019-05-14 14:18:01 -04:00
parent 29de3297a1
commit 928ac1bcd9

View file

@ -7,9 +7,11 @@ import (
"net/mail" "net/mail"
"net/url" "net/url"
"strings" "strings"
"time"
"github.com/emersion/go-sasl" "github.com/emersion/go-sasl"
"github.com/emersion/go-smtp" "github.com/emersion/go-smtp"
"github.com/gdamore/tcell"
"git.sr.ht/~sircmpwn/aerc2/widgets" "git.sr.ht/~sircmpwn/aerc2/widgets"
) )
@ -77,6 +79,9 @@ func SendMessage(aerc *widgets.Aerc, args []string) error {
return fmt.Errorf("Unsupported auth mechanism %s", auth) return fmt.Errorf("Unsupported auth mechanism %s", auth)
} }
aerc.SetStatus("Sending...")
sendAsync := func() {
tlsConfig := &tls.Config{ tlsConfig := &tls.Config{
// TODO: ask user first // TODO: ask user first
InsecureSkipVerify: true, InsecureSkipVerify: true,
@ -89,13 +94,17 @@ func SendMessage(aerc *widgets.Aerc, args []string) error {
} }
conn, err = smtp.Dial(host) conn, err = smtp.Dial(host)
if err != nil { if err != nil {
return err aerc.PushStatus(" "+err.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
return
} }
defer conn.Close() defer conn.Close()
if sup, _ := conn.Extension("STARTTLS"); sup { if sup, _ := conn.Extension("STARTTLS"); sup {
// TODO: let user configure tls? // TODO: let user configure tls?
if err = conn.StartTLS(tlsConfig); err != nil { if err = conn.StartTLS(tlsConfig); err != nil {
return err aerc.PushStatus(" "+err.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
return
} }
} }
case "smtps": case "smtps":
@ -105,7 +114,9 @@ func SendMessage(aerc *widgets.Aerc, args []string) error {
} }
conn, err = smtp.DialTLS(host, tlsConfig) conn, err = smtp.DialTLS(host, tlsConfig)
if err != nil { if err != nil {
return err aerc.PushStatus(" "+err.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
return
} }
defer conn.Close() defer conn.Close()
} }
@ -113,25 +124,40 @@ func SendMessage(aerc *widgets.Aerc, args []string) error {
// TODO: sendmail // TODO: sendmail
if saslClient != nil { if saslClient != nil {
if err = conn.Auth(saslClient); err != nil { if err = conn.Auth(saslClient); err != nil {
return err aerc.PushStatus(" "+err.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
return
} }
} }
// TODO: the user could conceivably want to use a different From and sender // TODO: the user could conceivably want to use a different From and sender
if err = conn.Mail(from.Address); err != nil { if err = conn.Mail(from.Address); err != nil {
return err aerc.PushStatus(" "+err.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
return
} }
for _, rcpt := range rcpts { for _, rcpt := range rcpts {
if err = conn.Rcpt(rcpt); err != nil { if err = conn.Rcpt(rcpt); err != nil {
return err aerc.PushStatus(" "+err.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
return
} }
} }
wc, err := conn.Data() wc, err := conn.Data()
if err != nil { if err != nil {
return err aerc.PushStatus(" "+err.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
return
} }
defer wc.Close() defer wc.Close()
composer.WriteMessage(header, wc) composer.WriteMessage(header, wc)
composer.Close() composer.Close()
aerc.RemoveTab(composer) aerc.RemoveTab(composer)
}
go func() {
sendAsync()
// TODO: Use a stack
aerc.SetStatus("Sent.")
}()
return nil return nil
} }