<?php  
function sendpulse_token($client_id,$client_secret){
    $curl = curl_init();
    $params = [
      'grant_type' => "client_credentials",
      'client_id' => $client_id,
      'client_secret' => $client_secret
    ];
    $curl_options = [
      CURLOPT_URL => 'https://api.sendpulse.com/oauth/access_token',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => json_encode($params),
      CURLOPT_HTTPHEADER => [
        'Content-Type: application/json'
      ]
    ];
    curl_setopt_array($curl, $curl_options);
    $response = curl_exec($curl);
    curl_close($curl);
    $reply=json_decode($response,true);
    if($reply['access_token']!=""){
        return $reply['access_token'];
    }else{return $reply['error_description'];}
}

function sendpulse_send($email,$token){
    $curl = curl_init();
    if(isset($email['html'])){$email['html'] = base64_encode($email['html']);}
    $params = ['email' => $email];
    $curl_options = [
      CURLOPT_URL => 'https://api.sendpulse.com/smtp/emails',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => json_encode($params),
      CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'Authorization: Bearer '.$token
      ]
    ];
    curl_setopt_array($curl, $curl_options);
    $response = curl_exec($curl);
    curl_close($curl);
    $reply=json_decode($response,true);
    if(isset($reply['result'])){
        if($reply['result'] == 'true'){return 1;}else{return 0;}
    }else{return $reply['message'];}
}
function sendpulse_template($name,$token){
    $curl = curl_init();
    $curl_options = [
    CURLOPT_URL => 'https://api.sendpulse.com/templates/?owner=me',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'Authorization: Bearer '.$token
      ]
    ];
    curl_setopt_array($curl, $curl_options);
    $response = curl_exec($curl);
    curl_close($curl);
    $reply=json_decode($response,true);
    if(!isset($reply['error_code'])){
        for ($i=0; $i < count($reply); $i++) {
            if($name == $reply[$i]['name']){$ok = $reply[$i]['id'];}
        }
        if(isset($ok)){
            return $ok;
        }else{return 0;}
    }else{return $reply['message'];}
}
/***************************/
$email = array(
    'html' => '<p>Hello!</p>',
    'subject' => 'Sipariş var',
    'from' => array(
        'name' => 'Firma adınız',
        'email' => 'firma email adresiniz',
    ),
    'to' => array(
        array(
            'name' => 'Client',
            'email' => 'gonderilecekkisi@gmail.com',
        ),
    ),
    'template' => array( //template isteğe bağlıdır,kaldırabilirsiniz.
        'id' => şablon id',
        'veriables' => '{"degisken": "tanimi"}'
    )
);
$token = sendpulse_token("Client Id","Client Secret"); //1 Saat boyunca geçerli token döndürür.
echo $token;
echo "<br>";
echo sendpulse_send($email,$token); //Email gönderir.
echo "<br>";
echo sendpulse_template($token,"order"); //panelde kaydettiğiniz şablon adı ile arama yapmanızı sağlar eğer varsa id döndürür.
?>