如何在AWS SES使用PHP SDK发送的电子邮件中实现List-Unsubscribe标头

时间:2016-02-08 20:09:14

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

我尝试使用AWS PHP SDK添加自定义标头,因此我可以实现" 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;

1 个答案:

答案 0 :(得分: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();
    }}