|
|
|
@ -1,7 +1,6 @@
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
@ -10,6 +9,7 @@ import (
|
|
|
|
|
|
|
|
|
|
"github.com/emersion/go-sasl"
|
|
|
|
|
"github.com/emersion/go-smtp"
|
|
|
|
|
"go.goblog.app/app/pkgs/bufferpool"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const defaultContactPath = "/contact"
|
|
|
|
@ -30,7 +30,8 @@ func (a *goBlog) sendContactSubmission(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// Get blog
|
|
|
|
|
_, bc := a.getBlog(r)
|
|
|
|
|
// Get form values and build message
|
|
|
|
|
var message bytes.Buffer
|
|
|
|
|
message := bufferpool.Get()
|
|
|
|
|
defer bufferpool.Put(message)
|
|
|
|
|
// Message
|
|
|
|
|
formMessage := cleanHTMLText(r.FormValue("message"))
|
|
|
|
|
if formMessage == "" {
|
|
|
|
@ -39,20 +40,20 @@ func (a *goBlog) sendContactSubmission(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
}
|
|
|
|
|
// Name
|
|
|
|
|
if formName := cleanHTMLText(r.FormValue("name")); formName != "" {
|
|
|
|
|
_, _ = fmt.Fprintf(&message, "Name: %s\n", formName)
|
|
|
|
|
_, _ = fmt.Fprintf(message, "Name: %s\n", formName)
|
|
|
|
|
}
|
|
|
|
|
// Email
|
|
|
|
|
formEmail := cleanHTMLText(r.FormValue("email"))
|
|
|
|
|
if formEmail != "" {
|
|
|
|
|
_, _ = fmt.Fprintf(&message, "Email: %s\n", formEmail)
|
|
|
|
|
_, _ = fmt.Fprintf(message, "Email: %s\n", formEmail)
|
|
|
|
|
}
|
|
|
|
|
// Website
|
|
|
|
|
if formWebsite := cleanHTMLText(r.FormValue("website")); formWebsite != "" {
|
|
|
|
|
_, _ = fmt.Fprintf(&message, "Website: %s\n", formWebsite)
|
|
|
|
|
_, _ = fmt.Fprintf(message, "Website: %s\n", formWebsite)
|
|
|
|
|
}
|
|
|
|
|
// Add line break if message is not empty
|
|
|
|
|
if message.Len() > 0 {
|
|
|
|
|
_, _ = fmt.Fprintf(&message, "\n")
|
|
|
|
|
_, _ = fmt.Fprintf(message, "\n")
|
|
|
|
|
}
|
|
|
|
|
// Add message text to message
|
|
|
|
|
_, _ = message.WriteString(formMessage)
|
|
|
|
@ -76,24 +77,25 @@ func (a *goBlog) sendContactEmail(cc *configContact, body, replyTo string) error
|
|
|
|
|
return fmt.Errorf("email not send as config is missing")
|
|
|
|
|
}
|
|
|
|
|
// Build email
|
|
|
|
|
var email bytes.Buffer
|
|
|
|
|
_, _ = fmt.Fprintf(&email, "To: %s\n", cc.EmailTo)
|
|
|
|
|
email := bufferpool.Get()
|
|
|
|
|
defer bufferpool.Put(email)
|
|
|
|
|
_, _ = fmt.Fprintf(email, "To: %s\n", cc.EmailTo)
|
|
|
|
|
if replyTo != "" {
|
|
|
|
|
_, _ = fmt.Fprintf(&email, "Reply-To: %s\n", 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)
|
|
|
|
|
_, _ = 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)
|
|
|
|
|
_, _ = 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}, bytes.NewReader(email.Bytes()))
|
|
|
|
|
return smtp.SendMail(cc.SMTPHost+":"+strconv.Itoa(port), auth, cc.EmailFrom, []string{cc.EmailTo}, email)
|
|
|
|
|
}
|
|
|
|
|