Example #1
0
// AddText applies quoted-printable encoding to the content of r before writing
// the encoded result in a new sub-part with media MIME type set to mediaType.
//
// Specifying the charset in the mediaType string is recommended
// ("plain/text; charset=utf-8").
func (p *Multipart) AddText(mediaType string, r io.Reader) error {
	if p.isClosed {
		return ErrPartClosed
	}

	p.header = textproto.MIMEHeader(map[string][]string{
		"Content-Type":              {mediaType},
		"Content-Transfer-Encoding": {"quoted-printable"},
	})

	w, err := p.writer.CreatePart(p.header)
	if err != nil {
		return err
	}

	reader := bufio.NewReader(r)
	encoder := qp.NewWriter(w)
	defer encoder.Close()
	buffer := make([]byte, maxLineLen)
	for {
		read, err := reader.Read(buffer[:])
		if err != nil {
			if err != io.EOF {
				return err
			}
			break
		}
		encoder.Write(buffer[:read])
	}
	fmt.Fprintf(w, crlf)
	fmt.Fprintf(w, crlf)
	return nil
}
Example #2
0
func (p *Multipart) AddText(mediaType string, txt string) error {
	if p.isClosed {
		return ErrPartClosed
	}

	p.header = textproto.MIMEHeader(map[string][]string{
		"Content-Type":              {mediaType},
		"Content-Transfer-Encoding": {"quoted-printable"},
	})

	w, err := p.writer.CreatePart(p.header)
	if err != nil {
		return err
	}

	encoder := qp.NewWriter(w)
	_, err = io.WriteString(encoder, txt)
	if err != nil {
		return err
	}

	encoder.Close()

	fmt.Fprintf(w, crlf)
	fmt.Fprintf(w, crlf)
	return nil
}
Example #3
0
func qpString(txt string) string {
	buf := bytes.NewBufferString("=?utf-8?q?")
	w := qp.NewWriter(buf)
	io.WriteString(w, txt)
	w.Close()

	buf.WriteString("?=")
	return buf.String()
}
Example #4
0
func qpStringWithGB2312(txt string) string {
	buf := bytes.NewBufferString("=?GB2312?Q?")
	bs, _, e := transform.Bytes(simplifiedchinese.GB18030.NewEncoder(), []byte(txt))
	if nil != e {
		return qpString(txt)
	}
	w := qp.NewWriter(buf)
	w.Write(bs)
	w.Close()

	buf.WriteString("?=")
	return buf.String()
}