How to attach a link in the setBody of gomail?

huangapple go评论88阅读模式
英文:

How to attach a link in the setBody of gomail?

问题

在这段代码中,我正在尝试将value[2]作为href链接,这样如果有人点击该标记,就会被重定向到某个页面,但我不确定如何实现。我搜索了解决方案,但没有找到。

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)
    }
}
英文:

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(&quot;EMAIL&quot;)
	myPassword := middleware.LoadEnvVariable(&quot;APP_PASSWORD&quot;)
	mail.SetHeader(&quot;From&quot;, myEmail)
	mail.SetHeader(&quot;To&quot;, value[0])
	mail.SetHeader(&quot;Reply-To&quot;, myEmail)
	mail.SetHeader(&quot;Subject&quot;, value[1])
	mail.SetBody(&quot;text/html&quot;, &quot;&lt;href=&#39;localhost:4000&#39;&gt;&quot;+value[2]+&quot;&lt;/href&gt;&quot;)

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

答案1

得分: 1

根据gomail文档,尝试使用<a>标签:

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

Following on the gomail docs, try to use the &lt;a&gt; tag:

mail.SetBody(&quot;text/html&quot;, &quot;&lt;a href=\&quot;&quot; + value[2] + &quot;\&quot;&gt;some text&lt;/a&gt;&quot;)

huangapple
  • 本文由 发表于 2022年1月26日 15:21:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/70859780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定