Friday, April 25, 2025
HomeGolangGolang e-mail not correct format on the physique with template - Getting...

Golang e-mail not correct format on the physique with template – Getting Assist


Hello All,

Received this code to send-email with template in Go, in order that sooner or later it’s straightforward to simply modify the template if wanted extra variables and presentable for others to see.

bundle fundamental

import (
	"bytes"
	"strings"
	"textual content/template"
	"internet/smtp"
	"log"
)

sort Email_struct struct {
	Standing      string
	App         string
	Undertaking     string
	TargetEnv   string
	AppVersion  string
	Url string
}

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

	var tmplFile = "e-mail.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 e-mail.
  	errs := smtp.SendMail("mail.area.com:25", nil, from, to, message) 
  	if errs != nil {
		log.Println(errs)
		return false
  	}
	return true
}

func fundamental() {
	// nl := "r"

	em := Email_struct{}
	em.Standing = "Synced"
	app := "pipeline-gitops-notification-sit"
	em.App = app
	// em.Undertaking = app + nl
	em.Undertaking = app
	GitopsFolder := "gitops/sit"
	extractTargetEnv := strings.Cut up(GitopsFolder, "https://discussion board.golangbridge.org/") 
	// em.TargetEnv = extractTargetEnv[1] + nl
	em.TargetEnv = extractTargetEnv[1] 
	mes := "Manifest-63db30e: qa-0.0.110 by consumer@area.com"
	extractVersion1 := strings.Cut up(mes, "-")
	extractVersion2 := strings.Cut up(extractVersion1[2], ".")
	extractVersion3 := strings.Cut up(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("consumer@area.com", em) 
	if success {
		log.Println("Electronic mail SUCCESS to despatched!")
	} else {
		log.Println("Electronic mail FAILED to ship!")
	}

}

With a template file e-mail.tmpl,

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

Undertaking: {{ .Undertaking }}
Goal : {{ .TargetEnv }}
Model: {{ .AppVersion }}
URL: {{ .Url }}

Finest regards,
Devops Crew

The specified e-mail physique must be (no concern with topic),

Undertaking: PPPPPPPPPPPPPPPPP
Goal : TTTTTTTTTTTTTTTTT
Model: VVVVVVVVVVVVVVVVV
URL: UUUUUUUUUUUUUUUUUUUU

Finest regards,
Devops Crew

But it surely’s going to a straight line,

Undertaking: PPPPPPPPPPPPPPPPP Goal : TTTTTTTTTTTTTTTTT Model: VVVVVVVVVVVVVVVVV URL: UUUUUUUUUUUUUUUUUUUU

Finest regards,
Devops Crew

If I allow nl := “r” or nl := “n” and even with each. The output could be,

Undertaking: PPPPPPPPPPPPPPPPP

Goal : TTTTTTTTTTTTTTTTT

Model: VVVVVVVVVVVVVVVVV

URL: UUUUUUUUUUUUUUUUUUUU

Finest regards,
Devops Crew

This isn’t presentable.

Please assist.

Thanks,
Ric

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments