Beispiel #1
0
// Send a mail using the SendGrid API
func SendMail(c appengine.Context, mail *Mail) error {
	client := &http.Client{
		Transport: &urlfetch.Transport{
			Context:  c,
			Deadline: time.Duration(20) * time.Second,
		},
	}

	// Build the data needed for the API call
	data := url.Values{
		"api_user": []string{conf.MAIL_API_USER},
		"api_key":  []string{conf.MAIL_API_KEY},
		"to":       []string{mail.To},
		"toname":   []string{mail.ToName},
		"subject":  []string{mail.Subject},
		"html":     []string{mail.Html},
		"from":     []string{mail.From},
		"fromname": []string{mail.FromName},
	}

	// Request the SendGrid API
	resp, err := client.PostForm(conf.MAIL_SEND_API, data)
	if err != nil {
		return errors.New(err)
	}
	defer resp.Body.Close()

	// Decode the Mail API response
	var r mailAPI
	if err := json.NewDecoder(resp.Body).Decode(&r); err != nil {
		return errors.New(err)
	}

	// Test for errors in the api call
	if r.Message != "success" {
		return errors.Format("cannot send the mail: api %s message: %v",
			r.Message, r.Errors)
	}

	return nil
}
Beispiel #2
0
// Serves a http request
func (fn Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	c := appengine.NewContext(req)

	// Emit some compatibility anti-cache headers for IE
	w.Header().Set("X-UA-Compatible", "chrome=1")
	w.Header().Set("Cache-Control", "max-age=0,no-cache,no-store,post-check=0,pre-check=0")
	w.Header().Set("Expires", "Mon, 26 Jul 1997 05:00:00 GMT")

	r := &Request{Req: req, W: w, C: c}

	defer func() {
		if rec := recover(); rec != nil {
			err := errors.Format("panic recovered error: %s", rec)
			r.processError(err)
		}
	}()

	if err := fn(r); err != nil {
		r.processError(err)
	}
}