簡體   English   中英

Amazon SES 中的 UTF-8 發件人姓名/簡單電子郵件服務

[英]UTF-8 Sender name in Amazon SES / Simple email service

使用 Amazon 的 SES 和SendEmail方法(PHP SDK v3)發送電子郵件時,是否可以設置一個用戶友好的發件人名稱,該名稱具有非標准字符(西里爾文、帶變音符號的拉丁文、中文等)字符?

我的代碼如下。

<?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"; // <==== This is not displayed correctly in the receiver"s inbox. The UT8-characters are ommitted or garbled

$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" => [
            // Let's assume that there's some sort of email validation for each of the email addresses
            "ToAddresses" => $toArr
        ],
        // Let's assume that there's some sort of email validation for each of the email addresses
        "ReplyToAddresses" => [$reply],
        "Source" => "$fromName <$from>",
        "Message" => [
            "Body" => [
                "Html" => [
                    "Charset" => $charset,
                    "Data" => $body,
                ],
                "Text" => [
                    "Charset" => $charset,
                    "Data" => $plainBody,
                ],
            ],
            "Subject" => [
                "Charset" => $charset,
                "Data" => $subject,
            ],
        ],
    ];

    // Let's assume that there's some sort of email validation before this
    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();
}
?>

雖然代碼確實有效(電子郵件已發送等),但發件人的姓名顯示不正確。 我的意思是省略非標准拉丁字符和非拉丁字符,或將其轉換為另一組字符。

我試過用這個修改Source值:

    'Source' => [
        "Charset" => $charset,
        "Data"  => "$fromName <$from>"
    ],

這(當然)不起作用,因為Source的值必須是一個string (這也是SendEmail 的文檔條目所指出的)。

在這種情況下,我什至可以在發件人姓名中使用 UTF-8 字符嗎?還是我必須更改發送電子郵件的方式(例如切換到SendRawEmail

多虧了文檔,設法解決了這個問題。

這是關鍵部分:

發件人姓名(也稱為友好名稱)可能包含非 ASCII 字符。 這些字符必須使用 MIME encoded-word syntax 進行編碼,如 RFC 2047 中所述。

以此為指導,我對代碼進行了以下更改(僅發布相關部分)。

<?php
// ...
$charset = "UTF-8";
$from = "info@example.com";
$fromName = "ĐŠĆČŽ-ЖЂШЋЧ usual alphabet";
$fromName = mb_encode_mimeheader($fromName,$charset); // <=== This is the key part

// ....

try {
    $sendingArray = [
        // ...
        "Source" => "$fromName <$from>",
        // ...
    ];
    // ...
} catch (AwsException $e) {
    // ...
} catch (Exception $ex) {
    // ...
}
?>

為了能夠使用它,您的 PHP 版本必須啟用多字節字符串擴展。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM