sendmail icon indicating copy to clipboard operation
sendmail copied to clipboard

Refactorings

Open dmke opened this issue 7 years ago • 0 comments

(This is a continuation of #2.)

Hey,

whilst using this more, I found I needed a more conveniant interface.

The first part in this commit series will introduce a sendmail.Option type and a sendmail.New() constructor function, which takes said Options as arguments. Together, they allow code to look like this:

// taken from `sendmail_test.go`, `TestTextMail`
var buf bytes.Buffer
sm := New(
	Subject("Cześć"),
	From("Michał", "[email protected]"),
	To("Ktoś", "[email protected]"),
	To("Ktoś2", "[email protected]"),
	DebugOutput(&buf),
)

or like this:

// taken from `options_test.go`, `TestChainingOptions`
m.SetSubject("Test subject").
	SetFrom("Dominik", "[email protected]").
	AppendTo("Dominik2", "[email protected]").
	SetDebugOutput(&buf).
	SetSendmail("/bin/true")

Another subset of the commits moves the recently introduced global Binary variable, as well as the debug variable into the Mail type. As it turns out, global variables are a source of frustration in concurrent applications :-) You can see an example in the snippets above (SetOutput/SetSendmail).

I also started with HTML mails, but mixed-content (multipart, Text+HTML) isn't there yet. I'd like to harvest some code from mailyak, but the current license prevents me from that. Having a solid MIME multipart implementation would also allow attachments to the mail.

dmke avatar Aug 02 '18 13:08 dmke