Skip to content

Commit

Permalink
Merge pull request #7 from inliquid/main
Browse files Browse the repository at this point in the history
Fix sendmail invocation
  • Loading branch information
wneessen authored May 27, 2022
2 parents db9358f + 4fe503d commit eed1eee
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,17 +514,11 @@ func (m *Msg) WriteToSendmailWithContext(ctx context.Context, sp string, a ...st
if err != nil {
return fmt.Errorf("failed to set STDERR pipe: %w", err)
}
defer func() {
_ = se.Close()
}()

si, err := ec.StdinPipe()
if err != nil {
return fmt.Errorf("failed to set STDIN pipe: %w", err)
}
defer func() {
_ = si.Close()
}()

// Start the execution and write to STDIN
if err := ec.Start(); err != nil {
Expand All @@ -537,20 +531,20 @@ func (m *Msg) WriteToSendmailWithContext(ctx context.Context, sp string, a ...st
}
}

// Close STDIN and wait for completion or cancellation of the sendmail executable
if err := si.Close(); err != nil {
return fmt.Errorf("failed to close STDIN pipe: %w", err)
}

// Read the stderr pipe for possible errors
var serr []byte
en, err := se.Read(serr)
serr, err := io.ReadAll(se)
if err != nil {
return fmt.Errorf("failed to read STDERR pipe: %w", err)
}
if en > 0 {
return fmt.Errorf("sendmail command failed: %s", serr)
if len(serr) > 0 {
return fmt.Errorf("sendmail command failed: %s", string(serr))
}

// Close STDIN and wait for completion or cancellation of the sendmail executable
if err := si.Close(); err != nil {
return fmt.Errorf("failed to close STDIN pipe: %w", err)
}
if err := ec.Wait(); err != nil {
return fmt.Errorf("sendmail command execution failed: %w", err)
}
Expand Down

0 comments on commit eed1eee

Please sign in to comment.