通过 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 中转义这些字符?


临摹微笑
浏览 199回答 1
1回答

FFIVE

尝试使用mail.Address.String格式化地址:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "net/mail")func main() {&nbsp; &nbsp; a := mail.Address{Name: "Michael čisto", Address: "sender@example.com"}&nbsp; &nbsp; fmt.Println(a.String())&nbsp; &nbsp; // Output:&nbsp; &nbsp; // =?utf-8?q?Michael_=C4=8Disto?= <sender@example.com>}如果您的域包含非 ASCII 字符:请注意,Amazon SES 不支持 SMTPUTF8 扩展。如果地址的域部分(@ 符号之后的部分)包含非 ASCII 字符,则必须使用 Punycode 对其进行编码(请参阅types.BulkEmailEntry)。mail.Address.String不会为你做这个。但是您可以使用idna.ToASCII自己转换域。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go