summary refs log tree commit diff stats
path: root/commands/compose/send.go
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-07-27 11:19:49 -0400
committerDrew DeVault <sir@cmpwn.com>2019-07-27 11:19:49 -0400
commit48758136c03b89757f7df1dd26331169c3d4ca02 (patch)
treef5c379b9be3ce1fa0fcc37c26d6b1a6131742121 /commands/compose/send.go
parent7a3765a36bfa1c54f8bc77c761a741d5529addcf (diff)
downloadaerc-48758136c03b89757f7df1dd26331169c3d4ca02.tar.gz
Implement sendmail support
Diffstat (limited to 'commands/compose/send.go')
-rw-r--r--commands/compose/send.go46
1 files changed, 36 insertions, 10 deletions
diff --git a/commands/compose/send.go b/commands/compose/send.go
index 26df82a..0574075 100644
--- a/commands/compose/send.go
+++ b/commands/compose/send.go
@@ -6,6 +6,7 @@ import (
 	"io"
 	"net/mail"
 	"net/url"
+	"os/exec"
 	"strings"
 	"time"
 
@@ -56,14 +57,16 @@ func (_ Send) Execute(aerc *widgets.Aerc, args []string) error {
 		scheme string
 		auth   string = "plain"
 	)
-	parts := strings.Split(uri.Scheme, "+")
-	if len(parts) == 1 {
-		scheme = parts[0]
-	} else if len(parts) == 2 {
-		scheme = parts[0]
-		auth = parts[1]
-	} else {
-		return fmt.Errorf("Unknown transfer protocol %s", uri.Scheme)
+	if uri.Scheme != "" {
+		parts := strings.Split(uri.Scheme, "+")
+		if len(parts) == 1 {
+			scheme = parts[0]
+		} else if len(parts) == 2 {
+			scheme = parts[0]
+			auth = parts[1]
+		} else {
+			return fmt.Errorf("Unknown transfer protocol %s", uri.Scheme)
+		}
 	}
 
 	header, rcpts, err := composer.PrepareHeader()
@@ -102,7 +105,7 @@ func (_ Send) Execute(aerc *widgets.Aerc, args []string) error {
 		starttls = starttls_ == "yes"
 	}
 
-	sendAsync := func() (int, error) {
+	smtpAsync := func() (int, error) {
 		switch scheme {
 		case "smtp":
 			host := uri.Host
@@ -154,7 +157,6 @@ func (_ Send) Execute(aerc *widgets.Aerc, args []string) error {
 			defer conn.Close()
 		}
 
-		// TODO: sendmail
 		if saslClient != nil {
 			if err = conn.Auth(saslClient); err != nil {
 				return 0, errors.Wrap(err, "conn.Auth")
@@ -180,6 +182,30 @@ func (_ Send) Execute(aerc *widgets.Aerc, args []string) error {
 		return int(ctr.Count()), nil
 	}
 
+	sendmailAsync := func() (int, error) {
+		cmd := exec.Command(uri.Path, rcpts...)
+		wc, err := cmd.StdinPipe()
+		if err != nil {
+			return 0, errors.Wrap(err, "cmd.StdinPipe")
+		}
+		defer wc.Close()
+		go cmd.Run()
+		ctr := datacounter.NewWriterCounter(wc)
+		composer.WriteMessage(header, ctr)
+		return int(ctr.Count()), nil
+	}
+
+	sendAsync := func() (int, error) {
+		switch scheme {
+		case "smtp":
+		case "smtps":
+			return smtpAsync()
+		case "":
+			return sendmailAsync()
+		}
+		return 0, errors.New("Unknown scheme")
+	}
+
 	go func() {
 		aerc.SetStatus("Sending...")
 		nbytes, err := sendAsync()