From 22374fb5f881be7682c1a27a192bf6e9da45e096 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sun, 24 Dec 2017 12:46:15 -0800 Subject: [PATCH] Log contact form messages to disk, just in case --- core/contact.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/core/contact.go b/core/contact.go index 4c1dcbe..f8b6f8a 100644 --- a/core/contact.go +++ b/core/contact.go @@ -4,6 +4,9 @@ import ( "fmt" "html/template" "net/http" + "os" + "path/filepath" + "time" "github.com/gorilla/mux" "github.com/kirsle/blog/core/forms" @@ -48,6 +51,22 @@ func (b *Blog) ContactRoutes(r *mux.Router) { "Email": form.Email, }, }) + + // Log it to disk, too. + fh, err := os.OpenFile(filepath.Join(b.UserRoot, ".contact.log"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) + if err != nil { + b.Flash(w, r, "Error logging the message to disk: %s", err) + } else { + fh.WriteString(fmt.Sprintf( + "Date: %s\nName: %s\nEmail: %s\nSubject: %s\n\n%s\n\n--------------------\n\n", + time.Now().Format(time.UnixDate), + form.Name, + form.Email, + form.Subject, + form.Message, + )) + fh.Close() + } b.FlashAndRedirect(w, r, "/contact", "Your message has been sent.") } }