Send emails asyncronously

This commit is contained in:
Drew DeVault 2019-05-14 14:18:01 -04:00
parent 29de3297a1
commit 928ac1bcd9
1 changed files with 73 additions and 47 deletions

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,61 +79,85 @@ func SendMessage(aerc *widgets.Aerc, args []string) error {
return fmt.Errorf("Unsupported auth mechanism %s", auth) return fmt.Errorf("Unsupported auth mechanism %s", auth)
} }
tlsConfig := &tls.Config{ aerc.SetStatus("Sending...")
// TODO: ask user first
InsecureSkipVerify: true, sendAsync := func() {
} tlsConfig := &tls.Config{
switch scheme { // TODO: ask user first
case "smtp": InsecureSkipVerify: true,
host := uri.Host
if !strings.ContainsRune(host, ':') {
host = host + ":587" // Default to submission port
} }
conn, err = smtp.Dial(host) switch scheme {
if err != nil { case "smtp":
return err host := uri.Host
if !strings.ContainsRune(host, ':') {
host = host + ":587" // Default to submission port
}
conn, err = smtp.Dial(host)
if err != nil {
aerc.PushStatus(" "+err.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
return
}
defer conn.Close()
if sup, _ := conn.Extension("STARTTLS"); sup {
// TODO: let user configure tls?
if err = conn.StartTLS(tlsConfig); err != nil {
aerc.PushStatus(" "+err.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
return
}
}
case "smtps":
host := uri.Host
if !strings.ContainsRune(host, ':') {
host = host + ":465" // Default to smtps port
}
conn, err = smtp.DialTLS(host, tlsConfig)
if err != nil {
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 { // TODO: sendmail
// TODO: let user configure tls? if saslClient != nil {
if err = conn.StartTLS(tlsConfig); err != nil { if err = conn.Auth(saslClient); err != nil {
return err aerc.PushStatus(" "+err.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
return
} }
} }
case "smtps": // TODO: the user could conceivably want to use a different From and sender
host := uri.Host if err = conn.Mail(from.Address); err != nil {
if !strings.ContainsRune(host, ':') { aerc.PushStatus(" "+err.Error(), 10*time.Second).
host = host + ":465" // Default to smtps port Color(tcell.ColorDefault, tcell.ColorRed)
return
} }
conn, err = smtp.DialTLS(host, tlsConfig) for _, rcpt := range rcpts {
if err = conn.Rcpt(rcpt); err != nil {
aerc.PushStatus(" "+err.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
return
}
}
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 conn.Close() defer wc.Close()
composer.WriteMessage(header, wc)
composer.Close()
aerc.RemoveTab(composer)
} }
// TODO: sendmail go func() {
if saslClient != nil { sendAsync()
if err = conn.Auth(saslClient); err != nil { // TODO: Use a stack
return err aerc.SetStatus("Sent.")
} }()
}
// TODO: the user could conceivably want to use a different From and sender
if err = conn.Mail(from.Address); err != nil {
return err
}
for _, rcpt := range rcpts {
if err = conn.Rcpt(rcpt); err != nil {
return err
}
}
wc, err := conn.Data()
if err != nil {
return err
}
defer wc.Close()
composer.WriteMessage(header, wc)
composer.Close()
aerc.RemoveTab(composer)
return nil return nil
} }