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 ?
CodePudding user response:
Try to format the address using mail.Address.String:
package main
import (
"fmt"
"net/mail"
)
func main() {
a := mail.Address{Name: "Michael čisto", Address: "[email protected]"}
fmt.Println(a.String())
// Output:
// =?utf-8?q?Michael_=C4=8Disto?= <[email protected]>
}
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.