GoBlog/contact.go

102 lines
2.8 KiB
Go
Raw Normal View History

2021-07-22 11:41:52 +00:00
package main
import (
"bytes"
"fmt"
"log"
"net/http"
"net/smtp"
"strconv"
"time"
)
const defaultContactPath = "/contact"
func (a *goBlog) serveContactForm(w http.ResponseWriter, r *http.Request) {
blog := r.Context().Value(blogKey).(string)
2021-07-22 11:41:52 +00:00
cc := a.cfg.Blogs[blog].Contact
a.render(w, r, templateContact, &renderData{
BlogString: blog,
Data: map[string]interface{}{
"title": cc.Title,
"description": cc.Description,
2021-07-22 12:04:46 +00:00
"privacy": cc.PrivacyPolicy,
2021-07-22 11:41:52 +00:00
},
})
}
func (a *goBlog) sendContactSubmission(w http.ResponseWriter, r *http.Request) {
// Get form values
// Name
2021-09-01 09:14:49 +00:00
formName := cleanHTMLText(r.FormValue("name"))
2021-07-22 11:41:52 +00:00
// Email
2021-09-01 09:14:49 +00:00
formEmail := cleanHTMLText(r.FormValue("email"))
2021-07-22 11:41:52 +00:00
// Website
2021-09-01 09:14:49 +00:00
formWebsite := cleanHTMLText(r.FormValue("website"))
2021-07-22 11:41:52 +00:00
// Message
2021-09-01 09:14:49 +00:00
formMessage := cleanHTMLText(r.FormValue("message"))
2021-07-22 11:41:52 +00:00
if formMessage == "" {
a.serveError(w, r, "Message is empty", http.StatusBadRequest)
return
}
// Build message
var message bytes.Buffer
if formName != "" {
_, _ = fmt.Fprintf(&message, "Name: %s", formName)
_, _ = fmt.Fprintln(&message)
}
if formEmail != "" {
_, _ = fmt.Fprintf(&message, "Email: %s", formEmail)
_, _ = fmt.Fprintln(&message)
}
if formWebsite != "" {
_, _ = fmt.Fprintf(&message, "Website: %s", formWebsite)
_, _ = fmt.Fprintln(&message)
}
if message.Len() > 0 {
_, _ = fmt.Fprintln(&message)
}
_, _ = message.WriteString(formMessage)
// Send submission
blog := r.Context().Value(blogKey).(string)
2021-07-22 11:41:52 +00:00
if cc := a.cfg.Blogs[blog].Contact; cc != nil && cc.SMTPHost != "" && cc.EmailFrom != "" && cc.EmailTo != "" {
// Build email
var email bytes.Buffer
if ef := cc.EmailFrom; ef != "" {
_, _ = fmt.Fprintf(&email, "From: %s <%s>", defaultIfEmpty(a.cfg.Blogs[blog].Title, "GoBlog"), cc.EmailFrom)
_, _ = fmt.Fprintln(&email)
}
_, _ = fmt.Fprintf(&email, "To: %s", cc.EmailTo)
_, _ = fmt.Fprintln(&email)
if formEmail != "" {
_, _ = fmt.Fprintf(&email, "Reply-To: %s", formEmail)
_, _ = fmt.Fprintln(&email)
}
_, _ = fmt.Fprintf(&email, "Date: %s", time.Now().UTC().Format(time.RFC1123Z))
_, _ = fmt.Fprintln(&email)
_, _ = fmt.Fprintln(&email, "Subject: New message")
_, _ = fmt.Fprintln(&email)
_, _ = fmt.Fprintln(&email, message.String())
// Send email
auth := smtp.PlainAuth("", cc.SMTPUser, cc.SMTPPassword, cc.SMTPHost)
port := cc.SMTPPort
if port == 0 {
port = 587
}
if err := smtp.SendMail(cc.SMTPHost+":"+strconv.Itoa(port), auth, cc.EmailFrom, []string{cc.EmailTo}, email.Bytes()); err != nil {
log.Println("Failed to send mail:", err.Error())
}
} else {
log.Println("New contact submission not send as email, config missing")
}
// Send notification
a.sendNotification(message.String())
// Give feedback
a.render(w, r, templateContact, &renderData{
BlogString: blog,
Data: map[string]interface{}{
"sent": true,
},
})
}