Last active
April 11, 2020 19:07
-
-
Save gspencerfabian/50ad2bef3b923f9c1a8d to your computer and use it in GitHub Desktop.
golang - post json contact form and email send from html template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{define "home"}} | |
<html> | |
<head> | |
<title>Hello World</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> | |
</head> | |
<body> | |
<h2>Welcome!</h2> | |
<p>Please fill out the following contact form</p> | |
<form id="contactForm"> | |
Name: <input type="text" name="nameForm"> | |
Email: <input type="text" name="emailForm"> | |
Phone: <input type="text" name="phoneForm"> | |
Message: <input type="text" name="messageForm"> | |
<input name="checkForm" type="checkbox" checked> I would like to be contacted at this phone number. | |
<input type="submit"> | |
</form> | |
<!-- <div name"sendSummary"></div> --> | |
<script> | |
(function ($) { | |
$.fn.serializeFormJSON = function () { | |
var o = {}; | |
var a = this.serializeArray(); | |
$.each(a, function () { | |
if (o[this.name]) { | |
if (!o[this.name].push) { | |
o[this.name] = [o[this.name]]; | |
} | |
o[this.name].push(this.value || ''); | |
} else { | |
o[this.name] = this.value || ''; | |
} | |
}); | |
return o; | |
}; | |
})(jQuery); | |
$(document).ready(function() { | |
$("#contactForm").submit(function(event) { | |
event.preventDefault(); | |
var data = $(this).serializeFormJSON(); | |
var json = JSON.stringify(data); | |
console.log(json); | |
$.post( "/send", json); | |
}); | |
}); | |
</script> | |
</body> | |
</html> | |
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"net/smtp" | |
"html/template" | |
"encoding/json" | |
) | |
var homeTmpl = template.Must(template.New("home").ParseFiles("templates/home.html")) | |
func homeHandle(w http.ResponseWriter, r *http.Request) { | |
homeTmpl.ExecuteTemplate(w, "home", nil) | |
} | |
type contactForm struct { | |
Name string `json:"nameForm"` | |
Email string `json:"emailForm"` | |
Phone string `json:"phoneForm"` | |
Check string `json:"checkForm"` | |
Message string `json:"messageForm"` | |
} | |
func sendHandle(rw http.ResponseWriter, req *http.Request) { | |
c := &contactForm{} | |
json.NewDecoder(req.Body).Decode(c) | |
to := "***RECIPIENT EMAIL***" | |
subject := "NEW CONTACT - Website Automation" | |
body := "To: " + to + "\r\nSubject: " + subject + "\r\n\r\n" + "Name: " + c.Name + "\r\n\r\n" + "Email: " + c.Email + "\r\n\r\n" + "Phone: " + c.Phone + "\r\n\r\n" + "Message: " + c.Message + "\r\n\r\n" + "OK to contact?: " + c.Check | |
auth := smtp.PlainAuth("", "***GMAIL USERNAME***", "***PASSWORD***", "smtp.gmail.com") | |
err := smtp.SendMail("smtp.gmail.com:587", auth, "***GMAIL USERNAME***", []string{to},[]byte(body)) | |
if err != nil { | |
log.Print("ERROR: attempting to send a mail ", err) | |
} | |
} | |
func main() { | |
http.HandleFunc("/home", homeHandle) | |
http.HandleFunc("/send", sendHandle) | |
http.ListenAndServe(":8080", nil) | |
} |
most I use jquery/ajax? won't it still work with just go alone?
nice, but how about file attachment email ??
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This article looks pretty cool. But could you please explain me, what is the purpose of using jquery in this form. Instead of that we can directly load the files in "main.go"