mirror of
https://github.com/wneessen/go-mail.git
synced 2024-12-22 18:50:37 +01:00
Refactor EML parsing into a single function
The previous separate parsing of EML headers and body parts has been refactored into a single function, parseEML. This change simplifies the operations in the readEML and makes the code cleaner by reducing repetition.
This commit is contained in:
parent
9fa8644a9a
commit
0cec8a28f7
1 changed files with 15 additions and 11 deletions
26
eml.go
26
eml.go
|
@ -39,11 +39,8 @@ func EMLToMsgFromReader(reader io.Reader) (*Msg, error) {
|
|||
return msg, fmt.Errorf("failed to parse EML from reader: %w", err)
|
||||
}
|
||||
|
||||
if err = parseEMLHeaders(&parsedMsg.Header, msg); err != nil {
|
||||
return msg, fmt.Errorf("failed to parse EML headers: %w", err)
|
||||
}
|
||||
if err = parseEMLBodyParts(parsedMsg, bodybuf, msg); err != nil {
|
||||
return msg, fmt.Errorf("failed to parse EML body parts: %w", err)
|
||||
if err := parseEML(parsedMsg, bodybuf, msg); err != nil {
|
||||
return msg, fmt.Errorf("failed to parse EML contents: %w", err)
|
||||
}
|
||||
|
||||
return msg, nil
|
||||
|
@ -64,17 +61,24 @@ func EMLToMsgFromFile(filePath string) (*Msg, error) {
|
|||
return msg, fmt.Errorf("failed to parse EML file: %w", err)
|
||||
}
|
||||
|
||||
if err = parseEMLHeaders(&parsedMsg.Header, msg); err != nil {
|
||||
return msg, fmt.Errorf("failed to parse EML headers: %w", err)
|
||||
if err := parseEML(parsedMsg, bodybuf, msg); err != nil {
|
||||
return msg, fmt.Errorf("failed to parse EML contents: %w", err)
|
||||
}
|
||||
if err = parseEMLBodyParts(parsedMsg, bodybuf, msg); err != nil {
|
||||
return msg, fmt.Errorf("failed to parse EML body parts: %w", err)
|
||||
}
|
||||
//fmt.Printf("FOO: %+v\n", msg)
|
||||
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
// parseEML parses the EML's headers and body and inserts the parsed values into the Msg
|
||||
func parseEML(parsedMsg *netmail.Message, bodybuf *bytes.Buffer, msg *Msg) error {
|
||||
if err := parseEMLHeaders(&parsedMsg.Header, msg); err != nil {
|
||||
return fmt.Errorf("failed to parse EML headers: %w", err)
|
||||
}
|
||||
if err := parseEMLBodyParts(parsedMsg, bodybuf, msg); err != nil {
|
||||
return fmt.Errorf("failed to parse EML body parts: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// readEML opens an EML file and uses net/mail to parse the header and body
|
||||
func readEML(filePath string) (*netmail.Message, *bytes.Buffer, error) {
|
||||
fileHandle, err := os.Open(filePath)
|
||||
|
|
Loading…
Reference in a new issue