encode.go
func (p *Part) setupMIMEHeaders() transferEncoding {
	// Determine content transfer encoding.

	// If we are encoding a part that previously had content-transfer-encoding set, unset it so
	// the correct encoding detection can be done below.
	p.Header.Del(hnContentEncoding)

	cte := te7Bit
	if len(p.Content) > 0 {
		cte = teBase64
		if p.TextContent() {
			cte = selectTransferEncoding(p.Content, false)
			if p.Charset == "" {
				p.Charset = utf8
			}
		}
		// RFC 2045: 7bit is assumed if CTE header not present.
		switch cte {
		case teBase64:
			p.Header.Set(hnContentEncoding, cteBase64)
		case teQuoted:
			p.Header.Set(hnContentEncoding, cteQuotedPrintable)
		}
	}
	// Setup headers.
	if p.FirstChild != nil && p.Boundary == "" {
		// Multipart, generate random boundary marker.
		p.Boundary = "enmime-" + stringutil.UUID()
	}
	if p.ContentID != "" {
		p.Header.Set(hnContentID, coding.ToIDHeader(p.ContentID))
	}
	if p.ContentType != "" {
		// Build content type header.
		param := make(map[string]string)
		setParamValue(param, hpCharset, p.Charset)
		setParamValue(param, hpName, stringutil.ToASCII(p.FileName))
		setParamValue(param, hpBoundary, p.Boundary)
		mt := mime.FormatMediaType(p.ContentType, param)
		if mt == "" {
			// There was an error, FormatMediaType couldn't encode the params.
			mt = p.ContentType
		}
		p.Header.Set(hnContentType, mt)
	}
	if p.Disposition != "" {
		// Build disposition header.
		param := make(map[string]string)
		setParamValue(param, hpFilename, stringutil.ToASCII(p.FileName))
		if !p.FileModDate.IsZero() {
			setParamValue(param, hpModDate, p.FileModDate.Format(time.RFC822))
		}
		mt := mime.FormatMediaType(p.Disposition, param)
		if mt == "" {
			// There was an error, FormatMediaType couldn't encode the params.
			mt = p.Disposition
		}
		p.Header.Set(hnContentDisposition, mt)
	}
	return cte
}
func (p *Part) setupMIMEHeaders() transferEncoding {
	// Determine content transfer encoding.

	// If we are encoding a part that previously had content-transfer-encoding set, unset it so
	// the correct encoding detection can be done below.
	p.Header.Del(hnContentEncoding)

	cte := te7Bit
	if len(p.Content) > 0 {
		cte = teBase64
		if p.TextContent() {
			cte = selectTransferEncoding(p.Content, false)
			if p.Charset == "" {
				p.Charset = utf8
			}
		}
		// RFC 2045: 7bit is assumed if CTE header not present.
		switch cte {
		case teBase64:
			p.Header.Set(hnContentEncoding, cteBase64)
		case teQuoted:
			p.Header.Set(hnContentEncoding, cteQuotedPrintable)
		}
	}
	// Setup headers.
	if p.FirstChild != nil && p.Boundary == "" {
		// Multipart, generate random boundary marker.
		p.Boundary = "enmime-" + stringutil.UUID()
	}
	if p.ContentID != "" {
		p.Header.Set(hnContentID, coding.ToIDHeader(p.ContentID))
	}
	if p.ContentType != "" {
		// Build content type header.
		param := make(map[string]string)
		setParamValue(param, hpCharset, p.Charset)
		setParamValue(param, hpName, stringutil.ToASCII(p.FileName))
		setParamValue(param, hpBoundary, p.Boundary)
		mt := mime.FormatMediaType(p.ContentType, param)
		if mt == "" {
			// There was an error, FormatMediaType couldn't encode the params.
			mt = p.ContentType
		}
		p.Header.Set(hnContentType, mt)
	}
	if p.Disposition != "" {
		// Build disposition header.
		param := make(map[string]string)
		setParamValue(param, hpFilename, stringutil.ToASCII(p.FileName))
		if !p.FileModDate.IsZero() {
			setParamValue(param, hpModDate, p.FileModDate.Format(time.RFC822))
		}
		mt := mime.FormatMediaType(p.Disposition, param)
		if mt == "" {
			// There was an error, FormatMediaType couldn't encode the params.
			mt = p.Disposition
		}
		p.Header.Set(hnContentDisposition, mt)
	}
	return cte
}
// setupMIMEHeaders determines content transfer encoding, generates a boundary string if required,
// then sets the Content-Type (type, charset, filename, boundary) and Content-Disposition headers.