Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactorings #3

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open

Refactorings #3

wants to merge 15 commits into from

Conversation

dmke
Copy link
Contributor

@dmke dmke commented Aug 2, 2018

(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 added 15 commits April 6, 2018 15:10
It turns out, a global variable is not a great idea, so we move it
into the Mail object itself.

This is also introduces a new "chaninable" API: `m.SetSendmail()`
returns `m`, which allows chaining of further options (see following
commits).
Don't depend on environment variable for debug output, instead let
the consumer decide where to print to.
AppendTo might be a bit misleading, as it doesn't append the mail
to something, but appends the mail's To field. It is literally
a `To = append(To, ...)` construct.
Accepts chainable Options as arguments.
Makes testing unneccessarily hard.
Place multipart mails (HTML+Text) on the roadmap.
WriteTo now returns (int64, error); however the returned number of bytes
written is always 0, since we don't get the actual number easily.
@dmke dmke mentioned this pull request Aug 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant