To my surprise I did not find a contact form mail handler example for go? I don't feel like making a wheel today, are there examples available?
EDIT: (cut and paste answer)
package bin
import (
"fmt"
"net/http"
netMail "net/mail"
"appengine"
"appengine/mail"
)
func contact(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
name := r.FormValue("name")
email := r.FormValue("email")
subject := r.FormValue("subject")
message := r.FormValue("message")
msg := &mail.Message{
Sender: name + " <...@yourappid.appspotmail.com>",
To: []string{"...@..."},
ReplyTo: email,
Subject: subject,
Body: message,
Headers: netMail.Header{
"On-Behalf-Of": []string{email},
},
}
if err := mail.Send(c, msg); err != nil {
c.Errorf("Couldn't send email: %v", err)
fmt.Fprint(w, "Mail NOT send! Error")
}else{
fmt.Fprint(w, "Mail send.")
}
}
NOTE:
1) ReplyTo
only works in gmail if Sender
and To
are different.
2) Sender
should have admin role in google cloud console or something@yourappid.appspotmail.com
.