<commit_before>package email

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/mail"

	"github.com/zachlatta/go-mailpin/model"

	"appengine"
	"appengine/datastore"
	appMail "appengine/mail"
)

func IncomingHandler(w http.ResponseWriter, r *http.Request) *model.AppError {
	c := appengine.NewContext(r)
	defer r.Body.Close()

	msg, err := mail.ReadMessage(r.Body)
	if err != nil {
		return &model.AppError{err, "Error parsing email", http.StatusBadRequest}
	}

	addresses, err := msg.Header.AddressList("From")
	if err != nil {
		return &model.AppError{err, "Error parsing 'From' field in email",
			http.StatusBadRequest}
	}

	bytes, err := ioutil.ReadAll(msg.Body)
	if err != nil {
		return &model.AppError{err, "Internal server error",
			http.StatusInternalServerError}
	}

	hasher := md5.New()
	hasher.Write(bytes)
	id := hex.EncodeToString(hasher.Sum(nil))

	page := model.Page{
		Subject: msg.Header["Subject"][0],
		Body:    string(bytes),
	}

	key := model.NewPageKey(c, id)
	if _, err := datastore.Put(c, key, &page); err != nil {
		return &model.AppError{err, "Error storing page",
			http.StatusInternalServerError}
	}

	notifyMsg := &appMail.Message{
		Sender:  "go-mailpin <<EMAIL>>",
		To:      addressesToStrings(addresses),
		Subject: "Mailpin",
		Body:    fmt.Sprintf("https://go-mailpin.appspot.com/%s", id),
	}

	if err := appMail.Send(c, notifyMsg); err != nil {
		return &model.AppError{err, "Error sending mail",
			http.StatusInternalServerError}
	}

	return nil
}

func addressesToStrings(addresses []*mail.Address) []string {
	strings := make([]string, len(addresses))
	for i, address := range addresses {
		strings[i] = address.String()
	}
	return strings
}
<commit_msg>Send from appspotmail domain.<commit_after>package email

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/mail"

	"github.com/zachlatta/go-mailpin/model"

	"appengine"
	"appengine/datastore"
	appMail "appengine/mail"
)

func IncomingHandler(w http.ResponseWriter, r *http.Request) *model.AppError {
	c := appengine.NewContext(r)
	defer r.Body.Close()

	msg, err := mail.ReadMessage(r.Body)
	if err != nil {
		return &model.AppError{err, "Error parsing email", http.StatusBadRequest}
	}

	addresses, err := msg.Header.AddressList("From")
	if err != nil {
		return &model.AppError{err, "Error parsing 'From' field in email",
			http.StatusBadRequest}
	}

	bytes, err := ioutil.ReadAll(msg.Body)
	if err != nil {
		return &model.AppError{err, "Internal server error",
			http.StatusInternalServerError}
	}

	hasher := md5.New()
	hasher.Write(bytes)
	id := hex.EncodeToString(hasher.Sum(nil))

	page := model.Page{
		Subject: msg.Header["Subject"][0],
		Body:    string(bytes),
	}

	key := model.NewPageKey(c, id)
	if _, err := datastore.Put(c, key, &page); err != nil {
		return &model.AppError{err, "Error storing page",
			http.StatusInternalServerError}
	}

	notifyMsg := &appMail.Message{
		Sender:  "go-mailpin <<EMAIL>>",
		To:      addressesToStrings(addresses),
		Subject: "Mailpin",
		Body:    fmt.Sprintf("https://go-mailpin.appspot.com/%s", id),
	}

	if err := appMail.Send(c, notifyMsg); err != nil {
		return &model.AppError{err, "Error sending mail",
			http.StatusInternalServerError}
	}

	return nil
}

func addressesToStrings(addresses []*mail.Address) []string {
	strings := make([]string, len(addresses))
	for i, address := range addresses {
		strings[i] = address.String()
	}
	return strings
}

{'fraction_non_alphanumeric': 0.1573325635103926, 'fraction_numerical': 0.0017321016166281756, 'mean_word_length': 4.273972602739726, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '5872430', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1259, 'n_words': 267}