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
1 package webutility 1 package webutility
2 2
3 // TODO(markO): test test test test test (and open source?) 3 // TODO(markO): test test test test test (and open source?)
4 4
5 import ( 5 import (
6 "crypto/tls" 6 "crypto/tls"
7 "errors" 7 "errors"
8 "fmt" 8 "fmt"
9 "net/smtp" 9 "net/smtp"
10 "strings" 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 type Email struct { 13 type Email struct {
34 To []string 14 To []string
35 From string 15 From string
36 Subject string 16 Subject string
37 Body string 17 Body string
38 } 18 }
39 19
40 func NewEmail(to []string, from, subject, body string) *Email { 20 func NewEmail(to []string, from, subject, body string) *Email {
41 return &Email{ 21 return &Email{
42 To: to, 22 To: to,
43 From: from, 23 From: from,
44 Subject: subject, 24 Subject: subject,
45 Body: body, 25 Body: body,
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 func SendEmail(email *Email, conf *EmailConfig) error { 55 func SendEmail(email *Email, conf *EmailConfig) error {
50 if email == nil { 56 if email == nil {
51 return errors.New("no email to send") 57 return errors.New("no email to send")
52
53 } 58 }
54 59
55 if conf == nil { 60 if conf == nil {
56 // use (some?) default settings 61 return errors.New("email configuration not provided")
57 conf = NewEmailConfig("", "marko.tikvic@to-net.rs", "quantumleap010", "mail.to-net.rs", 25)
58 } 62 }
59 63
60 c, err := smtp.Dial(conf.ServerName) 64 c, err := smtp.Dial(conf.ServerName)
61 if err != nil { 65 if err != nil {
62 return err 66 return err
63 } 67 }
64
65 defer c.Close() 68 defer c.Close()
66 69
67 // not sure if this is needed 70 // not sure if this is needed
68 //if err = c.Hello(conf.ServerName); err != nil { 71 //if err = c.Hello(conf.ServerName); err != nil {
69 // return err 72 // return err
70 //} 73 //}
71 74
72 if ok, _ := c.Extension("STARTTLS"); ok { 75 if ok, _ := c.Extension("STARTTLS"); ok {
73 // disable stupid tls check for self-signed certificates 76 // disable stupid tls check for self-signed certificates
74 config := &tls.Config{ 77 config := &tls.Config{
75 ServerName: conf.ServerName, 78 ServerName: conf.ServerName,
76 InsecureSkipVerify: true, 79 InsecureSkipVerify: true,
77 } 80 }
78 // for golang testing 81 // for golang testing
79 //if testHookStartTLS != nil { 82 //if testHookStartTLS != nil {
80 // testHookStartTLS(config) 83 // testHookStartTLS(config)
81 //} 84 //}
82 if err = c.StartTLS(config); err != nil { 85 if err = c.StartTLS(config); err != nil {
83 return err 86 return err
84 } 87 }
85 } 88 }
86 89
87 /* 90 /*
88 // don't know what to do with this 91 // don't know what to do with this
89 if a != nil && c.ext != nil { 92 if a != nil && c.ext != nil {
90 if _, ok := c.ext["AUTH"]; !ok { 93 if _, ok := c.ext["AUTH"]; !ok {
91 return errors.New("smtp: server doesn't support AUTH") 94 return errors.New("smtp: server doesn't support AUTH")
92 } 95 }
93 if err = c.Auth(a); err != nil { 96 if err = c.Auth(a); err != nil {
94 return err 97 return err
95 } 98 }
96 } 99 }
97 */ 100 */
98 101
99 // Set up authentication information. 102 // Set up authentication information.
100 auth := smtp.PlainAuth(conf.Identity, conf.Username, conf.Password, conf.Host) 103 auth := smtp.PlainAuth(conf.Identity, conf.Username, conf.Password, conf.Host)
101 if err = c.Auth(auth); err != nil { 104 if err = c.Auth(auth); err != nil {
102 return err 105 return err
103 } 106 }
104 107
105 if err = c.Mail(email.From); err != nil { 108 if err = c.Mail(email.From); err != nil {
106 return err 109 return err
107 } 110 }
108 111
109 for _, addr := range email.To { 112 for _, addr := range email.To {
110 if err = c.Rcpt(addr); err != nil { 113 if err = c.Rcpt(addr); err != nil {
111 return err 114 return err
112 } 115 }
113 } 116 }
114 117
115 w, err := c.Data() 118 w, err := c.Data()
116 if err != nil { 119 if err != nil {
117 return err 120 return err
118 } 121 }
119 122
120 _, err = w.Write(email.Bytes()) 123 _, err = w.Write(email.Bytes())
121 if err != nil { 124 if err != nil {
122 return err 125 return err
123 } 126 }
124 127
125 err = w.Close() 128 err = w.Close()
126 if err != nil { 129 if err != nil {
127 return err 130 return err
128 } 131 }
129 132
130 return c.Quit() 133 return c.Quit()
131 } 134 }
132 135