<commit_before>package api

import (
	"github.com/kennygrant/sanitize"
	"github.com/nathan-osman/go-cannon/email"
	"github.com/nathan-osman/go-cannon/queue"
	"github.com/zenazn/goji/web"

	"encoding/json"
	"html"
	"net/http"
)

// Parameters for the /send method.
type sendParams struct {
	From    string   `json:"from"`
	To      []string `json:"to"`
	Cc      []string `json:"cc"`
	Bcc     []string `json:"bcc"`
	Subject string   `json:"subject"`
	Text    string   `json:"text"`
	Html    string   `json:"html"`
}

// Send an email with the specified parameters.
func Send(c web.C, w http.ResponseWriter, r *http.Request) {
	var p sendParams
	if err := json.NewDecoder(r.Body).Decode(&p); err != nil {
		respondWithError(w, "malformed JSON")
	} else {

		// Ensure that if either 'text' or 'html' was not provided, its value
		// is populated by the other field
		if p.Html == "" {
			p.Html = html.EscapeString(p.Text)
		} else if p.Text == "" {
			p.Text = sanitize.HTML(p.Html)
		}

		// Create the individual emails to send and put them into the queue
		if emails, err := email.NewEmails(p.From, p.To, p.Cc, p.Bcc, p.Subject, p.Text, p.Html); err != nil {
			respondWithError(w, err.Error())
		} else {
			for _, e := range emails {
				c.Env["queue"].(*queue.Queue).Deliver(e)
			}
			respondWithJSON(w, struct{}{})
		}
	}
}<commit_msg>Updated /send method to use Email and Attachment types.<commit_after>package api

import (
	"github.com/kennygrant/sanitize"
	"github.com/nathan-osman/go-cannon/email"
	"github.com/nathan-osman/go-cannon/queue"
	"github.com/zenazn/goji/web"

	"encoding/json"
	"html"
	"net/http"
)

// Send an email with the specified parameters.
func Send(c web.C, w http.ResponseWriter, r *http.Request) {

	// Attempt to decode the parameters as an email.Email instance
	var e email.Email
	if err := json.NewDecoder(r.Body).Decode(&e); err != nil {
		respondWithError(w, err.Error())
	} else {

		// Ensure that if either 'text' or 'html' was not provided, its value
		// is populated by the other field
		if e.Html == "" {
			e.Html = html.EscapeString(e.Text)
		} else if e.Text == "" {
			e.Text = sanitize.HTML(e.Html)
		}

		// Convert the email into an array of messages
		if messages, err := e.Messages(); err == nil {

			// Deliver each of the messages to the queue
			for _, m := range messages {
				c.Env["queue"].(*queue.Queue).Deliver(m)
			}

			// Respond with an empty object
			respondWithJSON(w, struct{}{})

		} else {
			respondWithError(w, err.Error())
		}
	}
}
{'fraction_non_alphanumeric': 0.1526504583499402, 'fraction_numerical': 0.0, 'mean_word_length': 3.1625207296849087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5231427', 'n_tokens_mistral': 966, 'n_tokens_neox': 898, 'n_words': 242}