-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendFreeBookViaMailAPI.go
64 lines (56 loc) · 1.75 KB
/
sendFreeBookViaMailAPI.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/smtp"
)
func sendFreeBookViaEmail(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
log.Println("SendFreeBookViaEmail")
fmt.Printf("%s\n", reqBody)
w.Write([]byte("Received a POST request\n"))
emails, ok := r.URL.Query()["email"]
if !ok || len(emails[0]) < 1 {
log.Println("SendFreeBookViaEmail URL Param 'email' is missing")
return
}
email := emails[0]
log.Println("\n SendFreeBookViaEmail URL Param 'email' is: " + email)
//Display all request params
for k, v := range r.URL.Query() {
fmt.Printf("SendFreeBookViaEmail %s: %s\n", k, v)
}
//Mail authorization
auth = smtp.PlainAuth("", "[email protected]", "password", "smtp.gmail.com")
templateAdminData := struct {
UserEmail string
}{
UserEmail: email,
}
rm := NewRequest([]string{"[email protected]"}, "Нове завантаження книги", "")
if err := rm.ParseTemplate("downloadFreeAdminTemplate.html", templateAdminData); err == nil {
ok, _ := rm.SendEmail()
fmt.Printf("SendFreeBookViaEmail email to admin sent... %t\n", ok)
}
templateData := struct {
URL string
}{
URL: "https://three-sides.com/pdf/Три сторони щастя (з реквізитами).pdf",
}
r := NewRequest([]string{email}, "Книга \"Три сторони щастя\"", "")
if err := r.ParseTemplate("mailTemplate.html", templateData); err == nil {
ok, _ := r.SendEmail()
fmt.Printf("SendFreeBookViaEmail email to user sent... %t\n", ok)
}
default:
w.WriteHeader(http.StatusNotImplemented)
w.Write([]byte(http.StatusText(http.StatusNotImplemented)))
}
}