英文:
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:
"content": [
{
"type": "text/html",
"value": "<html><head></head><body>Hello You link <a href="http://example.com/reschedule?id=12334">Click</a></body></html>"
}
],
i get error:
> 400 {"errors":[{"message":"Bad Request","field":null,"help":null}]}
But this code works correctly:
"content": [
{
"type": "text/html",
"value": "<html><head></head><body>Hello!</body></html>"
}
],
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:
<div class=\"ad_box\"><img class=\"banner\" src=\"some_ad.png\" alt=\"\" \/>
Example
"content": [
{
"type": "text/html",
"value": "<html><head></head><body>Hello You link <a href=\"http://example.com/reschedule?id=12334\">Click</a></body></html>"
}
],
答案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)
}
英文:
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("from", "from@mail.com")
to := mail.NewEmail("to", "to@mail.com")
content := mail.NewContent("text/html", contentHtml)
email := mail.NewV3MailInit(from, "Sample about sendgrid client", 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("Cannot send the email: %v", err)
}
if response.StatusCode != 202 {
return fmt.Errorf("Cannot send the email: %s", response.Body)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论