Log contact form messages to disk, just in case

pull/4/head
Noah 2017-12-24 12:46:15 -08:00
parent ce4f0d2f7c
commit 22374fb5f8
1 changed files with 19 additions and 0 deletions

View File

@ -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.")
}
}