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)
|
|
|
|
|
|
|
|
// Part is a part of the Msg
|
|
|
|
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)
|
2024-09-26 11:37:53 +02:00
|
|
|
smime bool
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
|
|
|
|
2022-10-11 17:05:44 +02:00
|
|
|
// GetContent executes the WriteFunc of the Part and returns the content as byte slice
|
|
|
|
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-02-05 11:41:10 +01:00
|
|
|
// GetCharset returns the currently set Charset of the Part
|
|
|
|
func (p *Part) GetCharset() Charset {
|
2024-02-27 11:21:28 +01:00
|
|
|
return p.charset
|
2024-02-05 11:41:10 +01:00
|
|
|
}
|
|
|
|
|
2022-10-11 17:05:44 +02:00
|
|
|
// GetContentType returns the currently set ContentType of the Part
|
|
|
|
func (p *Part) GetContentType() ContentType {
|
2024-02-27 11:21:28 +01:00
|
|
|
return p.contentType
|
2022-10-11 17:05:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetEncoding returns the currently set Encoding of the Part
|
|
|
|
func (p *Part) GetEncoding() Encoding {
|
2024-02-27 11:21:28 +01:00
|
|
|
return p.encoding
|
2022-10-11 17:05:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetWriteFunc returns the currently set WriterFunc of the Part
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-31 18:35:48 +01:00
|
|
|
// GetDescription returns the currently set Content-Description of the Part
|
|
|
|
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-09-26 11:37:53 +02:00
|
|
|
// IsSMimeSigned returns true if the Part should be singed with S/MIME
|
|
|
|
func (p *Part) IsSMimeSigned() bool {
|
|
|
|
return p.smime
|
|
|
|
}
|
|
|
|
|
2022-10-11 19:27:54 +02:00
|
|
|
// SetContent overrides the content of the Part with the given string
|
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
|
|
|
}
|
|
|
|
|
2022-10-11 17:05:44 +02:00
|
|
|
// SetContentType overrides the ContentType of 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-01-31 16:28:41 +01:00
|
|
|
// SetCharset overrides the Charset of 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
|
|
|
}
|
|
|
|
|
2022-03-14 10:29:53 +01:00
|
|
|
// SetEncoding creates a new mime.WordEncoder based on the encoding setting of the message
|
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
|
|
|
}
|
|
|
|
|
2023-01-31 18:35:48 +01:00
|
|
|
// SetDescription overrides the Content-Description of 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-09-26 11:37:53 +02:00
|
|
|
// SetIsSMimeSigned sets the flag for signing the Part with S/MIME
|
|
|
|
func (p *Part) SetIsSMimeSigned(smime bool) {
|
|
|
|
p.smime = smime
|
|
|
|
}
|
|
|
|
|
2022-10-11 17:05:44 +02:00
|
|
|
// SetWriteFunc overrides the WriteFunc of the Part
|
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
|
|
|
}
|
|
|
|
|
2023-01-28 14:39:14 +01:00
|
|
|
// Delete removes the current part from the parts list of the Msg by setting the
|
2024-02-27 11:21:28 +01:00
|
|
|
// isDeleted flag to true. The msgWriter will skip it then
|
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-01-31 16:28:41 +01:00
|
|
|
// WithPartCharset overrides the default Part 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 10:29:53 +01:00
|
|
|
// WithPartEncoding overrides the default Part 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
|
|
|
|
|
|
|
// WithPartContentDescription overrides the default Part 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
|
|
|
}
|
|
|
|
}
|
2024-09-26 11:37:53 +02:00
|
|
|
|
|
|
|
// WithSMimeSinging overrides the flag for signing the Part with S/MIME
|
|
|
|
func WithSMimeSinging() PartOption {
|
|
|
|
return func(p *Part) {
|
|
|
|
p.smime = true
|
|
|
|
}
|
|
|
|
}
|