Ejemplo n.º 1
0
func (bm message) bodyEncrypted(gm *gomail.Message, pgpTo string) {

	pgpBuf := bufpool.Get()
	defer bufpool.Put(pgpBuf)

	msgBuf := bufpool.Get()
	defer bufpool.Put(msgBuf)

	bm.renderTemplate(msgBuf)

	// the next line may crash if the PGP key gets removed ... some how. but the crash is fine
	w, err := openpgp.Encrypt(pgpBuf, openpgp.EntityList{0: bm.mc.pgpEmailKeyEntities[pgpTo]}, nil, nil, nil)
	if err != nil {
		bm.mc.maillog.Errorf("PGP encrypt Error: %s", err)
		return
	}

	_, err = w.Write(msgBuf.Bytes())
	if err != nil {
		bm.mc.maillog.Errorf("PGP encrypt Write Error: %s", err)
		return
	}

	err = w.Close()
	if err != nil {
		bm.mc.maillog.Errorf("PGP encrypt Close Error: %s", err)
		return
	}

	b64Buf := make([]byte, base64.StdEncoding.EncodedLen(pgpBuf.Len()))
	base64.StdEncoding.Encode(b64Buf, pgpBuf.Bytes())

	gm.SetBody("text/plain", "This should be an OpenPGP/MIME encrypted message (RFC 4880 and 3156)")

	gm.Embed(
		bm.mc.pgpAttachmentName,
		gomail.SetCopyFunc(func(w io.Writer) error {
			if _, err := w.Write(pgpStartText); err != nil {
				return err
			}
			if _, err := w.Write(b64Buf); err != nil {
				return err
			}
			if _, err := w.Write(pgpEndText); err != nil {
				return err
			}
			return nil
		}),
	)
}
Ejemplo n.º 2
0
func (bm message) bodyUnencrypted(gm *gomail.Message) {
	contentType := "text/plain"
	if bm.mc.bodyIsHTML {
		contentType = "text/html"
	}

	buf := bufpool.Get()
	defer bufpool.Put(buf)

	bm.renderTemplate(buf)
	gm.SetBody(contentType, buf.String())
}
Ejemplo n.º 3
0
func (bm message) renderSubject(gm *gomail.Message) {
	subjBuf := bufpool.Get()
	defer bufpool.Put(subjBuf)

	err := bm.mc.subjectTpl.Execute(subjBuf, struct {
		Form    url.Values
		Request *http.Request
	}{
		Form:    bm.r.PostForm,
		Request: bm.r,
	})
	if err != nil {
		bm.mc.maillog.Errorf("Render Subject Error: %s\nForm: %#v\nWritten: %s", err, bm.r.PostForm, subjBuf)
	}
	gm.SetHeader("Subject", subjBuf.String())
}
Ejemplo n.º 4
0
func (h *handler) writeJSON(je JSONError, w http.ResponseWriter) (int, error) {
	buf := bufpool.Get()
	defer bufpool.Put(buf)

	w.Header().Set(headerContentType, headerApplicationJSONUTF8)

	// https://github.com/mholt/caddy/issues/637#issuecomment-189599332
	w.WriteHeader(je.Code)

	if err := json.NewEncoder(buf).Encode(je); err != nil {
		return http.StatusInternalServerError, err
	}
	if _, err := w.Write(buf.Bytes()); err != nil {
		return http.StatusInternalServerError, err
	}

	return StatusEmpty, nil
}