aboutsummaryrefslogtreecommitdiffstats
path: root/lib/send/sender.go
blob: 87ca47454fead1eb6d4a78aab7f52cb0743048b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package send

import (
	"fmt"
	"io"
	"net/url"

	"github.com/emersion/go-message/mail"

	"git.sr.ht/~rjarry/aerc/worker/types"
)

// NewSender returns an io.WriterCloser into which the caller can write
// contents of a message. The caller must invoke the Close() method on the
// sender when finished.
func NewSender(
	worker *types.Worker, uri *url.URL, domain string,
	from *mail.Address, rcpts []*mail.Address,
	copyTo string,
) (io.WriteCloser, error) {
	protocol, auth, err := parseScheme(uri)
	if err != nil {
		return nil, err
	}

	switch protocol {
	case "smtp", "smtp+insecure", "smtps":
		return newSmtpSender(protocol, auth, uri, domain, from, rcpts)
	case "jmap":
		return newJmapSender(worker, from, rcpts, copyTo)
	case "":
		return newSendmailSender(uri, rcpts)
	default:
		return nil, fmt.Errorf("unsupported protocol %s", protocol)
	}
}