forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.go
102 lines (96 loc) · 2.83 KB
/
contact.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
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
"go.goblog.app/app/pkgs/bufferpool"
)
const defaultContactPath = "/contact"
func (a *goBlog) serveContactForm(w http.ResponseWriter, r *http.Request) {
_, bc := a.getBlog(r)
cc := bc.Contact
a.render(w, r, a.renderContact, &renderData{
Data: &contactRenderData{
title: cc.Title,
description: cc.Description,
privacy: cc.PrivacyPolicy,
},
})
}
func (a *goBlog) sendContactSubmission(w http.ResponseWriter, r *http.Request) {
// Get blog
_, bc := a.getBlog(r)
// Get form values and build message
message := bufferpool.Get()
defer bufferpool.Put(message)
// Message
formMessage := cleanHTMLText(r.FormValue("message"))
if formMessage == "" {
a.serveError(w, r, "Message is empty", http.StatusBadRequest)
return
}
// Name
if formName := cleanHTMLText(r.FormValue("name")); formName != "" {
_, _ = fmt.Fprintf(message, "Name: %s\n", formName)
}
// Email
formEmail := cleanHTMLText(r.FormValue("email"))
if formEmail != "" {
_, _ = fmt.Fprintf(message, "Email: %s\n", formEmail)
}
// Website
if formWebsite := cleanHTMLText(r.FormValue("website")); formWebsite != "" {
_, _ = fmt.Fprintf(message, "Website: %s\n", formWebsite)
}
// Add line break if message is not empty
if message.Len() > 0 {
_, _ = fmt.Fprintf(message, "\n")
}
// Add message text to message
_, _ = message.WriteString(formMessage)
// Send submission
if err := a.sendContactEmail(bc.Contact, message.String(), formEmail); err != nil {
log.Println(err.Error())
}
// Send notification
a.sendNotification(message.String())
// Give feedback
a.render(w, r, a.renderContact, &renderData{
Data: &contactRenderData{
sent: true,
},
})
}
func (*goBlog) sendContactEmail(cc *configContact, body, replyTo string) error {
// Check required config
if cc == nil || cc.SMTPHost == "" || cc.EmailFrom == "" || cc.EmailTo == "" {
return fmt.Errorf("email not send as config is missing")
}
// Build email
email := bufferpool.Get()
defer bufferpool.Put(email)
_, _ = email.WriteString("Content-Type: text/plain; charset=UTF-8\n")
_, _ = fmt.Fprintf(email, "To: %s\n", cc.EmailTo)
if replyTo != "" {
_, _ = fmt.Fprintf(email, "Reply-To: %s\n", replyTo)
}
_, _ = fmt.Fprintf(email, "Date: %s\n", time.Now().UTC().Format(time.RFC1123Z))
_, _ = fmt.Fprintf(email, "From: %s\n", cc.EmailFrom)
subject := cc.EmailSubject
if subject == "" {
subject = "New contact message"
}
_, _ = fmt.Fprintf(email, "Subject: %s\n\n", subject)
_, _ = fmt.Fprintf(email, "%s\n", body)
// Send email using SMTP
auth := sasl.NewPlainClient("", cc.SMTPUser, cc.SMTPPassword)
port := cc.SMTPPort
if port == 0 {
port = 587
}
return smtp.SendMail(cc.SMTPHost+":"+strconv.Itoa(port), auth, cc.EmailFrom, []string{cc.EmailTo}, email)
}