8// HeaderWriter helps create headers, folding to the next line when it would
9// become too large. Useful for creating Received and DKIM-Signature headers.
10type HeaderWriter struct {
16// Addf formats the string and calls Add.
17func (w *HeaderWriter) Addf(separator string, format string, args ...any) {
18 w.Add(separator, fmt.Sprintf(format, args...))
21// Add adds texts, each separated by separator. Individual elements in text are
23func (w *HeaderWriter) Add(separator string, texts ...string) {
25 w.b = &strings.Builder{}
27 for _, text := range texts {
29 if w.nonfirst && w.lineLen > 1 && w.lineLen+len(separator)+n > 78 {
30 w.b.WriteString("\r\n\t")
32 } else if w.nonfirst && separator != "" {
33 w.b.WriteString(separator)
34 w.lineLen += len(separator)
37 w.lineLen += len(text)
42// AddWrap adds data, folding anywhere in the buffer. E.g. for base64 data.
43func (w *HeaderWriter) AddWrap(buf []byte) {
48 line, buf = buf[:n], buf[n:]
56 w.b.WriteString("\r\n\t")
62// Newline starts a new line.
63func (w *HeaderWriter) Newline() {
64 w.b.WriteString("\r\n\t")
69// String returns the header in string form, ending with \r\n.
70func (w *HeaderWriter) String() string {
71 return w.b.String() + "\r\n"