wneessen / go-mail

📧 Easy to use, yet comprehensive library for sending mails with Go
https://go-mail.dev
MIT License
571 stars 44 forks source link

Error from body writer is ignored #81

Closed oschwald closed 1 year ago

oschwald commented 1 year ago

Description

If you set a body writer with SetBodyWriter and the writer fails with an error, (*Msg).WriteTo will not return an error itself.

To Reproduce

Run:

package main

import (
    "errors"
    "fmt"
    "io"

    "github.com/wneessen/go-mail"
)

func main() {
    m := mail.NewMsg()

    m.SetBodyWriter(
        mail.TypeTextPlain,
        func(io.Writer) (int64, error) {
            return 0, errors.New("failed!")
        },
    )

    _, err := m.WriteTo(io.Discard)
    fmt.Println(err)
}

Expected behaviour

The above prints out <nil>. I would expect the body writer error to cause WriteTo to return an error, resulting in the program outputting failed! or a wrapped version of it. Right now, there is no indication that an error occurred.

Screenshots

No response

Attempted Fixes

No response

Additional context

No response

wneessen commented 1 year ago

Thanks for the report. That indeed doesn't sound right. I'll have a look what's going on there.

oschwald commented 1 year ago

Thanks for the quick fix on this.