Commit edd7c4f4d70a4a883d03a5e53fe677c8d98e1660
1 parent
c99e4944e5
Exists in
master
new email interface
Showing
1 changed file
with
31 additions
and
11 deletions
Show diff stats
email.go
... | ... | @@ -15,17 +15,40 @@ type Email struct { |
15 | 15 | From string |
16 | 16 | Subject string |
17 | 17 | Body string |
18 | + | |
19 | + config *EmailConfig | |
18 | 20 | } |
19 | 21 | |
20 | -func NewEmail(to []string, from, subject, body string) *Email { | |
22 | +func NewEmail() *Email { | |
21 | 23 | return &Email{ |
22 | - To: to, | |
23 | - From: from, | |
24 | - Subject: subject, | |
25 | - Body: body, | |
24 | + To: nil, | |
25 | + From: "", | |
26 | + Subject: "", | |
27 | + Body: "", | |
28 | + config: nil, | |
26 | 29 | } |
27 | 30 | } |
28 | 31 | |
32 | +func (e *Email) Config(cfg *EmailConfig) { | |
33 | + e.config = cfg | |
34 | +} | |
35 | + | |
36 | +func (e *Email) SetFrom(from string) { | |
37 | + e.From = from | |
38 | +} | |
39 | + | |
40 | +func (e *Email) SetTo(to []string) { | |
41 | + e.To = to | |
42 | +} | |
43 | + | |
44 | +func (e *Email) SetSubject(sub string) { | |
45 | + e.Subject = sub | |
46 | +} | |
47 | + | |
48 | +func (e *Email) SetBody(body string) { | |
49 | + e.Body = body | |
50 | +} | |
51 | + | |
29 | 52 | func (e *Email) String() string { |
30 | 53 | var str strings.Builder |
31 | 54 | |
... | ... | @@ -52,14 +75,11 @@ func (e *Email) Bytes() []byte { |
52 | 75 | return []byte(e.String()) |
53 | 76 | } |
54 | 77 | |
55 | -func SendEmail(email *Email, conf *EmailConfig) error { | |
56 | - if email == nil { | |
57 | - return errors.New("no email to send") | |
58 | - } | |
59 | - | |
60 | - if conf == nil { | |
78 | +func (email *Email) Send() error { | |
79 | + if email.config == nil { | |
61 | 80 | return errors.New("email configuration not provided") |
62 | 81 | } |
82 | + conf := email.config | |
63 | 83 | |
64 | 84 | c, err := smtp.Dial(conf.ServerName) |
65 | 85 | if err != nil { | ... | ... |