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

create a copy of the data for Reset #124

Closed lublak closed 1 year ago

lublak commented 1 year ago

NewMsg can have a MsgOption array it can change addrHeader, attachments, etc. Reset should go back to the state of NewMsg

wneessen commented 1 year ago

Hi @lublak, thanks for the PR. I am not sure I understand the intention of this PR, though. What problem are you trying to solve with this? The Reset() method does not touch any of the things that can be controlled via the provided MsgOption set. As the decription of the Reset() method states:

// Reset resets all headers, body parts and attachments/embeds of the Msg
// It leaves already set encodings, charsets, boundaries, etc. as is

What problem did you run into with this method? Maybe we can find a better way to solve this.

lublak commented 1 year ago

@wneessen of course you can change this options with MsgOption. Example:

    if err != nil {
        return err
    }
    baseMessage = mail.NewMsg(func(m *mail.Msg) {
        if err = m.From(from); err != nil {
            return
        }
        if err = m.To(to...); err != nil {
            return
        }
        if err = m.Cc(cc...); err != nil {
            return
        }
        if err = m.Bcc(bcc...); err != nil {
            return
        }
    })

    if err != nil {
        return err
    }

But these would be reseted via Reset()

wneessen commented 1 year ago

@wneessen of course you can change this options with MsgOption. Example:

  baseMessage = mail.NewMsg(func(m *mail.Msg) {
      if err = m.From(from); err != nil {
          return
      }
      if err = m.To(to...); err != nil {
[...]

But these would be reseted via Reset()

Yes, that is expected behaviour. What you are trying to do is a pretty edge-case that I would not expect as "good practice". If you want some kind of "base message" I suggest to create a method for this instead that either returns a new msg with those fields field or that takes a msg pointer and uses the Set* methods to set these "defaults" for your messages. Reset is expected to do what it's name says, reset the message, which needs to include fields like "From" or "To". I don't consider this a good practice we should force as default in the library.

We could think about adding some "base message" feature, but this should be well thought out so it can be universally be used.

lublak commented 1 year ago

@wneessen It would not be quite as important. Currently I also use a method that generates the message again each time. Just thought it would be handy to have a BaseMessage function. You are also welcome to simply ignore it.