Add comprehensive documentation comments

This commit is contained in:
Oliver Jakoubek 2025-11-18 18:36:48 +01:00
commit 9db82f92c5
6 changed files with 128 additions and 26 deletions

View file

@ -6,7 +6,9 @@ import (
"os"
)
// Message repräsentiert eine E-Mail-Nachricht
// Message represents an email message with all its components including recipients,
// content, headers, and attachments. Messages are constructed using the fluent builder
// pattern provided by the setter methods.
type Message struct {
To []string `json:"to"`
CC []string `json:"cc,omitempty"`
@ -19,20 +21,21 @@ type Message struct {
Attachments []Attachment `json:"attachments,omitempty"`
}
// Header repräsentiert einen benutzerdefinierten E-Mail-Header
// Header represents a custom email header as a name-value pair.
type Header struct {
Header string `json:"header"`
Value string `json:"value"`
}
// Attachment repräsentiert einen E-Mail-Anhang
// Attachment represents an email attachment with its filename, MIME type, and base64-encoded data.
type Attachment struct {
Filename string `json:"filename"`
Data string `json:"data"` // Base64-kodiert
Data string `json:"data"` // Base64-encoded file content
MimeType string `json:"mimetype"`
}
// NewMessage erstellt eine neue Message
// NewMessage creates and returns a new empty Message with initialized slices for recipients,
// headers, and attachments. Use the setter methods to populate the message fields.
func NewMessage() *Message {
return &Message{
To: []string{},
@ -43,49 +46,58 @@ func NewMessage() *Message {
}
}
// AddTo fügt einen Empfänger hinzu
// AddTo adds a recipient email address to the To field.
// Returns the message for method chaining.
func (m *Message) AddTo(email string) *Message {
m.To = append(m.To, email)
return m
}
// AddCC fügt einen CC-Empfänger hinzu
// AddCC adds a recipient email address to the CC (carbon copy) field.
// Returns the message for method chaining.
func (m *Message) AddCC(email string) *Message {
m.CC = append(m.CC, email)
return m
}
// AddBCC fügt einen BCC-Empfänger hinzu
// AddBCC adds a recipient email address to the BCC (blind carbon copy) field.
// Returns the message for method chaining.
func (m *Message) AddBCC(email string) *Message {
m.BCC = append(m.BCC, email)
return m
}
// SetSender setzt den Absender
// SetSender sets the sender email address for the message.
// Returns the message for method chaining.
func (m *Message) SetSender(email string) *Message {
m.Sender = email
return m
}
// SetSubject setzt den Betreff
// SetSubject sets the email subject line.
// Returns the message for method chaining.
func (m *Message) SetSubject(subject string) *Message {
m.Subject = subject
return m
}
// SetTextBody setzt den Text-Körper
// SetTextBody sets the plain text body of the email.
// Returns the message for method chaining.
func (m *Message) SetTextBody(body string) *Message {
m.TextBody = body
return m
}
// SetHTMLBody setzt den HTML-Körper
// SetHTMLBody sets the HTML body of the email.
// Returns the message for method chaining.
func (m *Message) SetHTMLBody(body string) *Message {
m.HTMLBody = body
return m
}
// AddHeader fügt einen benutzerdefinierten Header hinzu
// AddHeader adds a custom email header with the specified name and value.
// Common examples include "Reply-To", "X-Priority", or custom application headers.
// Returns the message for method chaining.
func (m *Message) AddHeader(name, value string) *Message {
m.Headers = append(m.Headers, Header{
Header: name,
@ -94,7 +106,9 @@ func (m *Message) AddHeader(name, value string) *Message {
return m
}
// AttachFile fügt eine Datei als Anhang hinzu
// AttachFile adds a file attachment to the message from a byte slice.
// The data is automatically base64-encoded for transmission.
// Returns the message for method chaining.
func (m *Message) AttachFile(filename, mimeType string, data []byte) *Message {
m.Attachments = append(m.Attachments, Attachment{
Filename: filename,
@ -104,7 +118,9 @@ func (m *Message) AttachFile(filename, mimeType string, data []byte) *Message {
return m
}
// AttachFileFromPath lädt eine Datei vom Dateisystem und fügt sie als Anhang hinzu
// AttachFileFromPath reads a file from the filesystem and adds it as an attachment.
// The filename is extracted from the path. Returns an error if the file cannot be read.
// The file data is automatically base64-encoded for transmission.
func (m *Message) AttachFileFromPath(path, mimeType string) error {
data, err := os.ReadFile(path)
if err != nil {
@ -126,7 +142,13 @@ func (m *Message) AttachFileFromPath(path, mimeType string) error {
return nil
}
// Validate prüft, ob die Message gültig ist
// Validate checks whether the message meets all required criteria for sending.
// It returns an error if any validation rules are violated:
// - At least one recipient is required
// - Maximum of 255 recipients allowed
// - Sender must be specified
// - Subject must be specified
// - Either TextBody or HTMLBody (or both) must be provided
func (m *Message) Validate() error {
if len(m.To) == 0 {
return errors.New("at least one recipient required")