【问题标题】:How to implement List-Unsubscribe header in emails sent by AWS SES with the PHP SDK如何使用 PHP SDK 在 AWS SES 发送的电子邮件中实现 List-Unsubscribe 标头
【发布时间】:2023-03-05 19:41:01
【问题描述】:

我尝试使用 AWS PHP 开发工具包添加自定义标头,以便实现“List-unsubscribe”标头。
问题是我在任何地方都找不到如何实现它。

我已阅读文档,但没有。
http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.Ses.SesClient.html#_sendEmail

我做错了什么?
提前感谢您提供的任何帮助。

require_once("./AWS/aws-sdk/aws-autoloader.php");

$config = array('credentials' => array('key' => $awsKey,
                                       'secret' => $awsSecret),
                'version' => 'latest',
                'region' => 'us-east-1');

$client = \Aws\Ses\SesClient::factory($config);

$to = 'test@example.com';
$from = 'testfrom@example.com';
$subject = 'This is a test';
$text= 'This is a test';
$html= 'This is a <b>test</b>';
$replyTo = 'replyto@example.com';

$message=array(
    // Source is required
    'Source' => $from,

    // Destination is required
    'Destination' => array('ToAddresses' => array($to)),

    // Message is required
    'Message' => array(
        // Subject is required
        'Subject' => array(
            // Data is required
            'Data' => $subject,
            'Charset' => 'UTF-8',
        ),
        // Body is required
        'Body' => array(
            'Text' => array(
                // Data is required
                'Data' => $text,
                'Charset' => 'UTF-8',
            ),
            'Html' => array(
                // Data is required
                'Data' => $html,
                'Charset' => 'UTF-8',
            ),
        ),
    ),

    // reply To..
    'ReplyToAddresses' => array($replyTo),

    // Is this correct??
    'AddHeaderAction' => array('header_name'=> "List-Unsubscribe",
                               'header_value'=> urlencode('<unsubscribeme@example.com>')));

try
{
    $result = $client->sendEmail($message);
    $messageID=$result->get('MessageId');
}
catch (Exception $response)
{
    die('error');
}

echo 'message sent: '. $messageID;

【问题讨论】:

    标签: php amazon-web-services amazon-ses


    【解决方案1】:

    试试这个对我有用的代码,我认为你不能使用 sendEmail 函数来实现它。

    public static function _sendMail($to, $subject, $body, $from)
    {
        $random_hash = 'site_' . md5(date('r', time()));
        $emailtext = "Return-Path: <{$from}>" . "\r\n";
        $emailtext .= "Reply-To: <$from>" . "\r\n";
        $emailtext .= "From: YOUR NAME <{$from}>" . "\r\n";
        $emailtext .= 'To: ' . $to . "\r\n";
        $emailtext .= "Subject: {$subject}" . "\r\n";
        $emailtext .= "List-Unsubscribe: <mailto:youremail@gmail.com>". "\r\n";
        $emailtext .= 'Date: ' . date('D, j M Y H:i:s O') ."\r\n";
        $emailtext .= 'Message-ID: <njnjknjkr, time())),0,5) . -' . substr(md5(date('r', time())),0,5) .'-' . substr(md5(date('r', time())),0,5) . '@site.com>' ."\r\n";
        $emailtext .= "Content-Type: multipart/alternative; " . "\r\n";
        $emailtext .= "\t" . 'boundary="' . $random_hash . '"' . "\r\n";
        $emailtext .= 'MIME-Version: 1.0' ."\r\n\r\n";
        $emailtext .= '--' . $random_hash . "\r\n";
        $emailtext .= 'Content-Type: text/plain; charset=us-ascii' . "\r\n";
        $emailtext .= 'Content-Transfer-Encoding: 7bit' . "\r\n\r\n\r\n";
        $emailtext .= '--' . $random_hash . "\r\n";
        $emailtext .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
        $emailtext .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n\r\n";
        $html = preg_replace("#(?<!\r)\n#si", "\r\n", $body) . "\r\n";
        $emailtext .= quoted_printable_encode($html);
        $emailtext .= '--' . $random_hash . "--";
    
        $args = array(
            'Source' => $from,
            'Destinations' => array($to),
            'RawMessage' => array(
                'Data' => base64_encode($emailtext),
            ),
            'FromArn' => $from,
            'SourceArn' => $from,
            'ReturnPathArn' => $from,
        );
    
        $client = SesClient::factory(array(
            'key' => self::$__accessKey,
            'secret' => self::$__secretKey,
            'region' => Region::US_EAST_1
        ));
    
        try {
            return $client->sendRawEmail($args);
        }catch (MessageRejectedException $mrEx){
            return $mrEx->getMessage();
        }catch (\Exception $ex){
           return $ex->getMessage();
        }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 2017-02-07
      • 2015-03-29
      相关资源
      最近更新 更多