英文:
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 '"'
这是我的代码:
package main
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sesv2"
"github.com/aws/aws-sdk-go-v2/service/sesv2/types"
)
func main() {
name := "Michael čisto"
body := "test email body"
subject := "test email"
CharSet := "UTF-8"
/* Assemble the email */
input := &sesv2.SendEmailInput{
Destination: &types.Destination{
CcAddresses: []string{},
ToAddresses: []string{receiverEmail},
},
Content: &types.EmailContent{
Simple: &types.Message{
Body: &types.Body{
Html: &types.Content{
Charset: aws.String(CharSet),
Data: aws.String(body),
},
},
Subject: &types.Content{
Charset: aws.String(CharSet),
Data: aws.String(subject),
},
},
},
ReplyToAddresses: []string{"\"" + name + "\" <" + senderEmail + ">"},
FromEmailAddress: aws.String("\"" + name + "\" <" + senderEmail + ">"),
}
cfg, err := awsconfig.LoadDefaultConfig(context.TODO(),
awsconfig.WithRegion("us-east-1"),
)
client := sesv2.NewFromConfig(cfg)
emailResp, err = client.SendEmail(context.TODO(), input)
}
有人可以帮我解决如何在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 '"'
Here is my code:
package main
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sesv2"
"github.com/aws/aws-sdk-go-v2/service/sesv2/types"
)
func main() {
name := "Michael čisto"
body := "test email body"
subject := "test email"
CharSet := "UTF-8"
/* Assemble the email */
input := &sesv2.SendEmailInput{
Destination: &types.Destination{
CcAddresses: []string{},
ToAddresses: []string{receiverEmail},
},
Content: &types.EmailContent{
Simple: &types.Message{
Body: &types.Body{
Html: &types.Content{
Charset: aws.String(CharSet),
Data: aws.String(body),
},
},
Subject: &types.Content{
Charset: aws.String(CharSet),
Data: aws.String(subject),
},
},
},
ReplyToAddresses: []string{"\"" + name + "\" <" + senderEmail + ">"},
FromEmailAddress: aws.String("\"" + name + "\" <" + senderEmail + ">"),
}
cfg, err := awsconfig.LoadDefaultConfig(context.TODO(),
awsconfig.WithRegion("us-east-1"),
)
client := sesv2.NewFromConfig(cfg)
emailResp, err = client.SendEmail(context.TODO(), input)
}
Can anyone help me to figure out how to escape these kind of characters in GO ?
答案1
得分: 2
尝试使用mail.Address.String来格式化地址:
package main
import (
"fmt"
"net/mail"
)
func main() {
a := mail.Address{Name: "Michael čisto", Address: "sender@example.com"}
fmt.Println(a.String())
// 输出:
// =?utf-8?q?Michael_=C4=8Disto?= <sender@example.com>
}
如果您的域名包含非ASCII字符:
请注意,Amazon SES不支持SMTPUTF8扩展。如果地址的域部分(@符号后面的部分)包含非ASCII字符,则必须使用Punycode进行编码(请参阅types.BulkEmailEntry)。mail.Address.String
不会为您执行此操作。但是,您可以使用idna.ToASCII自行转换域名。
英文:
Try to format the address using mail.Address.String:
package main
import (
"fmt"
"net/mail"
)
func main() {
a := mail.Address{Name: "Michael čisto", Address: "sender@example.com"}
fmt.Println(a.String())
// Output:
// =?utf-8?q?Michael_=C4=8Disto?= <sender@example.com>
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论