2023-01-15 16:14:19 +01:00
|
|
|
// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors
|
2022-06-17 15:05:54 +02:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-03-12 15:10:01 +01:00
|
|
|
package mail
|
|
|
|
|
|
|
|
import (
|
2022-03-19 16:56:14 +01:00
|
|
|
"bytes"
|
2022-03-13 17:15:23 +01:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2022-03-12 15:10:01 +01:00
|
|
|
"io"
|
2022-03-14 10:29:53 +01:00
|
|
|
"mime"
|
2022-03-13 17:15:23 +01:00
|
|
|
"mime/multipart"
|
|
|
|
"mime/quotedprintable"
|
|
|
|
"net/textproto"
|
2022-03-14 10:29:53 +01:00
|
|
|
"path/filepath"
|
2022-03-20 19:11:58 +01:00
|
|
|
"sort"
|
2022-03-12 15:10:01 +01:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MaxHeaderLength defines the maximum line length for a mail header
|
|
|
|
// RFC 2047 suggests 76 characters
|
|
|
|
const MaxHeaderLength = 76
|
|
|
|
|
2022-08-14 12:06:26 +02:00
|
|
|
// MaxBodyLength defines the maximum line length for the mail body
|
|
|
|
// RFC 2047 suggests 76 characters
|
|
|
|
const MaxBodyLength = 76
|
|
|
|
|
2022-04-12 16:01:30 +02:00
|
|
|
// SingleNewLine represents a new line that can be used by the msgWriter to issue a carriage return
|
|
|
|
const SingleNewLine = "\r\n"
|
|
|
|
|
|
|
|
// DoubleNewLine represents a double new line that can be used by the msgWriter to
|
|
|
|
// indicate a new segement of the mail
|
|
|
|
const DoubleNewLine = "\r\n\r\n"
|
|
|
|
|
2022-03-12 15:10:01 +01:00
|
|
|
// msgWriter handles the I/O to the io.WriteCloser of the SMTP client
|
|
|
|
type msgWriter struct {
|
2024-02-24 12:43:01 +01:00
|
|
|
bytesWritten int64
|
|
|
|
charset Charset
|
|
|
|
depth int8
|
|
|
|
encoder mime.WordEncoder
|
|
|
|
err error
|
|
|
|
multiPartWriter [3]*multipart.Writer
|
|
|
|
partWriter io.Writer
|
|
|
|
writer io.Writer
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements the io.Writer interface for msgWriter
|
2024-02-24 12:43:01 +01:00
|
|
|
func (mw *msgWriter) Write(payload []byte) (int, error) {
|
2022-03-13 17:15:23 +01:00
|
|
|
if mw.err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to write due to previous error: %w", mw.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var n int
|
2024-02-24 12:43:01 +01:00
|
|
|
n, mw.err = mw.writer.Write(payload)
|
|
|
|
mw.bytesWritten += int64(n)
|
2022-03-13 17:15:23 +01:00
|
|
|
return n, mw.err
|
2022-03-12 15:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// writeMsg formats the message and sends it to its io.Writer
|
2024-02-24 12:43:01 +01:00
|
|
|
func (mw *msgWriter) writeMsg(msg *Msg) {
|
|
|
|
msg.addDefaultHeader()
|
|
|
|
msg.checkUserAgent()
|
|
|
|
mw.writeGenHeader(msg)
|
|
|
|
mw.writePreformattedGenHeader(msg)
|
2022-06-13 10:18:35 +02:00
|
|
|
|
|
|
|
// Set the FROM header (or envelope FROM if FROM is empty)
|
2024-02-24 12:43:01 +01:00
|
|
|
hasFrom := true
|
|
|
|
from, ok := msg.addrHeader[HeaderFrom]
|
|
|
|
if !ok || (len(from) == 0 || from == nil) {
|
|
|
|
from, ok = msg.addrHeader[HeaderEnvelopeFrom]
|
|
|
|
if !ok || (len(from) == 0 || from == nil) {
|
|
|
|
hasFrom = false
|
2022-06-13 10:18:35 +02:00
|
|
|
}
|
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
if hasFrom && (len(from) > 0 && from[0] != nil) {
|
|
|
|
mw.writeHeader(Header(HeaderFrom), from[0].String())
|
2022-06-13 10:18:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the rest of the address headers
|
2024-02-24 12:43:01 +01:00
|
|
|
for _, to := range []AddrHeader{HeaderTo, HeaderCc} {
|
|
|
|
if addresses, ok := msg.addrHeader[to]; ok {
|
|
|
|
var val []string
|
|
|
|
for _, addr := range addresses {
|
|
|
|
val = append(val, addr.String())
|
2022-03-12 15:10:01 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
mw.writeHeader(Header(to), val...)
|
2022-03-12 15:10:01 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-13 17:15:23 +01:00
|
|
|
|
2024-02-24 12:43:01 +01:00
|
|
|
if msg.hasMixed() {
|
|
|
|
mw.startMP("mixed", msg.boundary)
|
2022-04-12 16:01:30 +02:00
|
|
|
mw.writeString(DoubleNewLine)
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
if msg.hasRelated() {
|
|
|
|
mw.startMP("related", msg.boundary)
|
2022-04-12 16:01:30 +02:00
|
|
|
mw.writeString(DoubleNewLine)
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
if msg.hasAlt() {
|
|
|
|
mw.startMP(MIMEAlternative, msg.boundary)
|
2022-04-12 16:01:30 +02:00
|
|
|
mw.writeString(DoubleNewLine)
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
if msg.hasPGPType() {
|
|
|
|
switch msg.pgptype {
|
2023-01-31 18:35:48 +01:00
|
|
|
case PGPEncrypt:
|
2024-02-24 12:43:01 +01:00
|
|
|
mw.startMP(`encrypted; protocol="application/pgp-encrypted"`,
|
|
|
|
msg.boundary)
|
2023-01-31 18:35:48 +01:00
|
|
|
case PGPSignature:
|
2024-02-24 12:43:01 +01:00
|
|
|
mw.startMP(`signed; protocol="application/pgp-signature";`,
|
|
|
|
msg.boundary)
|
|
|
|
default:
|
2023-01-31 18:35:48 +01:00
|
|
|
}
|
|
|
|
mw.writeString(DoubleNewLine)
|
|
|
|
}
|
2022-03-14 10:29:53 +01:00
|
|
|
|
2024-02-24 12:43:01 +01:00
|
|
|
for _, part := range msg.parts {
|
2024-02-27 11:21:28 +01:00
|
|
|
if !part.isDeleted {
|
2024-02-24 12:43:01 +01:00
|
|
|
mw.writePart(part, msg.charset)
|
2023-01-28 14:39:14 +01:00
|
|
|
}
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
2023-01-31 18:35:48 +01:00
|
|
|
|
2024-02-24 12:43:01 +01:00
|
|
|
if msg.hasAlt() {
|
2022-03-13 17:15:23 +01:00
|
|
|
mw.stopMP()
|
|
|
|
}
|
2022-03-14 10:29:53 +01:00
|
|
|
|
|
|
|
// Add embeds
|
2024-02-24 12:43:01 +01:00
|
|
|
mw.addFiles(msg.embeds, false)
|
|
|
|
if msg.hasRelated() {
|
2022-03-14 10:29:53 +01:00
|
|
|
mw.stopMP()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add attachments
|
2024-02-24 12:43:01 +01:00
|
|
|
mw.addFiles(msg.attachments, true)
|
|
|
|
if msg.hasMixed() {
|
2022-03-14 10:29:53 +01:00
|
|
|
mw.stopMP()
|
|
|
|
}
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 19:11:58 +01:00
|
|
|
// writeGenHeader writes out all generic headers to the msgWriter
|
2024-02-24 12:43:01 +01:00
|
|
|
func (mw *msgWriter) writeGenHeader(msg *Msg) {
|
|
|
|
keys := make([]string, 0, len(msg.genHeader))
|
|
|
|
for key := range msg.genHeader {
|
|
|
|
keys = append(keys, string(key))
|
2022-03-20 19:11:58 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
sort.Strings(keys)
|
|
|
|
for _, key := range keys {
|
|
|
|
mw.writeHeader(Header(key), msg.genHeader[Header(key)]...)
|
2022-03-20 19:11:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 15:33:03 +02:00
|
|
|
// writePreformatedHeader writes out all preformated generic headers to the msgWriter
|
2024-02-24 12:43:01 +01:00
|
|
|
func (mw *msgWriter) writePreformattedGenHeader(msg *Msg) {
|
|
|
|
for key, val := range msg.preformHeader {
|
|
|
|
mw.writeString(fmt.Sprintf("%s: %s%s", key, val, SingleNewLine))
|
2022-10-26 15:33:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-13 17:15:23 +01:00
|
|
|
// startMP writes a multipart beginning
|
2024-02-24 12:43:01 +01:00
|
|
|
func (mw *msgWriter) startMP(mimeType MIMEType, boundary string) {
|
|
|
|
multiPartWriter := multipart.NewWriter(mw)
|
|
|
|
if boundary != "" {
|
|
|
|
mw.err = multiPartWriter.SetBoundary(boundary)
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
|
|
|
|
2024-02-24 12:43:01 +01:00
|
|
|
contentType := fmt.Sprintf("multipart/%s;\r\n boundary=%s", mimeType,
|
|
|
|
multiPartWriter.Boundary())
|
|
|
|
mw.multiPartWriter[mw.depth] = multiPartWriter
|
2022-03-13 17:15:23 +01:00
|
|
|
|
2024-02-24 12:43:01 +01:00
|
|
|
if mw.depth == 0 {
|
|
|
|
mw.writeString(fmt.Sprintf("%s: %s", HeaderContentType, contentType))
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
if mw.depth > 0 {
|
|
|
|
mw.newPart(map[string][]string{"Content-Type": {contentType}})
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
mw.depth++
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// stopMP closes the multipart
|
|
|
|
func (mw *msgWriter) stopMP() {
|
2024-02-24 12:43:01 +01:00
|
|
|
if mw.depth > 0 {
|
|
|
|
mw.err = mw.multiPartWriter[mw.depth-1].Close()
|
|
|
|
mw.depth--
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 10:29:53 +01:00
|
|
|
// addFiles adds the attachments/embeds file content to the mail body
|
2024-02-24 12:43:01 +01:00
|
|
|
func (mw *msgWriter) addFiles(files []*File, isAttachment bool) {
|
|
|
|
for _, file := range files {
|
|
|
|
encoding := EncodingB64
|
|
|
|
if _, ok := file.getHeader(HeaderContentType); !ok {
|
|
|
|
mimeType := mime.TypeByExtension(filepath.Ext(file.Name))
|
|
|
|
if mimeType == "" {
|
|
|
|
mimeType = "application/octet-stream"
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
if file.ContentType != "" {
|
|
|
|
mimeType = string(file.ContentType)
|
2023-01-31 18:35:48 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
file.setHeader(HeaderContentType, fmt.Sprintf(`%s; name="%s"`, mimeType,
|
|
|
|
mw.encoder.Encode(mw.charset.String(), file.Name)))
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
|
|
|
|
2024-02-24 12:43:01 +01:00
|
|
|
if _, ok := file.getHeader(HeaderContentTransferEnc); !ok {
|
|
|
|
if file.Enc != "" {
|
|
|
|
encoding = file.Enc
|
2023-01-31 18:35:48 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
file.setHeader(HeaderContentTransferEnc, string(encoding))
|
2023-01-31 18:35:48 +01:00
|
|
|
}
|
|
|
|
|
2024-02-24 12:43:01 +01:00
|
|
|
if file.Desc != "" {
|
|
|
|
if _, ok := file.getHeader(HeaderContentDescription); !ok {
|
|
|
|
file.setHeader(HeaderContentDescription, file.Desc)
|
2023-01-31 18:35:48 +01:00
|
|
|
}
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
|
|
|
|
2024-02-24 12:43:01 +01:00
|
|
|
if _, ok := file.getHeader(HeaderContentDisposition); !ok {
|
|
|
|
disposition := "inline"
|
|
|
|
if isAttachment {
|
|
|
|
disposition = "attachment"
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
file.setHeader(HeaderContentDisposition, fmt.Sprintf(`%s; filename="%s"`,
|
|
|
|
disposition, mw.encoder.Encode(mw.charset.String(), file.Name)))
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
|
|
|
|
2024-02-24 12:43:01 +01:00
|
|
|
if !isAttachment {
|
|
|
|
if _, ok := file.getHeader(HeaderContentID); !ok {
|
|
|
|
file.setHeader(HeaderContentID, fmt.Sprintf("<%s>", file.Name))
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
if mw.depth == 0 {
|
|
|
|
for header, val := range file.Header {
|
|
|
|
mw.writeHeader(Header(header), val...)
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
2022-04-12 16:01:30 +02:00
|
|
|
mw.writeString(SingleNewLine)
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
if mw.depth > 0 {
|
|
|
|
mw.newPart(file.Header)
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
2023-08-08 10:59:10 +02:00
|
|
|
|
|
|
|
if mw.err == nil {
|
2024-02-24 12:43:01 +01:00
|
|
|
mw.writeBody(file.Writer, encoding)
|
2023-08-08 10:59:10 +02:00
|
|
|
}
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-13 17:15:23 +01:00
|
|
|
// newPart creates a new MIME multipart io.Writer and sets the partwriter to it
|
2024-02-24 12:43:01 +01:00
|
|
|
func (mw *msgWriter) newPart(header map[string][]string) {
|
|
|
|
mw.partWriter, mw.err = mw.multiPartWriter[mw.depth-1].CreatePart(header)
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// writePart writes the corresponding part to the Msg body
|
2024-02-24 12:43:01 +01:00
|
|
|
func (mw *msgWriter) writePart(part *Part, charset Charset) {
|
2024-02-27 11:21:28 +01:00
|
|
|
partCharset := part.charset
|
2024-02-24 12:43:01 +01:00
|
|
|
if partCharset.String() == "" {
|
|
|
|
partCharset = charset
|
|
|
|
}
|
2024-02-27 11:21:28 +01:00
|
|
|
contentType := fmt.Sprintf("%s; charset=%s", part.contentType, partCharset)
|
|
|
|
contentTransferEnc := part.encoding.String()
|
2024-02-24 12:43:01 +01:00
|
|
|
if mw.depth == 0 {
|
|
|
|
mw.writeHeader(HeaderContentType, contentType)
|
|
|
|
mw.writeHeader(HeaderContentTransferEnc, contentTransferEnc)
|
2022-04-12 16:01:30 +02:00
|
|
|
mw.writeString(SingleNewLine)
|
2022-03-13 19:04:58 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
if mw.depth > 0 {
|
|
|
|
mimeHeader := textproto.MIMEHeader{}
|
2024-02-27 11:21:28 +01:00
|
|
|
if part.description != "" {
|
|
|
|
mimeHeader.Add(string(HeaderContentDescription), part.description)
|
2023-01-31 18:35:48 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
mimeHeader.Add(string(HeaderContentType), contentType)
|
|
|
|
mimeHeader.Add(string(HeaderContentTransferEnc), contentTransferEnc)
|
|
|
|
mw.newPart(mimeHeader)
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
2024-02-27 11:21:28 +01:00
|
|
|
mw.writeBody(part.writeFunc, part.encoding)
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// writeString writes a string into the msgWriter's io.Writer interface
|
|
|
|
func (mw *msgWriter) writeString(s string) {
|
|
|
|
if mw.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var n int
|
2024-02-24 12:43:01 +01:00
|
|
|
n, mw.err = io.WriteString(mw.writer, s)
|
|
|
|
mw.bytesWritten += int64(n)
|
2022-03-12 15:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// writeHeader writes a header into the msgWriter's io.Writer
|
2024-02-24 12:43:01 +01:00
|
|
|
func (mw *msgWriter) writeHeader(key Header, values ...string) {
|
2024-02-24 17:38:42 +01:00
|
|
|
buffer := strings.Builder{}
|
|
|
|
charLength := MaxHeaderLength - 2
|
|
|
|
buffer.WriteString(string(key))
|
|
|
|
charLength -= len(key)
|
2024-02-24 12:43:01 +01:00
|
|
|
if len(values) == 0 {
|
2024-02-24 17:38:42 +01:00
|
|
|
buffer.WriteString(":\r\n")
|
2022-03-12 15:10:01 +01:00
|
|
|
return
|
|
|
|
}
|
2024-02-24 17:38:42 +01:00
|
|
|
buffer.WriteString(": ")
|
|
|
|
charLength -= 2
|
2022-03-12 15:10:01 +01:00
|
|
|
|
2024-02-24 17:38:42 +01:00
|
|
|
fullValueStr := strings.Join(values, ", ")
|
|
|
|
words := strings.Split(fullValueStr, " ")
|
|
|
|
for i, val := range words {
|
|
|
|
if charLength-len(val) <= 1 {
|
|
|
|
buffer.WriteString(fmt.Sprintf("%s ", SingleNewLine))
|
|
|
|
charLength = MaxHeaderLength - 3
|
2022-03-12 15:10:01 +01:00
|
|
|
}
|
2024-02-24 17:38:42 +01:00
|
|
|
buffer.WriteString(val)
|
|
|
|
if i < len(words)-1 {
|
|
|
|
buffer.WriteString(" ")
|
|
|
|
charLength -= 1
|
2022-03-12 15:10:01 +01:00
|
|
|
}
|
2024-02-24 17:38:42 +01:00
|
|
|
charLength -= len(val)
|
2022-03-12 15:10:01 +01:00
|
|
|
}
|
2022-03-20 18:18:06 +01:00
|
|
|
|
2024-02-24 17:38:42 +01:00
|
|
|
bufferString := buffer.String()
|
|
|
|
bufferString = strings.ReplaceAll(bufferString, fmt.Sprintf(" %s", SingleNewLine),
|
|
|
|
SingleNewLine)
|
|
|
|
mw.writeString(bufferString)
|
2022-03-12 15:10:01 +01:00
|
|
|
mw.writeString("\r\n")
|
|
|
|
}
|
|
|
|
|
2022-03-13 17:15:23 +01:00
|
|
|
// writeBody writes an io.Reader into an io.Writer using provided Encoding
|
2024-02-24 17:38:42 +01:00
|
|
|
func (mw *msgWriter) writeBody(writeFunc func(io.Writer) (int64, error), encoding Encoding) {
|
|
|
|
var writer io.Writer
|
|
|
|
var encodedWriter io.WriteCloser
|
2022-03-18 17:08:05 +01:00
|
|
|
var n int64
|
2022-11-19 09:55:38 +01:00
|
|
|
var err error
|
2024-02-24 12:43:01 +01:00
|
|
|
if mw.depth == 0 {
|
2024-02-24 17:38:42 +01:00
|
|
|
writer = mw.writer
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
if mw.depth > 0 {
|
2024-02-24 17:38:42 +01:00
|
|
|
writer = mw.partWriter
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
2024-02-24 17:38:42 +01:00
|
|
|
writeBuffer := bytes.Buffer{}
|
|
|
|
lineBreaker := Base64LineBreaker{}
|
|
|
|
lineBreaker.out = &writeBuffer
|
2022-03-13 17:15:23 +01:00
|
|
|
|
2024-02-24 17:38:42 +01:00
|
|
|
switch encoding {
|
2022-03-13 17:15:23 +01:00
|
|
|
case EncodingQP:
|
2024-02-24 17:38:42 +01:00
|
|
|
encodedWriter = quotedprintable.NewWriter(&writeBuffer)
|
2022-03-13 17:15:23 +01:00
|
|
|
case EncodingB64:
|
2024-02-24 17:38:42 +01:00
|
|
|
encodedWriter = base64.NewEncoder(base64.StdEncoding, &lineBreaker)
|
2022-03-13 17:15:23 +01:00
|
|
|
case NoEncoding:
|
2024-02-24 17:38:42 +01:00
|
|
|
_, err = writeFunc(&writeBuffer)
|
2022-11-19 09:55:38 +01:00
|
|
|
if err != nil {
|
|
|
|
mw.err = fmt.Errorf("bodyWriter function: %w", err)
|
|
|
|
}
|
2024-02-24 17:38:42 +01:00
|
|
|
n, err = io.Copy(writer, &writeBuffer)
|
2022-11-19 09:55:38 +01:00
|
|
|
if err != nil && mw.err == nil {
|
|
|
|
mw.err = fmt.Errorf("bodyWriter io.Copy: %w", err)
|
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
if mw.depth == 0 {
|
|
|
|
mw.bytesWritten += n
|
2022-03-19 18:32:51 +01:00
|
|
|
}
|
2022-03-12 15:10:01 +01:00
|
|
|
return
|
2022-03-13 17:15:23 +01:00
|
|
|
default:
|
2024-02-24 17:38:42 +01:00
|
|
|
encodedWriter = quotedprintable.NewWriter(writer)
|
2022-03-12 15:10:01 +01:00
|
|
|
}
|
2022-03-13 17:15:23 +01:00
|
|
|
|
2024-02-24 17:38:42 +01:00
|
|
|
_, err = writeFunc(encodedWriter)
|
2022-11-19 09:55:38 +01:00
|
|
|
if err != nil {
|
|
|
|
mw.err = fmt.Errorf("bodyWriter function: %w", err)
|
|
|
|
}
|
2024-02-24 17:38:42 +01:00
|
|
|
err = encodedWriter.Close()
|
2022-11-19 09:55:38 +01:00
|
|
|
if err != nil && mw.err == nil {
|
|
|
|
mw.err = fmt.Errorf("bodyWriter close encoded writer: %w", err)
|
|
|
|
}
|
2024-02-24 17:38:42 +01:00
|
|
|
err = lineBreaker.Close()
|
2022-11-19 09:55:38 +01:00
|
|
|
if err != nil && mw.err == nil {
|
|
|
|
mw.err = fmt.Errorf("bodyWriter close linebreaker: %w", err)
|
|
|
|
}
|
2024-02-24 17:38:42 +01:00
|
|
|
n, err = io.Copy(writer, &writeBuffer)
|
2022-11-19 09:55:38 +01:00
|
|
|
if err != nil && mw.err == nil {
|
|
|
|
mw.err = fmt.Errorf("bodyWriter io.Copy: %w", err)
|
|
|
|
}
|
2022-03-19 18:32:51 +01:00
|
|
|
|
2022-05-24 15:46:59 +02:00
|
|
|
// Since the part writer uses the WriteTo() method, we don't need to add the
|
2022-03-19 18:32:51 +01:00
|
|
|
// bytes twice
|
2024-02-24 12:43:01 +01:00
|
|
|
if mw.depth == 0 {
|
|
|
|
mw.bytesWritten += n
|
2022-03-19 18:32:51 +01:00
|
|
|
}
|
2022-03-12 15:10:01 +01:00
|
|
|
}
|