go-mail/msg_totmpfile.go
Winni Neessen 0b105048e6
Refine WriteToTempFile docstring
Clarify the documentation for WriteToTempFile to better explain its functionality, ensure consistency, and detail its return values.
2024-10-06 16:21:37 +02:00

30 lines
806 B
Go

// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors
//
// SPDX-License-Identifier: MIT
//go:build go1.17
// +build go1.17
package mail
import (
"fmt"
"os"
)
// WriteToTempFile creates a temporary file and writes the Msg content to this file.
//
// This method generates a temporary file with a ".eml" extension, writes the Msg to it, and returns the
// filename of the created temporary file.
//
// Returns:
// - A string representing the filename of the temporary file.
// - An error if the file creation or writing process fails.
func (m *Msg) WriteToTempFile() (string, error) {
f, err := os.CreateTemp("", "go-mail_*.eml")
if err != nil {
return "", fmt.Errorf("failed to create output file: %w", err)
}
defer func() { _ = f.Close() }()
return f.Name(), m.WriteToFile(f.Name())
}