Send HTML content via SendGrid v3

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

Send HTML content via SendGrid v3

问题

我使用GO语言,并尝试通过sendgrid API v3发送邮件(https://github.com/sendgrid/sendgrid-go,不使用Mail Helper类)。但是当我使用以下代码时:

"content": [
    {
      "type": "text/html", 
      "value": "<html><head></head><body>Hello You link <a href=\"http://example.com/reschedule?id=12334\">Click</a></body></html>"
    }
  ], 

我收到错误信息:

400 {"errors":[{"message":"Bad Request","field":null,"help":null}]}

但是以下代码可以正常工作:

"content": [
    {
      "type": "text/html", 
      "value": "<html><head></head><body>Hello!</body></html>"
    }
  ], 

我认为问题出在特殊字符上,但我该如何解决呢?谢谢!

英文:

I use GO and I try send mail via sendgrid API v3(https://github.com/sendgrid/sendgrid-go Without Mail Helper Class). But when i use this code:

&quot;content&quot;: [
    {
      &quot;type&quot;: &quot;text/html&quot;, 
      &quot;value&quot;: &quot;&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;Hello You link &lt;a href=&quot;http://example.com/reschedule?id=12334&quot;&gt;Click&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;&quot;
    }
  ], 

i get error:

> 400 {"errors":[{"message":"Bad Request","field":null,"help":null}]}

But this code works correctly:

&quot;content&quot;: [
    {
      &quot;type&quot;: &quot;text/html&quot;, 
      &quot;value&quot;: &quot;&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;Hello!&lt;/body&gt;&lt;/html&gt;&quot;
    }
  ], 

I think problem in special characters, but how i can fix it? Thank you!

答案1

得分: 5

需要这样做:

<div class="ad_box"><img class="banner" src="some_ad.png" alt="" />

示例
    "content": [
        {
          "type": "text/html", 
          "value": "<html><head></head><body>Hello You link <a href=\"http://example.com/reschedule?id=12334\">Click</a></body></html>"
        }
      ],
英文:

Need do this:

&lt;div class=\&quot;ad_box\&quot;&gt;&lt;img class=\&quot;banner\&quot; src=\&quot;some_ad.png\&quot; alt=\&quot;\&quot; \/&gt;

Example
    &quot;content&quot;: [
        {
          &quot;type&quot;: &quot;text/html&quot;, 
          &quot;value&quot;: &quot;&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;Hello You link &lt;a href=\&quot;http://example.com/reschedule?id=12334\&quot;&gt;Click&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;&quot;
        }
      ], 

答案2

得分: 4

我遇到了同样的问题,特殊字符也会转义,解决方法是使用官方客户端和其辅助工具。

示例代码:

from := mail.NewEmail("发件人", "from@mail.com")
to := mail.NewEmail("收件人", "to@mail.com")
content := mail.NewContent("text/html", contentHtml)

email := mail.NewV3MailInit(from, "关于sendgrid客户端的示例", to, content)

personalization := mail.NewPersonalization()
personalization.AddTos(to)
email.AddPersonalizations(personalization)

client := sendgrid.NewSendClient(os.Getenv("SENDGRID_API_KEY"))
response, err = client.Send(email)
if err != nil {
    return fmt.Errorf("无法发送邮件:%v", err)
}

if response.StatusCode != 202 {
    return fmt.Errorf("无法发送邮件:%s", response.Body)
}

sendgrid-go

helpers示例

英文:

I was having the same problem, same escapes special characters, what solved was to use the official client to go and its helpers.

Sample code:

from := mail.NewEmail(&quot;from&quot;, &quot;from@mail.com&quot;)
to := mail.NewEmail(&quot;to&quot;, &quot;to@mail.com&quot;)
content := mail.NewContent(&quot;text/html&quot;, contentHtml)

email := mail.NewV3MailInit(from, &quot;Sample about sendgrid client&quot;, to, content)

personalization := mail.NewPersonalization()
personalization.AddTos(to)
email.AddPersonalizations(personalization)

client := sendgrid.NewSendClient(os.Getenv(&quot;SENDGRID_API_KEY&quot;))
response, err = client.Send(email)
if err != nil {
    return fmt.Errorf(&quot;Cannot send the email: %v&quot;, err)
}

if response.StatusCode != 202 {
    return fmt.Errorf(&quot;Cannot send the email: %s&quot;, response.Body)
}

sendgrid-go

helpers sample

huangapple
  • 本文由 发表于 2017年2月16日 17:59:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/42270563.html
匿名

发表评论

匿名网友

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

确定