Search code examples
htmlgohref

How to attach a link in the setBody of gomail?


In this code, I am trying to make the value[2] as an href link so that if somebody clicks on the token, it will be redirected to some page but I am not sure how to do it. I searched for the solution but didn't find it.

func SendEmail(value [3]string) {
    mail := gomail.NewMessage()
    myEmail := middleware.LoadEnvVariable("EMAIL")
    myPassword := middleware.LoadEnvVariable("APP_PASSWORD")
    mail.SetHeader("From", myEmail)
    mail.SetHeader("To", value[0])
    mail.SetHeader("Reply-To", myEmail)
    mail.SetHeader("Subject", value[1])
    mail.SetBody("text/html", "<href='localhost:4000'>"+value[2]+"</href>")

    a := gomail.NewDialer("smtp.gmail.com", 587, myEmail, myPassword)
    if err := a.DialAndSend(mail); err != nil {
        log.Fatal(err)
    }
}

Solution

  • Following on the gomail docs, try to use the <a> tag:

    mail.SetBody("text/html", "<a href=\"" + value[2] + "\">some text</a>")