-
Notifications
You must be signed in to change notification settings - Fork 1
/
70-sendgrid.go
72 lines (63 loc) · 2 KB
/
70-sendgrid.go
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
72
package main
import (
"errors"
"log"
"net/http"
"net/url"
"github.com/aerth/mbox"
"github.com/goware/emailx"
//sendgrid "github.com/sendgrid/sendgrid-go"
sendgrid "gopkg.in/sendgrid/sendgrid-go.v2"
)
// Form is our email struct
type Form struct {
Name, Email, Subject, Message string
}
// sendgridSend connects to the Sendgrid API and sends the form as an email to destinationEmail.
func sendgridder(destinationEmail string, form *mbox.Form) (ok bool, msg string) {
//log.Println("Key: " + sendgridKey) // debug sendgrid
sg := sendgrid.NewSendGridClientWithApiKey(*sendgridKey)
message := sendgrid.NewMail()
message.AddTo(destinationEmail)
message.SetFrom(form.Email)
message.SetFromName(form.Name)
message.SetSubject(form.Subject)
message.SetText(form.Message)
r := sg.Send(message)
if r == nil {
return true, string("Sendgrid: Email sent to " + destinationEmail)
}
return false, string("Sendgrid Error:" + r.Error())
}
// sendgridSender
func sendgridSender(rw http.ResponseWriter, r *http.Request, destination string, query url.Values) error {
form := mbox.ParseQuery(query)
//Validate user submitted email address
err := emailx.Validate(form.Email)
if err != nil {
if err == emailx.ErrInvalidFormat {
return errors.New("Bad email format.")
}
if err == emailx.ErrUnresolvableHost {
return errors.New("Bad email provider.")
}
return errors.New("Bad email address.")
}
//Normalize email address
form.Email = emailx.Normalize(form.Email)
// Is it empty?
if form.Email == "" || form.Email == "@" {
return errors.New("Bad email address.")
}
// Looks good! Lets send it to sendgrid!
ok, msg := sendgridder(destination, form)
// Is good send!
if ok == true {
log.Printf("SUCCESS-contact: %s at %s", r.UserAgent(), r.RemoteAddr)
log.Printf(msg+" %s at %s", r.UserAgent(), r.RemoteAddr)
return nil
}
// The send wasn't good. Sending msg to log just in case it was important.
log.Printf("Bad Message: %q from a %s at %s", msg, r.UserAgent(), r.RemoteAddr)
return errors.New(msg)
}