go-gomail / gomail

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

Incorrect behavior using html/template with AddAlternativeWriter #176

Open BorisKoz opened 1 year ago

BorisKoz commented 1 year ago

There is an issue where if the template crashes, the gomail.Send(sender, msg) returns the error, therefore implying correct error handling, but the email is sent regardless of template execution. I.e.: the html contains:

a class="button-3" href="{{.Data.ConfirmationLink}}"> Confirm Email

where Data is a map. the template is configured for "missingkey=error", so if Data does not contain this key, the function tmp.execute(w, mail) will return an error. However, the email will still be sent:

Снимок экрана 2022-12-27 в 13 47 37

As seen, with the error log being:

{"level":"error","time":"2022-12-27T13:47:24+03:00","message":"Could not send email to \"xxx\": gomail: could not send email 1: template: emailConfirmation.html:110:53: executing \"emailConfirmation.html\" at <.Data.ConfirmationLink>: map has no entry for key \"ConfirmationLink\""}

I might be wrong in pointing this out, but in my opinion, if an error is found while executing the message template writer, the whole message should be discarded

BorisKoz commented 1 year ago

I should add, that executing the template beforehand is an option, and it certainly helps, but it means wasting memory and performance - you need an extra buffer for the mail and an extra execution time

wneessen commented 1 year ago

This module isn't maintained anymore. It might be worth looking at go-mail which is actively maintained and supports text.Template/html.Template out of the box. The syntax is very similar, so a switch should be faily simple.

BorisKoz commented 1 year ago

This module isn't maintained anymore. It might be worth looking at go-mail which is actively maintained and supports text.Template/html.Template out of the box. The syntax is very similar, so a switch should be faily simple.

Thank you! managed to switch in a blink of an eye. The only thing is - got some problems with SMTPAuthLogin there. maybe would open a request later, not a pressing issue - plain works!