mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-25 15:10:48 +01:00
- Stderr should be read after closing Stdin
- Fix reading of Stderr contents - Remove redundand calls to `Close`
This commit is contained in:
parent
db9358f8b8
commit
4fe503d142
1 changed files with 10 additions and 16 deletions
26
msg.go
26
msg.go
|
@ -514,17 +514,11 @@ func (m *Msg) WriteToSendmailWithContext(ctx context.Context, sp string, a ...st
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to set STDERR pipe: %w", err)
|
return fmt.Errorf("failed to set STDERR pipe: %w", err)
|
||||||
}
|
}
|
||||||
defer func() {
|
|
||||||
_ = se.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
si, err := ec.StdinPipe()
|
si, err := ec.StdinPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to set STDIN pipe: %w", err)
|
return fmt.Errorf("failed to set STDIN pipe: %w", err)
|
||||||
}
|
}
|
||||||
defer func() {
|
|
||||||
_ = si.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Start the execution and write to STDIN
|
// Start the execution and write to STDIN
|
||||||
if err := ec.Start(); err != nil {
|
if err := ec.Start(); err != nil {
|
||||||
|
@ -537,20 +531,20 @@ func (m *Msg) WriteToSendmailWithContext(ctx context.Context, sp string, a ...st
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the stderr pipe for possible errors
|
|
||||||
var serr []byte
|
|
||||||
en, err := se.Read(serr)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to read STDERR pipe: %w", err)
|
|
||||||
}
|
|
||||||
if en > 0 {
|
|
||||||
return fmt.Errorf("sendmail command failed: %s", serr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close STDIN and wait for completion or cancellation of the sendmail executable
|
// Close STDIN and wait for completion or cancellation of the sendmail executable
|
||||||
if err := si.Close(); err != nil {
|
if err := si.Close(); err != nil {
|
||||||
return fmt.Errorf("failed to close STDIN pipe: %w", err)
|
return fmt.Errorf("failed to close STDIN pipe: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read the stderr pipe for possible errors
|
||||||
|
serr, err := io.ReadAll(se)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read STDERR pipe: %w", err)
|
||||||
|
}
|
||||||
|
if len(serr) > 0 {
|
||||||
|
return fmt.Errorf("sendmail command failed: %s", string(serr))
|
||||||
|
}
|
||||||
|
|
||||||
if err := ec.Wait(); err != nil {
|
if err := ec.Wait(); err != nil {
|
||||||
return fmt.Errorf("sendmail command execution failed: %w", err)
|
return fmt.Errorf("sendmail command execution failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue