ashbringer
ashbringer

Reputation: 103

Amazon SES Raw Email with personalized body

I'm trying to use Amazon SES API on PHP. I can send special html, header and file etc to recipients with SMTP method, but I want to do this with API. I tried the sendBulkTemplatedEmail method, but I can't send custom headers and files. I tried this with SendRawEmail but I can't create recipient specific content. The Amazon SES API document states that with SendRawEmail, the same content can be sent to multiple recipients.

What is wrong with the method I used below?

$credentials = new \Aws\Credentials\Credentials('access', 'secret_key');

$SesClient = new \Aws\Ses\SesClient([
    'version' => '2010-12-01',
    'region'  => 'us-west-2',
    'credentials' => $credentials
]);

$templateResult = $SesClient->createTemplate([
    'Template' => [
       'TemplateName' => 'MyTemplate', //Required
       'SubjectPart'  => "{{subject}}",
       'TextPart'      => "{{text}}",
       'HtmlPart'     => "{{html}}"
   ],
]);

$aws_data = array(
    "Template"=>"MyTemplate",
    "DefaultTemplateData"=>"{}",
    'Destinations'=>array(),
    'Source'=>'[email protected]',
    'ReplyToAddresses'=>['[email protected]']
);


foreach($data['receivers'] as $key => $value){

        $replacementData = array(
                                    'subject'=>'Test Subject for ' . $value['mail'],
                                    'html'=>'Test Html',
                                    'text'=>'Test Text'
        );

$destinations[] = array(
                            'Destination'=>array(
                                                    'ToAddresses'=>[$value['mail']]
                            ),
                            'ReplacementTemplateData' => json_encode($replacementData),
                            'Headers'=>array(
                                                'X-MyHeader'=>array(
                                                                        'Charset' => 'UTF-8',
                                                                        'Data' => '123'
                                                )
                            )
);

}

$aws_data["Destinations"] = $destinations;

try {
    $result = $SesClient->sendBulkTemplatedEmail($aws_data);
    $messageId = $result['MessageId'];
    echo("Email sent! Message ID: $messageId"."\n");
} catch (AwsException $e) {
 
    // The output error message if it fails
 
    echo $e->getMessage();
    echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n");
    echo "\n";
}

Thanks!

Upvotes: 0

Views: 361

Answers (0)

Related Questions