如何使用PHP SDK在AWS SES发送的电子邮件中实现列表取消订阅标头 [英] How to implement List-Unsubscribe header in emails sent by AWS SES with the PHP SDK

查看:289
本文介绍了如何使用PHP SDK在AWS SES发送的电子邮件中实现列表取消订阅标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用AWS PHP SDK添加自定义标头,以便实现列表取消订阅"标头.
问题是我找不到任何方法来实现它.

我已经阅读了文档,但一无所知.
http://docs .aws.amazon.com/aws-sdk-php/v2/api/class-Aws.Ses.SesClient.html#_sendEmail

我在做什么错了?
在此先感谢您提供的任何帮助.

I was trying to add a custom header using the AWS PHP SDK so I can implement the "List-unsubscribe" header.
The problem is that I cannot find anywhere how to implement it.

I've read the documentation but nothing.
http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.Ses.SesClient.html#_sendEmail

What am I doing wrong?
Thanks in advance for any help provided.

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;

推荐答案

尝试一下与我一起使用的这段代码,我认为您不能使用 sendEmail 函数来实现此功能.

Try this code it's worked with me, i don't think you can use the sendEmail function to implement this.

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();
    }}

这篇关于如何使用PHP SDK在AWS SES发送的电子邮件中实现列表取消订阅标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆