通过AWS SES发送电子邮件时,需要转义特殊字符。

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

Escape special characters while sending email through AWS ses

问题

你好,我是你的中文翻译助手。以下是你要翻译的内容:

你好,我正在使用AWS SES SDK V2发送电子邮件。我遇到了一个问题,发送者的姓名是Michael čisto。如果我直接将这个姓名传递给AWS的电子邮件输入结构体,就会出现以下错误:

operation error SESv2: SendEmail, https response error StatusCode: 400, BadRequestException: Missing '"'

这是我的代码:

  1. package main
  2. import (
  3. "context"
  4. "github.com/aws/aws-sdk-go-v2/aws"
  5. awsconfig "github.com/aws/aws-sdk-go-v2/config"
  6. "github.com/aws/aws-sdk-go-v2/service/sesv2"
  7. "github.com/aws/aws-sdk-go-v2/service/sesv2/types"
  8. )
  9. func main() {
  10. name := "Michael čisto"
  11. body := "test email body"
  12. subject := "test email"
  13. CharSet := "UTF-8"
  14. /* Assemble the email */
  15. input := &sesv2.SendEmailInput{
  16. Destination: &types.Destination{
  17. CcAddresses: []string{},
  18. ToAddresses: []string{receiverEmail},
  19. },
  20. Content: &types.EmailContent{
  21. Simple: &types.Message{
  22. Body: &types.Body{
  23. Html: &types.Content{
  24. Charset: aws.String(CharSet),
  25. Data: aws.String(body),
  26. },
  27. },
  28. Subject: &types.Content{
  29. Charset: aws.String(CharSet),
  30. Data: aws.String(subject),
  31. },
  32. },
  33. },
  34. ReplyToAddresses: []string{"\"" + name + "\" <" + senderEmail + ">"},
  35. FromEmailAddress: aws.String("\"" + name + "\" <" + senderEmail + ">"),
  36. }
  37. cfg, err := awsconfig.LoadDefaultConfig(context.TODO(),
  38. awsconfig.WithRegion("us-east-1"),
  39. )
  40. client := sesv2.NewFromConfig(cfg)
  41. emailResp, err = client.SendEmail(context.TODO(), input)
  42. }

有人可以帮我解决如何在GO中转义这些特殊字符的问题吗?

英文:

Hello I am using AWS SES SDK V2 to send emails. I have a case where sender name is like Michael čisto. If I pass this name directly to AWS email input struct then it gives me following error:

operation error SESv2: SendEmail, https response error StatusCode: 400, BadRequestException: Missing &#39;&quot;&#39;

Here is my code:

  1. package main
  2. import (
  3. &quot;context&quot;
  4. &quot;github.com/aws/aws-sdk-go-v2/aws&quot;
  5. awsconfig &quot;github.com/aws/aws-sdk-go-v2/config&quot;
  6. &quot;github.com/aws/aws-sdk-go-v2/service/sesv2&quot;
  7. &quot;github.com/aws/aws-sdk-go-v2/service/sesv2/types&quot;
  8. )
  9. func main() {
  10. name := &quot;Michael čisto&quot;
  11. body := &quot;test email body&quot;
  12. subject := &quot;test email&quot;
  13. CharSet := &quot;UTF-8&quot;
  14. /* Assemble the email */
  15. input := &amp;sesv2.SendEmailInput{
  16. Destination: &amp;types.Destination{
  17. CcAddresses: []string{},
  18. ToAddresses: []string{receiverEmail},
  19. },
  20. Content: &amp;types.EmailContent{
  21. Simple: &amp;types.Message{
  22. Body: &amp;types.Body{
  23. Html: &amp;types.Content{
  24. Charset: aws.String(CharSet),
  25. Data: aws.String(body),
  26. },
  27. },
  28. Subject: &amp;types.Content{
  29. Charset: aws.String(CharSet),
  30. Data: aws.String(subject),
  31. },
  32. },
  33. },
  34. ReplyToAddresses: []string{&quot;\&quot;&quot; + name + &quot;\&quot; &lt;&quot; + senderEmail + &quot;&gt;&quot;},
  35. FromEmailAddress: aws.String(&quot;\&quot;&quot; + name + &quot;\&quot; &lt;&quot; + senderEmail + &quot;&gt;&quot;),
  36. }
  37. cfg, err := awsconfig.LoadDefaultConfig(context.TODO(),
  38. awsconfig.WithRegion(&quot;us-east-1&quot;),
  39. )
  40. client := sesv2.NewFromConfig(cfg)
  41. emailResp, err = client.SendEmail(context.TODO(), input)
  42. }

Can anyone help me to figure out how to escape these kind of characters in GO ?

答案1

得分: 2

尝试使用mail.Address.String来格式化地址:

  1. package main
  2. import (
  3. "fmt"
  4. "net/mail"
  5. )
  6. func main() {
  7. a := mail.Address{Name: "Michael čisto", Address: "sender@example.com"}
  8. fmt.Println(a.String())
  9. // 输出:
  10. // =?utf-8?q?Michael_=C4=8Disto?= <sender@example.com>
  11. }

如果您的域名包含非ASCII字符

请注意,Amazon SES不支持SMTPUTF8扩展。如果地址的域部分(@符号后面的部分)包含非ASCII字符,则必须使用Punycode进行编码(请参阅types.BulkEmailEntry)。mail.Address.String不会为您执行此操作。但是,您可以使用idna.ToASCII自行转换域名。

英文:

Try to format the address using mail.Address.String:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;net/mail&quot;
  5. )
  6. func main() {
  7. a := mail.Address{Name: &quot;Michael čisto&quot;, Address: &quot;sender@example.com&quot;}
  8. fmt.Println(a.String())
  9. // Output:
  10. // =?utf-8?q?Michael_=C4=8Disto?= &lt;sender@example.com&gt;
  11. }

In case your domain contains non-ASCII characters:

Please note that Amazon SES does not support the SMTPUTF8 extension. If the domain part of an address (the part after the @ sign) contains non-ASCII characters, they must be encoded using Punycode (see types.BulkEmailEntry). mail.Address.String does not do this for you. But you can use idna.ToASCII to convert the domain yourself.

huangapple
  • 本文由 发表于 2022年11月14日 12:07:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/74426826.html
匿名

发表评论

匿名网友

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

确定