From edd7c4f4d70a4a883d03a5e53fe677c8d98e1660 Mon Sep 17 00:00:00 2001 From: "marko.tikvic" Date: Mon, 26 Nov 2018 12:53:03 +0100 Subject: [PATCH] new email interface --- email.go | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/email.go b/email.go index 052981e..3ff4d7b 100644 --- a/email.go +++ b/email.go @@ -15,17 +15,40 @@ type Email struct { From string Subject string Body string + + config *EmailConfig } -func NewEmail(to []string, from, subject, body string) *Email { +func NewEmail() *Email { return &Email{ - To: to, - From: from, - Subject: subject, - Body: body, + To: nil, + From: "", + Subject: "", + Body: "", + config: nil, } } +func (e *Email) Config(cfg *EmailConfig) { + e.config = cfg +} + +func (e *Email) SetFrom(from string) { + e.From = from +} + +func (e *Email) SetTo(to []string) { + e.To = to +} + +func (e *Email) SetSubject(sub string) { + e.Subject = sub +} + +func (e *Email) SetBody(body string) { + e.Body = body +} + func (e *Email) String() string { var str strings.Builder @@ -52,14 +75,11 @@ func (e *Email) Bytes() []byte { return []byte(e.String()) } -func SendEmail(email *Email, conf *EmailConfig) error { - if email == nil { - return errors.New("no email to send") - } - - if conf == nil { +func (email *Email) Send() error { + if email.config == nil { return errors.New("email configuration not provided") } + conf := email.config c, err := smtp.Dial(conf.ServerName) if err != nil { -- 1.8.1.2