Send HTML content via SendGrid v3

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

Send HTML content via SendGrid v3

问题

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

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

我收到错误信息:

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

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

  1. "content": [
  2. {
  3. "type": "text/html",
  4. "value": "<html><head></head><body>Hello!</body></html>"
  5. }
  6. ],

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

英文:

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:

  1. &quot;content&quot;: [
  2. {
  3. &quot;type&quot;: &quot;text/html&quot;,
  4. &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;
  5. }
  6. ],

i get error:

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

But this code works correctly:

  1. &quot;content&quot;: [
  2. {
  3. &quot;type&quot;: &quot;text/html&quot;,
  4. &quot;value&quot;: &quot;&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;Hello!&lt;/body&gt;&lt;/html&gt;&quot;
  5. }
  6. ],

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

答案1

得分: 5

需要这样做:

  1. <div class="ad_box"><img class="banner" src="some_ad.png" alt="" />
  2. 示例
  3. "content": [
  4. {
  5. "type": "text/html",
  6. "value": "<html><head></head><body>Hello You link <a href=\"http://example.com/reschedule?id=12334\">Click</a></body></html>"
  7. }
  8. ],
英文:

Need do this:

  1. &lt;div class=\&quot;ad_box\&quot;&gt;&lt;img class=\&quot;banner\&quot; src=\&quot;some_ad.png\&quot; alt=\&quot;\&quot; \/&gt;
  2. Example
  3. &quot;content&quot;: [
  4. {
  5. &quot;type&quot;: &quot;text/html&quot;,
  6. &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;
  7. }
  8. ],

答案2

得分: 4

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

示例代码:

  1. from := mail.NewEmail("发件人", "from@mail.com")
  2. to := mail.NewEmail("收件人", "to@mail.com")
  3. content := mail.NewContent("text/html", contentHtml)
  4. email := mail.NewV3MailInit(from, "关于sendgrid客户端的示例", to, content)
  5. personalization := mail.NewPersonalization()
  6. personalization.AddTos(to)
  7. email.AddPersonalizations(personalization)
  8. client := sendgrid.NewSendClient(os.Getenv("SENDGRID_API_KEY"))
  9. response, err = client.Send(email)
  10. if err != nil {
  11. return fmt.Errorf("无法发送邮件:%v", err)
  12. }
  13. if response.StatusCode != 202 {
  14. return fmt.Errorf("无法发送邮件:%s", response.Body)
  15. }

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:

  1. from := mail.NewEmail(&quot;from&quot;, &quot;from@mail.com&quot;)
  2. to := mail.NewEmail(&quot;to&quot;, &quot;to@mail.com&quot;)
  3. content := mail.NewContent(&quot;text/html&quot;, contentHtml)
  4. email := mail.NewV3MailInit(from, &quot;Sample about sendgrid client&quot;, to, content)
  5. personalization := mail.NewPersonalization()
  6. personalization.AddTos(to)
  7. email.AddPersonalizations(personalization)
  8. client := sendgrid.NewSendClient(os.Getenv(&quot;SENDGRID_API_KEY&quot;))
  9. response, err = client.Send(email)
  10. if err != nil {
  11. return fmt.Errorf(&quot;Cannot send the email: %v&quot;, err)
  12. }
  13. if response.StatusCode != 202 {
  14. return fmt.Errorf(&quot;Cannot send the email: %s&quot;, response.Body)
  15. }

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:

确定