emersion / go-smtp

📤 An SMTP client & server library written in Go
MIT License
1.72k stars 216 forks source link

Unable to set headers when using SendMail with go templating. #238

Closed awalvie closed 11 months ago

awalvie commented 11 months ago

I'm using smtp.SendMail to send emails via gmail. The body of the email is being generated by a text go template. I'm running into an issue where the received email doesn't set the headers properly. How do I know the headers are not being set properly -- well because the received email says (no subject) Ref: image

Here's the code snippet I'm using:

// Execute the template
var emailBody bytes.Buffer
err = t.ExecuteTemplate(&emailBody, "email", nil)
if err != nil {
    log.Println("error executing template", err)
}

//Send email
err = smtp.SendMail(addr, auth, a.Config.Mail.From, a.Config.Mail.To, &emailBody)
if err != nil {
log.Println("error sending mail", err)
}

email template:

{{ define "email" }}
To: foo@bar.com
Subject: Woah

This is a test email sent via Go and net/smtp using templating.

Best regards,
Your Name
{{ end }}
emersion commented 11 months ago

Probably because there is an empty newline before "To". Note, well-formed emails use \r\n line endings instead of \n.

Anyways, not a go-smtp bug.

awalvie commented 11 months ago

Yup, that was it. Apologies about my own stupidity there, probably should have asked instead in the irc channel instead of creating an issue. Thank you for the insanely quick reply though. Really appreciate it.