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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FileOption returns a function that can be used for grouping File options
|
|
|
|
type FileOption func(*File)
|
|
|
|
|
|
|
|
// File is an attachment or embedded file of the Msg
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// WithFileName sets the filename of the File
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 18:35:48 +01:00
|
|
|
// WithFileDescription sets an optional file description of the File that will be
|
|
|
|
// added as Content-Description part
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFileEncoding sets the encoding of the File. By default we should always use
|
|
|
|
// Base64 encoding but there might be exceptions, where this might come handy.
|
|
|
|
// Please note that quoted-printable should never be used for attachments/embeds. If this
|
|
|
|
// is provided as argument, the function will automatically override back to Base64
|
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.
|
|
|
|
// By default go-mail will try to guess the file type and its corresponding
|
|
|
|
// content type and fall back to application/octet-stream if the file type
|
|
|
|
// could not be guessed. In some cases, however, it might be needed to force
|
|
|
|
// this to a specific type. For such situations this override method can
|
|
|
|
// be used
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 10:29:53 +01:00
|
|
|
// setHeader sets header fields to a 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
|
|
|
}
|
|
|
|
|
2023-01-31 18:35:48 +01:00
|
|
|
// getHeader return header fields of a File
|
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 != ""
|
|
|
|
}
|