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-14 10:29:53 +01:00
|
|
|
package mail
|
|
|
|
|
2022-10-11 17:05:44 +02:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
)
|
2022-03-14 10:29:53 +01:00
|
|
|
|
|
|
|
// PartOption returns a function that can be used for grouping Part options
|
|
|
|
type PartOption func(*Part)
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// Part is a part of the Msg.
|
|
|
|
//
|
|
|
|
// This struct represents a single part of a multipart message. Each part has a content type,
|
|
|
|
// charset, optional description, encoding, and a function to write its content to an io.Writer.
|
|
|
|
// It also includes a flag to mark the part as deleted.
|
2022-03-14 10:29:53 +01:00
|
|
|
type Part struct {
|
2024-02-27 11:21:28 +01:00
|
|
|
contentType ContentType
|
|
|
|
charset Charset
|
|
|
|
description string
|
|
|
|
encoding Encoding
|
|
|
|
isDeleted bool
|
|
|
|
writeFunc func(io.Writer) (int64, error)
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// GetContent executes the WriteFunc of the Part and returns the content as a byte slice.
|
|
|
|
//
|
|
|
|
// This function runs the part's writeFunc to write its content into a buffer and then returns
|
|
|
|
// the content as a byte slice. If an error occurs during the writing process, it is returned.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - A byte slice containing the part's content.
|
|
|
|
// - An error if the writeFunc encounters an issue.
|
2022-10-11 17:05:44 +02:00
|
|
|
func (p *Part) GetContent() ([]byte, error) {
|
|
|
|
var b bytes.Buffer
|
2024-02-27 11:21:28 +01:00
|
|
|
if _, err := p.writeFunc(&b); err != nil {
|
2022-10-11 17:05:44 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// GetCharset returns the currently set Charset of the Part.
|
|
|
|
//
|
|
|
|
// This function returns the Charset that is currently set for the Part.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - The Charset of the Part.
|
2024-02-05 11:41:10 +01:00
|
|
|
func (p *Part) GetCharset() Charset {
|
2024-02-27 11:21:28 +01:00
|
|
|
return p.charset
|
2024-02-05 11:41:10 +01:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// GetContentType returns the currently set ContentType of the Part.
|
|
|
|
//
|
|
|
|
// This function returns the ContentType that is currently set for the Part.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - The ContentType of the Part.
|
2022-10-11 17:05:44 +02:00
|
|
|
func (p *Part) GetContentType() ContentType {
|
2024-02-27 11:21:28 +01:00
|
|
|
return p.contentType
|
2022-10-11 17:05:44 +02:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// GetEncoding returns the currently set Encoding of the Part.
|
|
|
|
//
|
|
|
|
// This function returns the Encoding that is currently set for the Part.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - The Encoding of the Part.
|
2022-10-11 17:05:44 +02:00
|
|
|
func (p *Part) GetEncoding() Encoding {
|
2024-02-27 11:21:28 +01:00
|
|
|
return p.encoding
|
2022-10-11 17:05:44 +02:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// GetWriteFunc returns the currently set WriteFunc of the Part.
|
|
|
|
//
|
|
|
|
// This function returns the WriteFunc that is currently set for the Part, which writes
|
|
|
|
// the part's content to an io.Writer.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - The WriteFunc of the Part, which is a function that takes an io.Writer and returns
|
|
|
|
// the number of bytes written and an error (if any).
|
2022-10-11 17:05:44 +02:00
|
|
|
func (p *Part) GetWriteFunc() func(io.Writer) (int64, error) {
|
2024-02-27 11:21:28 +01:00
|
|
|
return p.writeFunc
|
2022-10-11 17:05:44 +02:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// GetDescription returns the currently set Content-Description of the Part.
|
|
|
|
//
|
|
|
|
// This function returns the Content-Description that is currently set for the Part.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - The Content-Description of the Part as a string.
|
2023-01-31 18:35:48 +01:00
|
|
|
func (p *Part) GetDescription() string {
|
2024-02-27 11:21:28 +01:00
|
|
|
return p.description
|
2023-01-31 18:35:48 +01:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// SetContent overrides the content of the Part with the given string.
|
|
|
|
//
|
|
|
|
// This function sets the content of the Part by creating a new writeFunc that writes the
|
|
|
|
// provided string content to an io.Writer.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - content: The string that will replace the current content of the Part.
|
2024-02-27 11:21:28 +01:00
|
|
|
func (p *Part) SetContent(content string) {
|
|
|
|
buffer := bytes.NewBufferString(content)
|
|
|
|
p.writeFunc = writeFuncFromBuffer(buffer)
|
2022-10-11 19:27:54 +02:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// SetContentType overrides the ContentType of the Part.
|
|
|
|
//
|
|
|
|
// This function sets a new ContentType for the Part, replacing the existing one.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - contentType: The new ContentType to be set for the Part.
|
2024-02-27 11:21:28 +01:00
|
|
|
func (p *Part) SetContentType(contentType ContentType) {
|
|
|
|
p.contentType = contentType
|
2022-10-11 17:05:44 +02:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// SetCharset overrides the Charset of the Part.
|
|
|
|
//
|
|
|
|
// This function sets a new Charset for the Part, replacing the existing one.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - charset: The new Charset to be set for the Part.
|
2024-02-27 11:21:28 +01:00
|
|
|
func (p *Part) SetCharset(charset Charset) {
|
|
|
|
p.charset = charset
|
2024-01-31 16:28:41 +01:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// SetEncoding creates a new mime.WordEncoder based on the encoding setting of the message.
|
|
|
|
//
|
|
|
|
// This function sets a new Encoding for the Part, replacing the existing one.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - encoding: The new Encoding to be set for the Part.
|
2024-02-27 11:21:28 +01:00
|
|
|
func (p *Part) SetEncoding(encoding Encoding) {
|
|
|
|
p.encoding = encoding
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// SetDescription overrides the Content-Description of the Part.
|
|
|
|
//
|
|
|
|
// This function sets a new Content-Description for the Part, replacing the existing one.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - description: The new Content-Description to be set for the Part.
|
2024-02-27 11:21:28 +01:00
|
|
|
func (p *Part) SetDescription(description string) {
|
|
|
|
p.description = description
|
2023-01-31 18:35:48 +01:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// SetWriteFunc overrides the WriteFunc of the Part.
|
|
|
|
//
|
|
|
|
// This function sets a new WriteFunc for the Part, replacing the existing one. The WriteFunc
|
|
|
|
// is responsible for writing the Part's content to an io.Writer.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - writeFunc: A function that writes the Part's content to an io.Writer and returns
|
|
|
|
// the number of bytes written and an error (if any).
|
2024-02-27 11:21:28 +01:00
|
|
|
func (p *Part) SetWriteFunc(writeFunc func(io.Writer) (int64, error)) {
|
|
|
|
p.writeFunc = writeFunc
|
2022-10-11 17:05:44 +02:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// Delete removes the current part from the parts list of the Msg by setting the isDeleted flag to true.
|
|
|
|
//
|
|
|
|
// This function marks the Part as deleted by setting the isDeleted flag to true. The msgWriter
|
|
|
|
// will skip over this Part during processing.
|
2023-01-28 14:39:14 +01:00
|
|
|
func (p *Part) Delete() {
|
2024-02-27 11:21:28 +01:00
|
|
|
p.isDeleted = true
|
2023-01-28 14:39:14 +01:00
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// WithPartCharset overrides the default Part charset.
|
|
|
|
//
|
|
|
|
// This function returns a PartOption that allows the charset of a Part to be overridden
|
|
|
|
// with the specified Charset.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - charset: The Charset to be set for the Part.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - A PartOption function that sets the Part's charset.
|
2024-02-27 11:21:28 +01:00
|
|
|
func WithPartCharset(charset Charset) PartOption {
|
2024-01-31 16:28:41 +01:00
|
|
|
return func(p *Part) {
|
2024-02-27 11:21:28 +01:00
|
|
|
p.charset = charset
|
2024-01-31 16:28:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// WithPartEncoding overrides the default Part encoding.
|
|
|
|
//
|
|
|
|
// This function returns a PartOption that allows the encoding of a Part to be overridden
|
|
|
|
// with the specified Encoding.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - encoding: The Encoding to be set for the Part.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - A PartOption function that sets the Part's encoding.
|
2024-02-27 11:21:28 +01:00
|
|
|
func WithPartEncoding(encoding Encoding) PartOption {
|
2022-03-14 10:29:53 +01:00
|
|
|
return func(p *Part) {
|
2024-02-27 11:21:28 +01:00
|
|
|
p.encoding = encoding
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-31 18:35:48 +01:00
|
|
|
|
2024-10-06 16:54:18 +02:00
|
|
|
// WithPartContentDescription overrides the default Part Content-Description.
|
|
|
|
//
|
|
|
|
// This function returns a PartOption that allows the Content-Description of a Part
|
|
|
|
// to be overridden with the specified description.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - description: The Content-Description to be set for the Part.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// - A PartOption function that sets the Part's Content-Description.
|
2024-02-27 11:21:28 +01:00
|
|
|
func WithPartContentDescription(description string) PartOption {
|
2023-01-31 18:35:48 +01:00
|
|
|
return func(p *Part) {
|
2024-02-27 11:21:28 +01:00
|
|
|
p.description = description
|
2023-01-31 18:35:48 +01:00
|
|
|
}
|
|
|
|
}
|