go-gomail / gomail

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

AddAlternative bug #48

Closed pjebs closed 8 years ago

pjebs commented 8 years ago

` message.SetBody("text/plain", "Hello pj")

message.AddAlternative("text/html", "Hello html <b>pj</b>")

` This produces:

`

--908b4cfdd408aaeb4ad5a5525eed978aa6361f8e322bf69397de36fa21d6 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Hello pj --908b4cfdd408aaeb4ad5a5525eed978aa6361f8e322bf69397de36fa21d6 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Hello html pj --908b4cfdd408aaeb4ad5a5525eed978aa6361f8e322bf69397de36fa21d6-- `

Hotmail shows the html version. Gmail shows the html version.

pjebs commented 8 years ago

` message.SetBody("text/html", "Hello html pj")

message.AddAlternative("text/plain", "Hello pj")

` This produces:

`

--94a45f8d9e1210c11d7ca9a070acc3618c1f827c3c928506d7a6e5a4064d Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Hello html pj --94a45f8d9e1210c11d7ca9a070acc3618c1f827c3c928506d7a6e5a4064d Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Hello pj --94a45f8d9e1210c11d7ca9a070acc3618c1f827c3c928506d7a6e5a4064d-- `

Hotmail shows the html version. Gmail shows the text/plain version.

pjebs commented 8 years ago

According to the official standard: http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

In general, user agents that compose multipart/alternative entities should place the body parts in increasing order of preference, that is, with the preferred format last.

Gmail respects the order (i.e. bottom is preferred). Your code should make the SetBody string go to the bottom. The AddAlternative string should go to the top.

Currently you have it the wrong way around.

I presume the reason why hotmail always shows the html version is due to a user setting or their default setting is always show html if available.

alexcesaro commented 8 years ago

It is working as intended. The parts of the message are added in the order defined by the user. Reversing the order would be confusing.

pjebs commented 8 years ago

What is so confusing? SetBody should set your preferred message. AddAlternative() should set your backup message if SetBody can't be used by the email client.

This means SetBody should be at the bottom. AddAlternative should be at the top. Simple!

What you have implemented is actually more confusing. My interpretation of what should happen is directly based on the method names. Your documentation does not even state that it is based on which order the developer adds it.

Either way, Excellent work!

pjebs commented 8 years ago

In fact, when I swapped them around as you suggested:

message.AddAlternative("text/plain", "Hello pj") message.SetBody("text/html", "Hello html <b>pj</b>")

The AddAlternative does not even work! hence is not even added.

alexcesaro commented 8 years ago

You should follow the example: https://godoc.org/gopkg.in/gomail.v2#example-Message-AddAlternative

I'll make the doc clearer.

pedromorgan commented 8 years ago

@alexcesaro you saying that the "order" the items are added, affects behaviour ??

pjebs commented 8 years ago

@pedromorgan That's what he is saying. I just tested it. Although I consider it wrong, the work around is super easy.

Just make sure m.AddAlternative is added last and it contains your PREFERRED email content.

pedromorgan commented 8 years ago

Sounds "wrong" to me also, and a future annoying "gotcha"!

alexcesaro commented 8 years ago

I added some details to the documentation. Is it clear enough now?

Also please note that an email can have more than two alternative parts. One plain text part followed by an HTML part is a common use case but it is not the only one.

pedromorgan commented 8 years ago

umm, its a bug afaik.. sorry..

alexcesaro commented 8 years ago

I am not sure I understand which bug you are talking about. Could you provide some details?