Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

102 linhas
2.1KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. log "github.com/sirupsen/logrus"
  6. "net/http"
  7. "net/smtp"
  8. "strings"
  9. )
  10. const formPrefix = "/form/"
  11. type formInput struct {
  12. w http.ResponseWriter
  13. r *http.Request
  14. }
  15. func formMain(w http.ResponseWriter, r *http.Request) {
  16. fi := formInput{w, r}
  17. fi.contactUs()
  18. }
  19. func (m *formInput) contactUs() {
  20. ret := map[string]interface{}{}
  21. ret["success"] = true
  22. m.r.ParseForm()
  23. name := m.r.FormValue("name")
  24. email := m.r.FormValue("email")
  25. message := m.r.FormValue("message")
  26. ret["name"] = name
  27. ret["email"] = email
  28. ret["message"] = message
  29. m.apiV1SendJson(ret)
  30. go sendContactUs(name, email, message)
  31. }
  32. func (m *formInput) apiV1SendJson(result interface{}) {
  33. //general setup
  34. m.w.Header().Set("Content-Type", "application/json;charset=UTF-8")
  35. out, e := json.Marshal(result)
  36. if e != nil {
  37. log.Warn("Cannot convert result to json ", result)
  38. }
  39. m.w.Header().Set("Content-Type", "text/json; charset=utf-8")
  40. //apiV1AddTrackingCookie(w, r, ss) //always the last one to set cookies
  41. fmt.Fprint(m.w, string(out))
  42. }
  43. func sendContactUs(name string, email string, userInput string) {
  44. // Sender data.
  45. from := "mailer@biukop.com.au"
  46. password := "hpfitsrujgkewcdw"
  47. // Receiver email address.
  48. to := []string{
  49. "patrick@biukop.com.au",
  50. }
  51. // smtp server configuration.
  52. smtpHost := "smtp.gmail.com"
  53. smtpPort := "587"
  54. raw := `Subject: {name} Contact form on Biukop Web
  55. Content-Type: text/plain; charset="UTF-8"
  56. Dear Manager,
  57. We receive a a form submission from biukop.com.au
  58. name : {name}
  59. email : {email}
  60. message:
  61. {message}
  62. Kind Regards
  63. Biukop Mailing service team.
  64. `
  65. raw = strings.Replace(raw, "{name}", name, -1)
  66. raw = strings.Replace(raw, "{email}", email, -1)
  67. raw = strings.Replace(raw, "{message}", userInput, -1)
  68. // Message.
  69. message := []byte(raw)
  70. // Authentication.
  71. auth := smtp.PlainAuth("", from, password, smtpHost)
  72. // Sending email.
  73. err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, message)
  74. if err != nil {
  75. fmt.Println(err)
  76. return
  77. }
  78. fmt.Println("Email Sent Successfully!")
  79. }