Install the cakemail
package using composer
$ composer require cakemail/cakemail
Create an object from the Cakemail\Api
class with your Cakemail username and password. The object will take care of
all authorization mechanisms automatically.
$username = '[email protected]';
$password = 'somepassword';
$api = new Api($username, $password);
Call one of the API operations (refer to the online documentation here)
$myAcount = $api->account->getSelf();
API operations accepts all the parameters in an array. You can use any parameter, in any order, but you must provide the required ones.
$filteredCampaigns = $api->campaign->list([
'with_count' => true,
'filter' => 'type==recurring;name==Newsletter'
]);
use Cakemail\Lib\Model;
$sender = $api->sender->create([
'create_sender' => new Model\CreateSender([
'name' => 'My Sender',
'email' => '[email protected]'
])
]);
# look for the confirmation ID in your email inbox
$api->sender->confirm([
'sender_id' => $sender['id'],
'confirm_sender' => new Model\ConfirmSender([
'confirmation_id' => '[Confirmation ID]'
])
]);
use Cakemail\Lib\Model;
$myList = $api->list->create([
'model_list' => new Model\ModelList([
'name' => 'My new List',
'default_sender' => new Model\Sender([
'id' => $senderId
])
])
]);
use Cakemail\Lib\Model;
$myList = $api->transactional_email->send([
'email' => new Model\Email([
'email' => '[email protected]',
'sender' => $sender,
'content' => new Model\EmailContent([
'subject' => 'Subject line',
'html' => 'Email body',
'encoding' => 'utf-8'
])
])
]);
The CakemailAPI always return its data under the 'data' key. For simplicity, the resource data is accessible from the returned response directly:
$myUser = $api->user->getSelf();
print_r($myUser['email']);
Some methods return a list of resources on which you can iterate directly:
$campaigns = $api->campaign->list();
foreach ($campaigns as $campaign) {
$html = $api->campaign->render([
'campaign_id' => $campaign['id']
])['html'];
print_r("id: {$campaign['id']}, name: {$campaign['name']}, html: {$html}");
}
Pagination is stored in the 'pagination' key of all methods returning a list of resources:
$campaigns = $api->campaign->list(['with_count' => 'true']);
print_r("
page: " . $campaigns['pagination']['page'] . ",
per_page: " . $campaigns['pagination']['per_page'] . ",
count: " . $campaigns['pagination']['count'] . "
");