diff --git a/email.go b/email.go index 750e479..7097825 100644 --- a/email.go +++ b/email.go @@ -10,26 +10,6 @@ import ( "strings" ) -type EmailConfig struct { - ServerName string - Identity string - Username string - Password string - Host string - Port int -} - -func NewEmailConfig(ident, uname, pword, host string, port int) *EmailConfig { - return &EmailConfig{ - ServerName: host + fmt.Sprintf(":%d", port), - Identity: ident, - Username: uname, - Password: pword, - Host: host, - Port: port, - } -} - type Email struct { To []string From string @@ -46,22 +26,45 @@ func NewEmail(to []string, from, subject, body string) *Email { } } +func (e *Email) String() string { + var str strings.Builder + + str.WriteString("From:" + e.From + "\r\n") + + str.WriteString("To:") + for i, _ := range e.To { + if i > 0 { + str.WriteString(",") + } + str.WriteString(e.To[i]) + } + str.WriteString("\r\n") + + str.WriteString("Subject:" + e.Subject + "\r\n") + + // body + str.WriteString("\r\n" + e.Body + "\r\n") + + return str.String() +} + +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 { - // use (some?) default settings - conf = NewEmailConfig("", "marko.tikvic@to-net.rs", "quantumleap010", "mail.to-net.rs", 25) + return errors.New("email configuration not provided") } c, err := smtp.Dial(conf.ServerName) if err != nil { return err } - defer c.Close() // not sure if this is needed @@ -130,28 +133,22 @@ func SendEmail(email *Email, conf *EmailConfig) error { return c.Quit() } -func (e *Email) String() string { - var str strings.Builder - - str.WriteString("From:" + e.From + "\r\n") - - str.WriteString("To:") - for i, _ := range e.To { - if i > 0 { - str.WriteString(",") - } - str.WriteString(e.To[i]) - } - str.WriteString("\r\n") - - str.WriteString("Subject:" + e.Subject + "\r\n") - - // body - str.WriteString("\r\n" + e.Body + "\r\n") - - return str.String() +type EmailConfig struct { + ServerName string + Identity string + Username string + Password string + Host string + Port int } -func (e *Email) Bytes() []byte { - return []byte(e.String()) +func NewEmailConfig(ident, uname, pword, host string, port int) *EmailConfig { + return &EmailConfig{ + ServerName: host + fmt.Sprintf(":%d", port), + Identity: ident, + Username: uname, + Password: pword, + Host: host, + Port: port, + } }