mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-09 23:42:55 +01:00
Protect against empty address list, refactor error handling
Added a condition to prevent assignment of an empty address list to the 'HeaderFrom' scenario. This eliminates potential runtime errors when trying to access an nonexistent element of a slice. Adjusted error handling in the sendmail execution part by reusing the 'err' variable, promoting cleaner, more readable code.
This commit is contained in:
parent
cdc8c37309
commit
468e1e2b2c
1 changed files with 6 additions and 4 deletions
8
msg.go
8
msg.go
|
@ -287,7 +287,9 @@ func (m *Msg) SetAddrHeader(h AddrHeader, v ...string) error {
|
||||||
}
|
}
|
||||||
switch h {
|
switch h {
|
||||||
case HeaderFrom:
|
case HeaderFrom:
|
||||||
|
if len(al) > 0 {
|
||||||
m.addrHeader[h] = []*mail.Address{al[0]}
|
m.addrHeader[h] = []*mail.Address{al[0]}
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
m.addrHeader[h] = al
|
m.addrHeader[h] = al
|
||||||
}
|
}
|
||||||
|
@ -1001,7 +1003,7 @@ func (m *Msg) WriteToSendmailWithContext(ctx context.Context, sp string, a ...st
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
return fmt.Errorf("could not start sendmail execution: %w", err)
|
return fmt.Errorf("could not start sendmail execution: %w", err)
|
||||||
}
|
}
|
||||||
_, err = m.WriteTo(si)
|
_, err = m.WriteTo(si)
|
||||||
|
@ -1012,7 +1014,7 @@ func (m *Msg) WriteToSendmailWithContext(ctx context.Context, sp string, a ...st
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1025,7 +1027,7 @@ func (m *Msg) WriteToSendmailWithContext(ctx context.Context, sp string, a ...st
|
||||||
return fmt.Errorf("sendmail command failed: %s", string(serr))
|
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