-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
71 lines (63 loc) · 2.25 KB
/
main.go
File metadata and controls
71 lines (63 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"net/http"
"net/smtp"
"os"
log "github.com/sirupsen/logrus"
)
type contactForm struct {
Name string `json:"name"`
Email string `json:"email"`
Message string `json:"message"`
}
func sendEmail(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
log.Errorf("Invalid request type for sending a email, %s", r.Method)
}
// get credentials set in env
from := os.Getenv("FROMMAIL")
password := os.Getenv("PASSWD")
to := os.Getenv("TOMAIL")
// read form data
name := r.FormValue("name")
email := r.FormValue("email")
message := r.FormValue("message")
// send mail with above info
subject := "Portfolio-Contact"
body := "To: " + to + "\r\nSubject: " + subject + "\r\n\r\n" + "Name: " + name + "\r\n\r\n" + "Email: " + email + "\r\n\r\n" + "\r\n\r\n" + "Message: " + message
auth := smtp.PlainAuth("", from, password, "smtp.gmail.com")
err := smtp.SendMail("smtp.gmail.com:587", auth, from, []string{to}, []byte(body))
if err != nil {
log.Errorf("Error occurred while sending an email, %v", err)
fmt.Fprintln(w, `<script type="text/javascript">alert("ERROR: attempting to send a mail !"); window.location = '/';</script>`)
} else {
log.Infof("Successfully sent email with name:msg, %s:%s", name, message)
fmt.Fprintln(w, `<script type="text/javascript">alert("mail sent successfully!"); window.location = '/';</script>`)
}
}
func main() {
// retrieve port variable set by heroku
// https://devcenter.heroku.com/articles/getting-started-with-go#define-config-vars
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
logger := &log.Logger{
Out: os.Stderr,
Level: log.DebugLevel,
Formatter: &log.TextFormatter{
DisableColors: false,
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
},
}
logger.Printf("Starting portfolio server....")
http.HandleFunc("/sendEmail", sendEmail)
http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("./static"))))
http.Handle("/templates/", http.StripPrefix("/templates", http.FileServer(http.Dir("./templates"))))
http.Handle("/", http.FileServer(http.Dir(".")))
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Errorf("failed to start portfolio server, Reason %v", err)
}
}