Open stefansaasen opened 4 months ago
Thanks for the detailed report @stefansaasen. I'll have a deeper look into it soon.
Sorry for the late response @stefansaasen. Again thanks for the detailed report, that's really valuable information. I agree that we should respect the RFC and the behaviour you described, yet changing this might break some already rolled solutions, so we need to be careful on how to implement this properly.
As quick "fix" I will update the example on the go-mail website and switch the calls so that they produce the expected output. As long term solution I think it's best to rewrite the logic in the msgwriter
code to always follow the correct order and produce results that follow the RFC. That way it's unlikely we break any existing code out there. I'll link the branch to this ticket once I've started work on it.
Thanks for your response. Sounds like a good plan!
Description
I believe the example on https://go-mail.dev/examples/bulk-mailer/ does not yield the expected result when it comes to alternative content (HTML vs. plain text).
Note: This might be entirely due to my expectations. But it might be beneficial to be explicit in the documentation.
The following snippet is used:
This sets HTML content and an alternative text content. The naming of the method suggests (at least to me) that the HTML content is the desired content and the text content an alternative (aka fallback content).
At least in Gmail (likely in other clients) the behavior is such, that the plain text content is shown and not the HTML content. This is likely due to the behavior outlined in https://www.rfc-editor.org/rfc/rfc2046#section-5.1.4
(emphasis mine)
The raw content of the email generated with the code above is:
I.e. the plain text part is the last part and thus chosen by Gmail as the content to show.
Given the naming of the methods, one might assume (at least I did) that the preferred content is the HTML content and the text content is the secondary or alternative content.
To Reproduce
Expected behaviour
The email uses the HTML content and only shows the plain text content if HTML cannot be displayed.
Screenshots
Actual email show by Gmail:
Desired email shown by Gmail:
Attempted Fixes
Swapping the order of calls causes this example to only send the HTML part:
This produces an email with the following content:
I.e. the text part is entirely missing.
Coincidentally, the following yields the desired result:
this leads to an email with the following structure:
I.e. it's an email with a
multipart/related
andmultipart/alternative
body with the HTML email content being last (and therefore chosen by Gmail).But this only works if I embed the image, removing the
msg.EmbedFromEmbedFS
call, leads to the same behavior describer above where the email content is just the HTML content.Setting the HTML as the alternative content
The following yields the desired result (with or without the embedded image):
Here the body is set to
text/plain
and the alternative totext/html
with the latter being chosen by Gmail.Ultimately this largely boils down to semantics and the meaning of
alternativeString
but to some degree also to the order of calling the methods. To me the desired behaviour would be for the body string to always come last for it to be chosen as the most appropriate format and for alternative strings to be added in order of the method execution. But simply calling out the behaviour in the documentation might be sufficient.Additional context
No response