Example #1
0
func view(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)
	q := datastore.NewQuery("Person").Order("BirthYear")

	html := ""

	iterator := q.Run(ctx)
	for {
		var person Person
		_, err := iterator.Next(&person)
		if err == datastore.Done {
			break
		} else if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}
		html += person.Name + " - " + person.BirthYear + "\n"
	}

	tpl, err := template.ParseFiles("view.html")
	if err != nil {
		log.Fatalln(err)
	}

	err = tpl.ExecuteTemplate(res, "view.html", html)
	if err != nil {
		http.Error(res, err.Error(), 500)
	}
}
Example #2
0
func sendFunc(res http.ResponseWriter, req *http.Request) {
	var emails []string
	if req.Method == "POST" {
		ctx := appengine.NewContext(req)
		q := datastore.NewQuery("Email").Order("Name")

		iterator := q.Run(ctx)
		for {
			var person Email
			_, err := iterator.Next(&person)
			if err == datastore.Done {
				break
			} else if err != nil {
				http.Error(res, err.Error(), 500)
				return
			}
			emails = append(emails, person.EmailAddr)
		}
	}

	tpl, err := template.ParseFiles("saved.html")
	if err != nil {
		log.Fatalln(err)
	}

	err = tpl.ExecuteTemplate(res, "saved.html", emails)
	if err != nil {
		http.Error(res, err.Error(), 500)
	}
}
Example #3
0
func sentFunc(res http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		ctx := appengine.NewContext(req)

		q := datastore.NewQuery("Email").Order("Name")

		var emails []Email
		_, err := q.GetAll(ctx, &emails)
		if err != nil {
			err.Error()
		}

		tplt, err := template.ParseFiles("saved.html")
		if err != nil {
			log.Fatalln(err)
		}

		for _, email := range emails {
			name := email.Name
			email := email.EmailAddr

			msg := &mail.Message{
				Sender:   "neworganization.net Support <*****@*****.**>",
				To:       []string{email},
				Subject:  "Welcome to the club " + name + "!",
				HTMLBody: fmt.Sprint(tplt),
			}
			if err := mail.Send(ctx, msg); err != nil {
				ctx.Err()
			}
		}
	}
	tpl, err := template.ParseFiles("sent.html")
	if err != nil {
		log.Fatalln(err)
	}

	err = tpl.ExecuteTemplate(res, "sent.html", nil)
	if err != nil {
		http.Error(res, err.Error(), 500)
	}
}