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"
|
|
|
|
)
|
|
|
|
|
2024-10-06 16:42:50 +02:00
|
|
|
const (
|
|
|
|
// MaxHeaderLength defines the maximum line length for a mail header.
|
|
|
|
//
|
|
|
|
// This constant follows the recommendation of RFC 2047, which suggests a maximum length of 76 characters.
|
|
|
|
//
|
|
|
|
// References:
|
|
|
|
// - https://datatracker.ietf.org/doc/html/rfc2047
|
|
|
|
MaxHeaderLength = 76
|
|
|
|
|
|
|
|
// MaxBodyLength defines the maximum line length for the mail body.
|
|
|
|
//
|
|
|
|
// This constant follows the recommendation of RFC 2047, which suggests a maximum length of 76 characters.
|
|
|
|
//
|
|
|
|
// References:
|
|
|
|
// - https://datatracker.ietf.org/doc/html/rfc2047
|
|
|
|
MaxBodyLength = 76
|
|
|
|
|
|
|
|
// SingleNewLine represents a single newline character sequence ("\r\n").
|
|
|
|
//
|
|
|
|
// This constant can be used by the msgWriter to issue a carriage return when writing mail content.
|
|
|
|
SingleNewLine = "\r\n"
|
|
|
|
|
|
|
|
// DoubleNewLine represents a double newline character sequence ("\r\n\r\n").
|
|
|
|
//
|
|
|
|
// This constant can be used by the msgWriter to indicate a new segment of the mail when writing mail content.
|
|
|
|
DoubleNewLine = "\r\n\r\n"
|
|
|
|
)
|
2022-04-12 16:01:30 +02:00
|
|
|
|
2024-10-06 16:42:50 +02:00
|
|
|
// msgWriter handles the I/O operations for writing to the io.WriteCloser of the SMTP client.
|
|
|
|
//
|
|
|
|
// This struct keeps track of the number of bytes written, the character set used, and the depth of the
|
|
|
|
// current multipart section. It also handles encoding, error tracking, and managing multipart and part
|
|
|
|
// writers for constructing the email message body.
|
2022-03-12 15:10:01 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-10-06 16:42:50 +02:00
|
|
|
// Write implements the io.Writer interface for msgWriter.
|
|
|
|
//
|
|
|
|
// This method writes the provided payload to the underlying writer. It keeps track of the number of bytes
|
|
|
|
// written and handles any errors encountered during the writing process. If a previous error exists, it
|
|
|
|
// prevents further writing and returns the error.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - payload: A byte slice containing the data to be written.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - The number of bytes successfully written.
|
|
|
|
// - An error if the writing process fails, or if a previous error was encountered.
|
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
|
|
|
}
|
|
|
|
|
2024-10-06 16:42:50 +02:00
|
|
|
// writeMsg formats the message and writes it to the msgWriter's io.Writer.
|
|
|
|
//
|
|
|
|
// This method handles the process of writing the message headers and body content, including handling
|
|
|
|
// multipart structures (e.g., mixed, related, alternative), PGP types, and attachments/embeds. It sets the
|
|
|
|
// required headers (e.g., "From", "To", "Cc") and iterates over the message parts, writing them to the
|
|
|
|
// output writer.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - msg: A pointer to the Msg struct containing the message data and headers to be written.
|
|
|
|
//
|
|
|
|
// References:
|
|
|
|
// - https://datatracker.ietf.org/doc/html/rfc2045 (Multipurpose Internet Mail Extensions - MIME)
|
|
|
|
// - https://datatracker.ietf.org/doc/html/rfc5322 (Internet Message Format)
|
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-09-18 14:23:26 +02:00
|
|
|
if msg.hasSMime() {
|
|
|
|
mw.startMP(MIMESMime, msg.boundary)
|
|
|
|
mw.writeString(DoubleNewLine)
|
|
|
|
}
|
2024-02-24 12:43:01 +01:00
|
|
|
if msg.hasMixed() {
|
2024-05-16 15:18:26 +02:00
|
|
|
mw.startMP(MIMEMixed, 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() {
|
2024-05-16 15:18:26 +02:00
|
|
|
mw.startMP(MIMERelated, msg.boundary)
|
2022-04-12 16:01:30 +02:00
|
|
|
mw.writeString(DoubleNewLine)
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
2024-09-26 16:32:51 +02:00
|
|
|
if msg.hasAlt() {
|
2024-02-24 12:43:01 +01:00
|
|
|
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()
|
|
|
|
}
|
2024-09-29 16:46:30 +02:00
|
|
|
|
|
|
|
if msg.hasSMime() {
|
|
|
|
mw.stopMP()
|
|
|
|
}
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:42:50 +02:00
|
|
|
// writeGenHeader writes out all generic headers to the msgWriter.
|
|
|
|
//
|
|
|
|
// This function extracts all generic headers from the provided Msg object, sorts them, and writes them
|
|
|
|
// to the msgWriter in alphabetical order.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - msg: The Msg object containing the headers to be written.
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 16:42:50 +02:00
|
|
|
// writePreformattedGenHeader writes out all preformatted generic headers to the msgWriter.
|
|
|
|
//
|
|
|
|
// This function iterates over all preformatted generic headers from the provided Msg object and writes
|
|
|
|
// them to the msgWriter in the format "key: value" followed by a newline.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - msg: The Msg object containing the preformatted headers to be written.
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 16:42:50 +02:00
|
|
|
// startMP writes a multipart beginning.
|
|
|
|
//
|
|
|
|
// This function initializes a multipart writer for the msgWriter using the specified MIME type and
|
|
|
|
// boundary. It sets the Content-Type header to indicate the multipart type and writes the boundary
|
|
|
|
// information. If a boundary is provided, it is set explicitly; otherwise, a default boundary is
|
|
|
|
// generated. It also handles writing a new part when nested multipart structures are used.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - mimeType: The MIME type of the multipart content (e.g., "mixed", "alternative").
|
|
|
|
// - boundary: The boundary string separating different parts of the multipart message.
|
|
|
|
//
|
|
|
|
// References:
|
|
|
|
// - https://datatracker.ietf.org/doc/html/rfc2046
|
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
|
|
|
}
|
|
|
|
|
2024-10-06 16:42:50 +02:00
|
|
|
// stopMP closes the multipart.
|
|
|
|
//
|
|
|
|
// This function closes the current multipart writer if there is an active multipart structure.
|
|
|
|
// It decreases the depth level of multipart nesting.
|
2022-03-13 17:15:23 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 16:42:50 +02:00
|
|
|
// addFiles adds the attachments/embeds file content to the mail body.
|
|
|
|
//
|
|
|
|
// This function iterates through the list of files, setting necessary headers for each file,
|
|
|
|
// including Content-Type, Content-Transfer-Encoding, Content-Disposition, and Content-ID
|
|
|
|
// (if the file is an embed). It determines the appropriate MIME type for each file based on
|
|
|
|
// its extension or the provided ContentType. It writes file headers and file content
|
|
|
|
// to the mail body using the appropriate encoding.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - files: A slice of File objects to be added to the mail body.
|
|
|
|
// - isAttachment: A boolean indicating whether the files are attachments (true) or embeds (false).
|
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,
|
2024-11-26 11:37:26 +01:00
|
|
|
mw.encoder.Encode(mw.charset.String(), sanitizeFilename(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 {
|
2024-11-26 11:37:26 +01:00
|
|
|
file.setHeader(HeaderContentDescription, mw.encoder.Encode(mw.charset.String(), 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"`,
|
2024-11-26 11:37:26 +01:00
|
|
|
disposition, mw.encoder.Encode(mw.charset.String(), sanitizeFilename(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 {
|
2024-11-26 11:37:26 +01:00
|
|
|
file.setHeader(HeaderContentID, fmt.Sprintf("<%s>", sanitizeFilename(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-09-26 16:32:51 +02:00
|
|
|
mw.writeBody(file.Writer, encoding, false)
|
2023-08-08 10:59:10 +02:00
|
|
|
}
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 16:42:50 +02:00
|
|
|
// newPart creates a new MIME multipart io.Writer and sets the partWriter to it.
|
|
|
|
//
|
|
|
|
// This function creates a new MIME part using the provided header information and assigns it
|
|
|
|
// to the partWriter. It interacts with the current multipart writer at the specified depth
|
|
|
|
// to create the part.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - header: A map containing the header fields and their corresponding values for the new part.
|
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
|
|
|
}
|
|
|
|
|
2024-10-06 16:42:50 +02:00
|
|
|
// writePart writes the corresponding part to the Msg body.
|
|
|
|
//
|
|
|
|
// This function writes a MIME part to the message body, setting the appropriate headers such
|
|
|
|
// as Content-Type and Content-Transfer-Encoding. It determines the charset for the part,
|
|
|
|
// either using the part's own charset or a fallback charset if none is specified. If the part
|
|
|
|
// is at the top level (depth 0), headers are written directly. For nested parts, it creates
|
|
|
|
// a new MIME part with the provided headers.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - part: The Part object containing the data to be written.
|
|
|
|
// - charset: The Charset used as a fallback if the part does not specify one.
|
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-10-09 13:53:15 +02:00
|
|
|
|
|
|
|
contentType := part.contentType.String()
|
|
|
|
if !part.IsSMimeSigned() {
|
|
|
|
contentType = strings.Join([]string{contentType, "; charset=", partCharset.String()}, "")
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:21:28 +01:00
|
|
|
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-09-26 16:32:51 +02:00
|
|
|
mw.writeBody(part.writeFunc, part.encoding, part.smime)
|
2022-03-13 17:15:23 +01:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:42:50 +02:00
|
|
|
// writeString writes a string into the msgWriter's io.Writer interface.
|
|
|
|
//
|
|
|
|
// This function writes the given string to the msgWriter's underlying writer. It checks for
|
|
|
|
// existing errors before performing the write operation. It also tracks the number of bytes
|
|
|
|
// written and updates the bytesWritten field accordingly.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - s: The string to be written.
|
2022-03-13 17:15:23 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-10-06 16:42:50 +02:00
|
|
|
// writeHeader writes a header into the msgWriter's io.Writer.
|
|
|
|
//
|
|
|
|
// This function writes a header key and its associated values to the msgWriter. It ensures
|
|
|
|
// proper formatting of long headers by inserting line breaks as needed. The header values
|
|
|
|
// are joined and split into words to ensure compliance with the maximum header length
|
|
|
|
// (MaxHeaderLength). After processing the header, it is written to the underlying writer.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - key: The Header key to be written.
|
|
|
|
// - values: A variadic parameter representing the values associated with the header.
|
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")
|
|
|
|
}
|
|
|
|
|
2024-10-06 16:42:50 +02:00
|
|
|
// writeBody writes an io.Reader into an io.Writer using the provided Encoding.
|
|
|
|
//
|
|
|
|
// This function writes data from an io.Reader to the underlying writer using a specified
|
|
|
|
// encoding (quoted-printable, base64, or no encoding). It handles encoding of the content
|
|
|
|
// and manages writing the encoded data to the appropriate writer, depending on the depth
|
|
|
|
// (whether the data is part of a multipart structure or not). It also tracks the number
|
|
|
|
// of bytes written and manages any errors encountered during the process.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - writeFunc: A function that writes the body content to the given io.Writer.
|
|
|
|
// - encoding: The encoding type to use when writing the content (e.g., base64, quoted-printable).
|
2024-10-09 16:25:29 +02:00
|
|
|
// - singingWithSMime: Whether the msg should be signed with S/MIME or not.
|
2024-09-26 16:32:51 +02:00
|
|
|
func (mw *msgWriter) writeBody(writeFunc func(io.Writer) (int64, error), encoding Encoding, singingWithSMime bool) {
|
2024-02-24 17:38:42 +01:00
|
|
|
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-09-26 16:32:51 +02:00
|
|
|
if encoding == EncodingQP {
|
2024-02-24 17:38:42 +01:00
|
|
|
encodedWriter = quotedprintable.NewWriter(&writeBuffer)
|
2024-09-26 16:32:51 +02:00
|
|
|
} else if encoding == EncodingB64 && !singingWithSMime {
|
2024-02-24 17:38:42 +01:00
|
|
|
encodedWriter = base64.NewEncoder(base64.StdEncoding, &lineBreaker)
|
2024-09-29 16:46:30 +02:00
|
|
|
} else if encoding == NoEncoding || singingWithSMime {
|
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
|
2024-09-26 16:32:51 +02:00
|
|
|
} else {
|
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
|
|
|
}
|
2024-11-26 11:37:26 +01:00
|
|
|
|
|
|
|
// sanitizeFilename sanitizes a given filename string by replacing specific unwanted characters with
|
|
|
|
// an underscore ('_').
|
|
|
|
//
|
|
|
|
// This method replaces any control character and any special character that is problematic for
|
|
|
|
// MIME headers and file systems with an underscore ('_') character.
|
|
|
|
//
|
|
|
|
// The following characters are replaced
|
|
|
|
// - Any control character (US-ASCII < 32)
|
|
|
|
// - ", /, :, <, >, ?, \, |, [DEL]
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - input: A string of a filename that is supposed to be sanitized
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - A string representing the sanitized version of the filename
|
|
|
|
func sanitizeFilename(input string) string {
|
|
|
|
var sanitized strings.Builder
|
|
|
|
for i := 0; i < len(input); i++ {
|
|
|
|
// We do not allow control characters in file names.
|
|
|
|
if input[i] < 32 || input[i] == 34 || input[i] == 47 || input[i] == 58 ||
|
|
|
|
input[i] == 60 || input[i] == 62 || input[i] == 63 || input[i] == 92 ||
|
|
|
|
input[i] == 124 || input[i] == 127 {
|
|
|
|
sanitized.WriteRune('_')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sanitized.WriteByte(input[i])
|
|
|
|
}
|
|
|
|
return sanitized.String()
|
|
|
|
}
|