0

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'=>'sender@test.com',
    'ReplyToAddresses'=>['reply@test.com']
);


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!

ashbringer
  • 83
  • 1
  • 5
  • I am not getting much, can you please be more clear about what are you trying to do and what are you expecting..may be with some example? – Arpit Jain Apr 17 '23 at 17:01
  • @ArpitJain Hello, I want to send bulk mail with receiver-specific and custom headers too. But above code does not allow to use raw content and personelized mails. For example I was used SendGrid API before, I can send 50 mail per connection with that. I think AWS Ses BulkEmail works like that but I cant use personelized contents and custom headers on this class. – ashbringer Apr 19 '23 at 12:05
  • To send bulk emails, you can use the `SendBulkEmail` API. – Arpit Jain Apr 19 '23 at 13:07
  • Also to send email using this operation, your call must meet the following requirements: [SendBulkTemplatedEmail] (https://docs.aws.amazon.com/ses/latest/APIReference/API_SendBulkTemplatedEmail.html) – Arpit Jain Apr 19 '23 at 13:12
  • @ArpitJain Hi, I've already read this doc, also successfully sent bulk mails with these instructions but I cant use customized bodies and headers with bulkemailtemplate method. SendRawMail do it but its not allows to send mails in a package for example, 50 mail per API connection etc. I'm trying to find out if this feature is missing or if I'm using the system incorrectly. Many thanks for your time. – ashbringer Apr 21 '23 at 18:43

0 Answers0