go-gomail / gomail

The best way to send emails in Go.
MIT License
4.37k stars 577 forks source link

Multiple bodies #30

Closed EtienneBruines closed 9 years ago

EtienneBruines commented 9 years ago

Its often convenient to send both text/html and text/plain: pretty emails for those that support it, and basic emails for those that don't.

msg := gomail.NewMessage()

msg.SetBody("text/plain", "Hello!")
msg.SetBody("text/html", "<b>Hello!</b>")

However, the second SetBody, replaces the first.

EtienneBruines commented 9 years ago

I managed to fix this, by changing in gomail.go (lines 175+)

// SetBody sets the body of the message.
func (msg *Message) SetBody(contentType, body string) {
    msg.parts = []part{
        part{
            contentType: contentType,
            body:        bytes.NewBufferString(body),
        },
    }
}

to

// SetBody sets the body of the message.
func (msg *Message) SetBody(contentType, body string) {
    msg.parts = append(msg.parts, []part{
        part{
            contentType: contentType,
            body:        bytes.NewBufferString(body),
        },
    }...)
}

However, the name SetBody does not seem appropriate anymore. Perhaps this should be renamed to AddBody in this case (breaking change), or another function all together (SetBody with the original behavior, and AddBody with the new behavior. ) Or perhaps AppendBody instead of AddBody.

Any thoughts?

alexcesaro commented 9 years ago

AddAlternative is exactly what you are looking for.

EtienneBruines commented 9 years ago

Did not see that one! Thank you.