-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.go
81 lines (69 loc) · 1.75 KB
/
mail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
78
79
80
// forms.go
package main
import (
"fmt"
"html/template"
"net/http"
"net/smtp"
)
type ContactDetails struct {
FEmail string
EmailPassword string
TEmail string
Subject string
Message string
}
func main() {
tmpl := template.Must(template.ParseFiles("forms.html"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
tmpl.Execute(w, nil)
return
}
details := ContactDetails{
FEmail: r.FormValue("femail"),
EmailPassword: r.FormValue("epass"),
TEmail: r.FormValue("temail"),
Subject: r.FormValue("subject"),
Message: r.FormValue("message"),
}
// do something with details
_ = details
// Address URI to smtp server
// Sender data.
from := details.FEmail
password := details.EmailPassword
// Receiver email address.
to := []string{details.TEmail}
// smtp server configuration.
smtpServer := smtpServer{host: "smtp.gmail.com", port: "587"}
// Message.
//message := []byte("This is a really unimaginative message, I know.")
message := []byte("To: " + details.TEmail +
"\r\n" +
"Subject: " + details.Subject +
"\r\n" +
details.Message)
//m := "Subject: discount Gophers!\r\n"
//copy(message[:], details.Message)
fmt.Printf("%q\n", message)
// Authentication.
auth := smtp.PlainAuth(from, from, password, smtpServer.host)
// Sending email.
err := smtp.SendMail(smtpServer.Address(), auth, from, to, message)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Email Sent!")
tmpl.Execute(w, struct{ Success bool }{true})
})
http.ListenAndServe(":8080", nil)
}
type smtpServer struct {
host string
port string
}
func (s *smtpServer) Address() string {
return s.host + ":" + s.port
}