Esempio n. 1
0
// Send sends mail
func Send(recipients []string, subject string, template string, context map[string]interface{}) error {

	// At present we use sendgrid, we may later allow many mail services to be used
	//	sg := sendgrid.NewSendGridClient(user, secret)
	sg := sendgrid.NewSendGridClientWithApiKey(secret)

	message := sendgrid.NewMail()
	message.SetFrom(from)
	message.AddTos(recipients)
	message.SetSubject(subject)

	// Hack for now, consider options TODO
	if context["reply_to"] != nil {
		replyTo := context["reply_to"].(string)
		message.SetReplyTo(replyTo)
	}

	// Load the template, and substitute using context
	// We should possibly set layout from caller too?
	view := view.New(&renderContext{})
	view.Template(template)
	view.Context(context)

	// We have a panic: runtime error: invalid memory address or nil pointer dereference
	// because of reloading templates on the fly I think
	// github.com/fragmenta/view/parser/template.html.go:90
	html, err := view.RenderString()
	if err != nil {
		return err
	}

	// For debug, print message
	//fmt.Printf("SENDING MAIL:\n%s", html)

	message.SetHTML(html)

	return sg.Send(message)
}
Esempio n. 2
0
// Send sends mail
func Send(recipients []string, subject string, template string, context map[string]interface{}) error {

	// For now  ensure that we don't send to more than 1 recipient while we debug emails
	if len(recipients) > 1 {
		return fmt.Errorf("mail.send: #error bad recipients for debug %v", recipients)
	}

	if recipients[0] != "*****@*****.**" {
		return fmt.Errorf("mail.send: #error bad recipients for debug %v", recipients)
	}

	// Send via sendgrid
	sg := sendgrid.NewSendGridClientWithApiKey(secret)

	message := sendgrid.NewMail()
	message.SetFrom(from)
	message.AddTos(recipients)
	message.SetSubject(subject)

	// Load the template, and substitute using context
	// We should possibly set layout from caller too?
	view := view.NewWithPath("", nil)
	view.Template(template)
	view.Context(context)

	html, err := view.RenderToString()
	if err != nil {
		return err
	}
	message.SetHTML(html)

	// For debug, print message
	fmt.Printf("#info sending MAIL to:%s", recipients)

	return sg.Send(message)
}