-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (111 loc) · 2.64 KB
/
main.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
81
82
83
84
85
86
87
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
package main
import (
"fmt"
"log"
"net/http"
"net/smtp"
"reflect"
"strconv"
"strings"
"text/template"
)
const (
yandex = "smtp.yandex.ru"
yandex_port = "465"
google = "smtp.gmail.com"
google_port = "587"
)
type Email struct {
Email_field string
Password_field string
Text_field string
Email_field_rec []string
}
func (e Email) SendEmail() error {
var port string
var host string
switch (strings.Split(e.Email_field, "@"))[1] {
case "yandex.ru":
port = yandex_port
host = yandex
case "gmail.com":
port = google_port
host = google
}
auth := smtp.PlainAuth("", e.Email_field, e.Password_field, host)
err := smtp.SendMail(host+":"+port, auth, e.Email_field, []string(e.Email_field_rec), []byte(e.Text_field))
if err != nil {
return err
}
fmt.Println("Sending email...")
return nil
}
func SendForm(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.URL.Path == "/action" {
err := r.ParseForm()
if err != nil {
log.Println(err)
}
data := Email{
Email_field: r.FormValue("email"),
Password_field: r.FormValue("password"),
Text_field: r.FormValue("message"),
Email_field_rec: []string{r.FormValue("email2")},
}
val := reflect.ValueOf(data)
for i := 0; i <= val.NumField(); i++ {
if (val.Field(i).Interface()) == "" {
http.Redirect(w, r, "/", http.StatusSeeOther)
break
}
}
messageNumber, err := strconv.Atoi(r.FormValue("message_number"))
if err != nil {
// Handle the error appropriately
fmt.Fprint(w, err)
}
for i := 0; i <= messageNumber; i++ {
err = data.SendEmail()
if err != nil {
fmt.Fprint(w, err)
return
}
}
ts, err := template.ParseFiles("static/answer.html")
if err != nil {
log.Println(err.Error())
http.Error(w, "Internal Server Error", 500)
return
}
ts.Execute(w, nil)
} else {
ts, err := template.ParseFiles("static/form.html")
if err != nil {
log.Println(err.Error())
http.Error(w, "Internal Server Error", 500)
return
}
ts.Execute(w, nil)
}
}
func start_file(w http.ResponseWriter, r *http.Request) {
ts, err := template.ParseFiles("static/form.html")
if err != nil {
log.Println(err.Error())
http.Error(w, "Internal Server Error", 500)
return
}
err = ts.Execute(w, nil)
if err != nil {
log.Println(err.Error())
http.Error(w, "Internal Server Error", 500)
}
}
func main() {
mux := http.NewServeMux()
// 1. Create a new http server listening on port 8080
mux.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("./static"))))
mux.HandleFunc("/", start_file)
mux.HandleFunc("/action", SendForm)
log.Fatal(http.ListenAndServe(":80", mux))
}