使用Amazon SES PHP SDK v3的SendEmail能否设置含特殊字符的发件人名称?
使用Amazon SES PHP SDK v3的SendEmail方法设置含非标准字符的发件人名称问题
问题描述
使用Amazon SES的SendEmail方法(PHP SDK v3)时,尝试设置包含西里尔字母、带变音符号的拉丁字母等非标准字符的友好发件人名称,发件人名称显示异常(非标准字符被省略或乱码)。尝试将Source参数改为带Charset的数组,但因SendEmail文档规定Source必须为字符串而无效。请问是否可以在SendEmail中使用UTF-8字符作为发件人名称,还是必须改用SendRawEmail?
原代码
<?php require "aws-autoloader.php"; use Aws\Credentials\CredentialProvider; use Aws\Ses\SesClient; use Aws\Exception\AwsException; $profile = "<myprofile>"; $AWS_PUBLIC = "<mypublic>"; $AWS_SECRET = "<mysecret>"; $path = "myconfig.env"; $region = "<myregion>"; $provider = CredentialProvider::ini($profile, $path); $provider = CredentialProvider::memoize($provider); $SesClient = new SesClient([ /*"profile" => "default",*/ "version" => "2010-12-01", "region" => $region, "credentials" => [ "key" => $AWS_PUBLIC, "secret" => $AWS_SECRET, ] ]); $from = "info@example.com"; $toArr = ["alice@example.com","bob@example.com"]; $reply = "charlie@example.com"; $subject = "Amazon test"; $body = "This is a new message :)<br><br><strong>Lorem ipsum etc</strong><hr><p>below the line</p>"; $bcc = "zed@example.com"; $fromName = "ĐŠĆČŽ-ЖЂШЋЧ usual alphabet"; // 收件人邮箱中此名称显示异常,UTF-8字符被省略或乱码 $plainBody = "This is a new message :)" . PHP_EOL . PHP_EOL . "Lorem ipsum etc" . PHP_EOL . "-----" . PHP_EOL . "below the line"; $charset = "UTF-8"; try { $sendingArray = [ "Destination" => [ // 假设已对所有邮箱地址进行验证 "ToAddresses" => $toArr ], // 假设已对所有邮箱地址进行验证 "ReplyToAddresses" => [$reply], "Source" => "$fromName <$from>", "Message" => [ "Body" => [ "Html" => [ "Charset" => $charset, "Data" => $body, ], "Text" => [ "Charset" => $charset, "Data" => $plainBody, ], ], "Subject" => [ "Charset" => $charset, "Data" => $subject, ], ], ]; // 假设发送前已进行邮箱验证 if(isset($bcc) && trim($bcc)) { $sendingArray["Destination"]["BccAddresses"] = [$bcc]; } $result = $SesClient->sendEmail($sendingArray); $messageId = $result["MessageId"]; echo "Message sent! Message ID: " . $messageId; } catch (AwsException $e) { echo "AWS sending error: " . $e->getMessage(); } catch (Exception $ex) { echo "General error: " . $ex->getMessage(); } ?>
解决方案
不需要改用SendRawEmail,你可以通过RFC 2047编码处理发件人名称,使其符合邮件头规范,SendEmail方法支持这种编码后的字符串作为Source参数。
修改步骤
使用PHP的mb_encode_mimeheader函数对含特殊字符的发件人名称进行编码,该函数会将UTF-8字符串转换为符合邮件标准的Base64编码格式:
// 对发件人名称进行MIME头编码 $encodedFromName = mb_encode_mimeheader($fromName, 'UTF-8', 'B'); // 组装Source参数 "Source" => "$encodedFromName <$from>",
修改后的完整发送数组示例
$sendingArray = [ "Destination" => [ "ToAddresses" => $toArr ], "ReplyToAddresses" => [$reply], // 使用编码后的发件人名称 "Source" => mb_encode_mimeheader($fromName, 'UTF-8', 'B') . " <$from>", "Message" => [ "Body" => [ "Html" => [ "Charset" => $charset, "Data" => $body, ], "Text" => [ "Charset" => $charset, "Data" => $plainBody, ], ], "Subject" => [ "Charset" => $charset, "Data" => $subject, ], ], ];
原理说明
邮件头的发件人名称不能直接包含非ASCII的UTF-8字符,必须通过RFC 2047标准编码为=?charset?encoding?encoded-text?=格式的字符串。mb_encode_mimeheader函数会自动完成这个转换,确保收件人邮箱客户端能正确解码并显示特殊字符。
内容的提问来源于stack exchange,提问作者FiddlingAway
相关产品推荐
相关产品推荐

