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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/textproto"
|
|
|
|
)
|
|
|
|
|
2024-10-05 11:42:21 +02:00
|
|
|
// FileOption is a function type used to modify properties of a File
|
2022-03-14 10:29:53 +01:00
|
|
|
type FileOption func(*File)
|
|
|
|
|
2024-10-05 11:42:21 +02:00
|
|
|
// File represents a file with properties like content type, description, encoding, headers, name, and
|
|
|
|
// writer function. This can either be an attachment or an embedded file for a Msg.
|
2022-03-14 10:29:53 +01:00
|
|
|
type File struct {
|
2023-01-31 18:35:48 +01:00
|
|
|
ContentType ContentType
|
|
|
|
Desc string
|
|
|
|
Enc Encoding
|
|
|
|
Header textproto.MIMEHeader
|
|
|
|
Name string
|
|
|
|
Writer func(w io.Writer) (int64, error)
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
|
|
|
|
2024-10-05 11:42:21 +02:00
|
|
|
// WithFileContentID sets the "Content-ID" header in the File's MIME headers to the specified id.
|
2024-06-28 13:49:21 +02:00
|
|
|
func WithFileContentID(id string) FileOption {
|
2024-06-28 11:54:06 +02:00
|
|
|
return func(f *File) {
|
|
|
|
f.Header.Set(HeaderContentID.String(), id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-05 11:42:21 +02:00
|
|
|
// WithFileName sets the name of a File to the provided value.
|
2024-02-27 11:21:28 +01:00
|
|
|
func WithFileName(name string) FileOption {
|
2022-03-14 10:29:53 +01:00
|
|
|
return func(f *File) {
|
2024-02-27 11:21:28 +01:00
|
|
|
f.Name = name
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-05 11:42:21 +02:00
|
|
|
// WithFileDescription sets an optional file description for the File. The description is used in the
|
|
|
|
// Content-Description header of the MIME output.
|
2024-02-27 11:21:28 +01:00
|
|
|
func WithFileDescription(description string) FileOption {
|
2023-01-31 18:35:48 +01:00
|
|
|
return func(f *File) {
|
2024-02-27 11:21:28 +01:00
|
|
|
f.Desc = description
|
2023-01-31 18:35:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-05 11:42:21 +02:00
|
|
|
// WithFileEncoding sets the encoding type for a file.
|
|
|
|
//
|
|
|
|
// By default one should always use Base64 encoding for attachments and embeds, but there might be exceptions in
|
|
|
|
// which this might come handy.
|
|
|
|
//
|
|
|
|
// Note: that quoted-printable must never be used for attachments or embeds. If EncodingQP is provided as encoding
|
|
|
|
// to this method, it will be automatically overwritten with EncodingB64.
|
2024-02-27 11:21:28 +01:00
|
|
|
func WithFileEncoding(encoding Encoding) FileOption {
|
2023-01-31 18:35:48 +01:00
|
|
|
return func(f *File) {
|
2024-02-27 11:21:28 +01:00
|
|
|
if encoding == EncodingQP {
|
2023-01-31 18:35:48 +01:00
|
|
|
return
|
|
|
|
}
|
2024-02-27 11:21:28 +01:00
|
|
|
f.Enc = encoding
|
2023-01-31 18:35:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFileContentType sets the content type of the File.
|
2024-10-05 11:42:21 +02:00
|
|
|
//
|
|
|
|
// By default we will try to guess the file type and its corresponding content type and fall back to
|
|
|
|
// application/octet-stream if the file type, if no matching type could be guessed. This FileOption can
|
|
|
|
// be used to override this type, in case a specific type is required.
|
2024-02-27 11:21:28 +01:00
|
|
|
func WithFileContentType(contentType ContentType) FileOption {
|
2023-01-31 18:35:48 +01:00
|
|
|
return func(f *File) {
|
2024-02-27 11:21:28 +01:00
|
|
|
f.ContentType = contentType
|
2023-01-31 18:35:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-05 11:42:21 +02:00
|
|
|
// setHeader sets the value of a given MIME header field for the File.
|
2024-02-27 11:21:28 +01:00
|
|
|
func (f *File) setHeader(header Header, value string) {
|
|
|
|
f.Header.Set(string(header), value)
|
2022-03-14 10:29:53 +01:00
|
|
|
}
|
|
|
|
|
2024-10-05 11:42:21 +02:00
|
|
|
// getHeader retrieves the value of the specified MIME header field. It returns the header value and a boolean
|
|
|
|
// indicating whether the header was present or not.
|
2024-02-27 11:21:28 +01:00
|
|
|
func (f *File) getHeader(header Header) (string, bool) {
|
|
|
|
v := f.Header.Get(string(header))
|
2022-03-14 10:29:53 +01:00
|
|
|
return v, v != ""
|
|
|
|
}
|