Go语言 通过AWS ses发送电子邮件时转义特殊字符

j1dl9f46  于 2022-12-07  发布在  Go
关注(0)|答案(1)|浏览(276)

您好,我正在使用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)
}

有谁能帮我弄清楚如何在围棋中逃脱这些角色吗?

kgsdhlau

kgsdhlau1#

尝试使用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>
}

如果您的域包含非ASCII字符

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

相关问题