Golang email not proper format on the body with template

Hi All,

Got this code to send-email with template in Go, so that in the future it’s easy to just modify the template if needed more variables and presentable for others to see.

package main

import (
	"bytes"
	"strings"
	"text/template"
	"net/smtp"
	"log"
)

type Email_struct struct {
	Status      string
	App         string
	Project     string
	TargetEnv   string
	AppVersion  string
	Url string
}

func SendmaiL(emailTo string, e Email_struct) bool {	
	from := "noreply@domain.com"
	to := []string{
		emailTo,
	}

	var tmplFile = "email.tmpl"
	tmpl, err := template.New(tmplFile).ParseFiles(tmplFile)
	if err != nil {
		panic(err)
	}
	var tpl bytes.Buffer
	if err := tmpl.Execute(&tpl, e); err != nil {
		panic(err)
	}
	message := tpl.Bytes()
	log.Print(string(message[:])) // print

	// Sending email.
  	errs := smtp.SendMail("mail.domain.com:25", nil, from, to, message) 
  	if errs != nil {
		log.Println(errs)
		return false
  	}
	return true
}

func main() {
	// nl := "\r"

	em := Email_struct{}
	em.Status = "Synced"
	app := "pipeline-gitops-notification-sit"
	em.App = app
	// em.Project = app + nl
	em.Project = app
	GitopsFolder := "gitops/sit"
	extractTargetEnv := strings.Split(GitopsFolder, "/") 
	// em.TargetEnv = extractTargetEnv[1] + nl
	em.TargetEnv = extractTargetEnv[1] 
	mes := "Manifest-63db30e: qa-0.0.110 by user@domain.com"
	extractVersion1 := strings.Split(mes, "-")
	extractVersion2 := strings.Split(extractVersion1[2], ".")
	extractVersion3 := strings.Split(extractVersion2[2], " ")
	// em.AppVersion = extractVersion2[0]+"."+extractVersion2[1]+"."+extractVersion3[0]  + nl
	em.AppVersion = extractVersion2[0]+"."+extractVersion2[1]+"."+extractVersion3[0] 
	RepoUrl := "https://cd.toronto.ca:6443/devops/pipeline/gitops-notification.git"
	em.Url = RepoUrl

	success := SendmaiL("user@domain.com", em) 
	if success {
		log.Println("Email SUCCESS to sent!")
	} else {
		log.Println("Email FAILED to send!")
	}

}

With a template file email.tmpl,

Subject: [{{ .Status }}] app {{ .App }} has been synced by argoCD

Project: {{ .Project }}
Target : {{ .TargetEnv }}
Version: {{ .AppVersion }}
URL: {{ .Url }}

Best regards,
Devops Team

The desired email body should be (no issue with subject),

Project: PPPPPPPPPPPPPPPPP
Target : TTTTTTTTTTTTTTTTT
Version: VVVVVVVVVVVVVVVVV
URL: UUUUUUUUUUUUUUUUUUUU

Best regards,
Devops Team

But it’s going to a straight line,

Project: PPPPPPPPPPPPPPPPP Target : TTTTTTTTTTTTTTTTT Version: VVVVVVVVVVVVVVVVV URL: UUUUUUUUUUUUUUUUUUUU

Best regards,
Devops Team

If I enable nl := “\r” or nl := “\n” or even with both. The output would be,

Project: PPPPPPPPPPPPPPPPP

Target : TTTTTTTTTTTTTTTTT

Version: VVVVVVVVVVVVVVVVV

URL: UUUUUUUUUUUUUUUUUUUU

Best regards,
Devops Team

This is not presentable.

Please help.

Thanks,
Ric

Actually that’s working fine, just on my actual the Target is “Target Env” but when I put it as “Target-Env” then it works like fine.

Ensure your template uses the appropriate newline character for the intended email client. You can use string functions like strings.ReplaceAll("\n", "\r\n", body) to convert newlines to the desired format before including them in the template.