-
Notifications
You must be signed in to change notification settings - Fork 1
/
70-sendgrid.go
executable file
·65 lines (60 loc) · 1.99 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
package main
import (
"github.com/aerth/mbox"
sendgrid "gopkg.in/sendgrid/sendgrid-go.v2" //sendgrid "github.com/sendgrid/sendgrid-go"
)
// Form is sent by a user
type Form struct {
Name, Email, Subject, Message string
}
// sendgridder connects to the Sendgrid API and sends the form as an email to cosgo.Destination.
func (c *Cosgo) sendgridder(form *mbox.Form) (msg string, ok bool) {
sg := sendgrid.NewSendGridClientWithApiKey(*sendgridKey)
message := sendgrid.NewMail()
message.AddTo(c.Destination)
message.SetFrom(form.Email)
message.SetFromName(form.Name)
message.SetSubject(form.Subject)
message.SetText(form.Message)
r := sg.Send(message)
if r == nil {
return string("Sendgrid: Email sent to " + c.Destination), true
}
return string("Sendgrid Error:" + r.Error()), false
}
//
// // sendgridSender format for sendgrid and send it
// func (c *Cosgo) 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!
// msg, ok := c.sendgridder(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)
// }