This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
/
webcontact.go
60 lines (51 loc) · 1.54 KB
/
webcontact.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
package nlgids
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
"github.com/miekg/nlgids/email"
"github.com/miekg/nlgids/webcontact"
)
// WebContact sends an email to the recipients from a contact form from the website.
func (n *NLgids) WebContact(w http.ResponseWriter, r *http.Request) (int, error) {
name, email := r.PostFormValue("name"), r.PostFormValue("email")
phone, persons := r.PostFormValue("phone"), r.PostFormValue("persons")
message := r.PostFormValue("message")
if name == "" || email == "" || message == "" {
return http.StatusBadRequest, fmt.Errorf("nlgids: all empty")
}
if !strings.Contains(email, "@") {
return http.StatusBadRequest, fmt.Errorf("nlgids: invalid email")
}
if strings.Contains(email, " ") {
return http.StatusBadRequest, fmt.Errorf("nlgids: invalid email")
}
if persons != "" {
if _, err := strconv.Atoi(persons); err != nil {
return http.StatusBadRequest, err
}
}
contact := &webcontact.Contact{
Name: name,
Email: email,
Phone: phone,
Persons: persons,
Message: message,
}
log.Printf("[INFO] NLgids contact: %s:%s:%s:%s:%s", name, email, phone, persons, message)
return sendContactMail(contact, n.Config.Recipients)
}
func sendContactMail(c *webcontact.Contact, rcpts []string) (int, error) {
subject := c.MailSubject()
body, err := c.MailBody()
if err != nil {
return http.StatusInternalServerError, err
}
mail := email.NewContact(c.Email, subject, body)
if err := mail.Do(rcpts); err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}