Blame view

email.go 2.98 KB
bca3975fd   Marko Tikvić   email
1
2
3
4
5
6
7
8
9
10
11
  package webutility
  
  // TODO(markO): test test test test test (and open source?)
  
  import (
  	"crypto/tls"
  	"errors"
  	"fmt"
  	"net/smtp"
  	"strings"
  )
bca3975fd   Marko Tikvić   email
12
13
14
15
16
  type Email struct {
  	To      []string
  	From    string
  	Subject string
  	Body    string
edd7c4f4d   Marko Tikvić   new email interface
17
18
  
  	config *EmailConfig
bca3975fd   Marko Tikvić   email
19
  }
edd7c4f4d   Marko Tikvić   new email interface
20
  func NewEmail() *Email {
bca3975fd   Marko Tikvić   email
21
  	return &Email{
edd7c4f4d   Marko Tikvić   new email interface
22
23
24
25
26
  		To:      nil,
  		From:    "",
  		Subject: "",
  		Body:    "",
  		config:  nil,
bca3975fd   Marko Tikvić   email
27
28
  	}
  }
edd7c4f4d   Marko Tikvić   new email interface
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  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
  }
2cff4b70c   Marko Tikvić   refactored email:...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  func (e *Email) String() string {
  	var str strings.Builder
  
  	str.WriteString("From:" + e.From + "\r
  ")
  
  	str.WriteString("To:")
  	for i, _ := range e.To {
  		if i > 0 {
  			str.WriteString(",")
  		}
  		str.WriteString(e.To[i])
  	}
  	str.WriteString("\r
  ")
  
  	str.WriteString("Subject:" + e.Subject + "\r
  ")
  
  	// body
  	str.WriteString("\r
  " + e.Body + "\r
  ")
  
  	return str.String()
  }
  
  func (e *Email) Bytes() []byte {
  	return []byte(e.String())
  }
edd7c4f4d   Marko Tikvić   new email interface
78
79
  func (email *Email) Send() error {
  	if email.config == nil {
2cff4b70c   Marko Tikvić   refactored email:...
80
  		return errors.New("email configuration not provided")
bca3975fd   Marko Tikvić   email
81
  	}
edd7c4f4d   Marko Tikvić   new email interface
82
  	conf := email.config
bca3975fd   Marko Tikvić   email
83
84
85
86
87
  
  	c, err := smtp.Dial(conf.ServerName)
  	if err != nil {
  		return err
  	}
bca3975fd   Marko Tikvić   email
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  	defer c.Close()
  
  	// not sure if this is needed
  	//if err = c.Hello(conf.ServerName); err != nil {
  	//	return err
  	//}
  
  	if ok, _ := c.Extension("STARTTLS"); ok {
  		// disable stupid tls check for self-signed certificates
  		config := &tls.Config{
  			ServerName:         conf.ServerName,
  			InsecureSkipVerify: true,
  		}
  		// for golang testing
  		//if testHookStartTLS != nil {
  		//	testHookStartTLS(config)
  		//}
  		if err = c.StartTLS(config); err != nil {
  			return err
  		}
  	}
  
  	/*
  		// don't know what to do with this
  		if a != nil && c.ext != nil {
  			if _, ok := c.ext["AUTH"]; !ok {
  				return errors.New("smtp: server doesn't support AUTH")
  			}
  			if err = c.Auth(a); err != nil {
  				return err
  			}
  		}
  	*/
  
  	// Set up authentication information.
  	auth := smtp.PlainAuth(conf.Identity, conf.Username, conf.Password, conf.Host)
  	if err = c.Auth(auth); err != nil {
  		return err
  	}
  
  	if err = c.Mail(email.From); err != nil {
  		return err
  	}
  
  	for _, addr := range email.To {
  		if err = c.Rcpt(addr); err != nil {
  			return err
  		}
  	}
  
  	w, err := c.Data()
  	if err != nil {
  		return err
  	}
  
  	_, err = w.Write(email.Bytes())
  	if err != nil {
  		return err
  	}
  
  	err = w.Close()
  	if err != nil {
  		return err
  	}
  
  	return c.Quit()
  }
2cff4b70c   Marko Tikvić   refactored email:...
155
  type EmailConfig struct {
ab548c502   Marko Tikvić   added Valid() to ...
156
157
158
159
160
161
  	ServerName string `json:"-"`
  	Identity   string `json:"-"`
  	Username   string `json:"username"`
  	Password   string `json:"password"`
  	Host       string `json:"host"`
  	Port       int    `json:"port"`
bca3975fd   Marko Tikvić   email
162
  }
2cff4b70c   Marko Tikvić   refactored email:...
163
164
165
166
167
168
169
170
171
  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,
  	}
bca3975fd   Marko Tikvić   email
172
  }