Commit 2cff4b70cd681676eeb7c9c8fd02ef147bd95fbc

Authored by Marko Tikvić
1 parent bca3975fdb
Exists in master

refactored email: no more default config

Showing 1 changed file with 43 additions and 46 deletions   Show diff stats
... ... @@ -10,26 +10,6 @@ import (
10 10 "strings"
11 11 )
12 12  
13   -type EmailConfig struct {
14   - ServerName string
15   - Identity string
16   - Username string
17   - Password string
18   - Host string
19   - Port int
20   -}
21   -
22   -func NewEmailConfig(ident, uname, pword, host string, port int) *EmailConfig {
23   - return &EmailConfig{
24   - ServerName: host + fmt.Sprintf(":%d", port),
25   - Identity: ident,
26   - Username: uname,
27   - Password: pword,
28   - Host: host,
29   - Port: port,
30   - }
31   -}
32   -
33 13 type Email struct {
34 14 To []string
35 15 From string
... ... @@ -46,22 +26,45 @@ func NewEmail(to []string, from, subject, body string) *Email {
46 26 }
47 27 }
48 28  
  29 +func (e *Email) String() string {
  30 + var str strings.Builder
  31 +
  32 + str.WriteString("From:" + e.From + "\r\n")
  33 +
  34 + str.WriteString("To:")
  35 + for i, _ := range e.To {
  36 + if i > 0 {
  37 + str.WriteString(",")
  38 + }
  39 + str.WriteString(e.To[i])
  40 + }
  41 + str.WriteString("\r\n")
  42 +
  43 + str.WriteString("Subject:" + e.Subject + "\r\n")
  44 +
  45 + // body
  46 + str.WriteString("\r\n" + e.Body + "\r\n")
  47 +
  48 + return str.String()
  49 +}
  50 +
  51 +func (e *Email) Bytes() []byte {
  52 + return []byte(e.String())
  53 +}
  54 +
49 55 func SendEmail(email *Email, conf *EmailConfig) error {
50 56 if email == nil {
51 57 return errors.New("no email to send")
52   -
53 58 }
54 59  
55 60 if conf == nil {
56   - // use (some?) default settings
57   - conf = NewEmailConfig("", "marko.tikvic@to-net.rs", "quantumleap010", "mail.to-net.rs", 25)
  61 + return errors.New("email configuration not provided")
58 62 }
59 63  
60 64 c, err := smtp.Dial(conf.ServerName)
61 65 if err != nil {
62 66 return err
63 67 }
64   -
65 68 defer c.Close()
66 69  
67 70 // not sure if this is needed
... ... @@ -130,28 +133,22 @@ func SendEmail(email *Email, conf *EmailConfig) error {
130 133 return c.Quit()
131 134 }
132 135  
133   -func (e *Email) String() string {
134   - var str strings.Builder
135   -
136   - str.WriteString("From:" + e.From + "\r\n")
137   -
138   - str.WriteString("To:")
139   - for i, _ := range e.To {
140   - if i > 0 {
141   - str.WriteString(",")
142   - }
143   - str.WriteString(e.To[i])
144   - }
145   - str.WriteString("\r\n")
146   -
147   - str.WriteString("Subject:" + e.Subject + "\r\n")
148   -
149   - // body
150   - str.WriteString("\r\n" + e.Body + "\r\n")
151   -
152   - return str.String()
  136 +type EmailConfig struct {
  137 + ServerName string
  138 + Identity string
  139 + Username string
  140 + Password string
  141 + Host string
  142 + Port int
153 143 }
154 144  
155   -func (e *Email) Bytes() []byte {
156   - return []byte(e.String())
  145 +func NewEmailConfig(ident, uname, pword, host string, port int) *EmailConfig {
  146 + return &EmailConfig{
  147 + ServerName: host + fmt.Sprintf(":%d", port),
  148 + Identity: ident,
  149 + Username: uname,
  150 + Password: pword,
  151 + Host: host,
  152 + Port: port,
  153 + }
157 154 }
... ...