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

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

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 &#39;&quot;&#39;

Here is my code:

package main

import (
	&quot;context&quot;

	&quot;github.com/aws/aws-sdk-go-v2/aws&quot;
	awsconfig &quot;github.com/aws/aws-sdk-go-v2/config&quot;
	&quot;github.com/aws/aws-sdk-go-v2/service/sesv2&quot;
	&quot;github.com/aws/aws-sdk-go-v2/service/sesv2/types&quot;
)

func main() {
	name := &quot;Michael čisto&quot;
	body := &quot;test email body&quot;
	subject := &quot;test email&quot;
	CharSet := &quot;UTF-8&quot;
	/* Assemble the email */
	input := &amp;sesv2.SendEmailInput{
		Destination: &amp;types.Destination{
			CcAddresses: []string{},
			ToAddresses: []string{receiverEmail},
		},
		Content: &amp;types.EmailContent{
			Simple: &amp;types.Message{
				Body: &amp;types.Body{
					Html: &amp;types.Content{
						Charset: aws.String(CharSet),
						Data:    aws.String(body),
					},
				},
				Subject: &amp;types.Content{
					Charset: aws.String(CharSet),
					Data:    aws.String(subject),
				},
			},
		},
		ReplyToAddresses: []string{&quot;\&quot;&quot; + name + &quot;\&quot; &lt;&quot; + senderEmail + &quot;&gt;&quot;},
		FromEmailAddress: aws.String(&quot;\&quot;&quot; + name + &quot;\&quot; &lt;&quot; + senderEmail + &quot;&gt;&quot;),
	}
	cfg, err := awsconfig.LoadDefaultConfig(context.TODO(),
		awsconfig.WithRegion(&quot;us-east-1&quot;),
	)
	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 (
	&quot;fmt&quot;
	&quot;net/mail&quot;
)

func main() {
	a := mail.Address{Name: &quot;Michael čisto&quot;, Address: &quot;sender@example.com&quot;}
	fmt.Println(a.String())
	// Output:
	// =?utf-8?q?Michael_=C4=8Disto?= &lt;sender@example.com&gt;
}

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:

确定