Vinod VT
Vinod VT

Reputation: 7159

How to replace tokens on email campain using REST API in PHP ?

I have an email campaign on Marketo to send emails using PHP. on my email template I have a token like, {{my.emailBody:default=Body}} I would like to replace the the token with my custom email content from my PHP code,

This is my code,

$sample = new SendSampleEmail();
$sample->id = 11111;
$sample->emailAddress = "[email protected]";
print_r($sample->postData());

class SendSampleEmail{
  private $host = "https://AAA-AAA-121.mktorest.com";
  private $clientId = "dxxxxxxxxxxxxxxxxxxxxx1";
  private $clientSecret = "Sxxxxxxxxxxxxxxxxxxxxxxxxxxxxe";
  public $id; //id of  to delete
  public $emailAddress;//email address to send to
  public $textOnly;//boolean option to send text only version
  public $leadId;// id of lead to impersonate

  public function postData(){
    $url = $this->host . "/rest/asset/v1/email/" . $this->id . "/sendSample.json?access_token=" . $this->getToken();
    $requestBody  = "&emailAddress=" . $this->emailAddress;
    if (isset($this->textOnly)){
      $requestBody .= "&textOnly=" . $this->textOnly;
    }
    if (isset($this->leadId)){
      $requestBody .= "&leadId=" . $this->leadId;
    }
    //print_r($requestBody);

    $ch = curl_init($url);
    curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
    curl_getinfo($ch);
    $response = curl_exec($ch);
    return $response;
  }

  private function getToken(){
    $ch = curl_init($this->host . "/identity/oauth/token?grant_type=client_credentials&client_id=" . $this->clientId . "&client_secret=" . $this->clientSecret);
    curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json',));
    $response = json_decode(curl_exec($ch));
    curl_close($ch);
    $token = $response->access_token;
    return $token;
  } 
}

Using this code I can successfully trigger the emails, but how can I replace the token value {{my.emailBody:default=Body}} ?

Upvotes: 1

Views: 369

Answers (2)

luis.florez
luis.florez

Reputation: 72

I have the same problem which I tried to used Assets Tokens from the REST API: http://developers.marketo.com/rest-api/assets/tokens/ to modify token's values, but it is the only endpoint I cannot make it work. Please let me know if you could make it work.

However, I used the SOAP API to solve for this issue:

You create a Batch Campaign from Marketo inside a Marketo Program that contains the Token you want to modify and the email you want to send using that token.

The SOAP API will schedule the campaign to run (you can set the current time for immediate run), and you can set the value for the tokens:

public function schedule_campaign($program_name,$campaign_name,$token_name,$token_value)
{
    $params = new stdClass();
    $params->programName = $program_name;
    $params->campaignName = $campaign_name;
    $dtzObj = new DateTimeZone("America/New_York");
    $dtObj = new DateTime('now', $dtzObj);
    $params->campaignRunAt = $dtObj->format(DATE_W3C);

    $token = new stdClass();
    $token->name = "{{my.".$token_name."}}";
    $token->value = $token_value;

    $params->programTokenList = array("attrib" => $token);
    $params = array("paramsScheduleCampaign" => $params);

    $soapClient = new SoapClient(MARKETO_SOAP_ENDPOINT ."?WSDL", self::$auth_options);
    try 
    {
        $response = $soapClient->__soapCall('scheduleCampaign', $params,  self::$auth_options, self::$auth_header);
        return true;
    }
    catch(Exception $ex) {
        return false;
    }
}

UPDATE: I've found the way to update/replace tokens using REST API:

public function create_token($folder_id,$name,$content,$folder_type = 'Program')
    {
        $folder_id = intval($folder_id);
        $endpoint = 'rest/asset/v1/folder/'.$folder_id.'/tokens';
        $url = $this->url . $endpoint . ".json?access_token=" . self::$token."&folderType=".$folder_type."&name=".$name."&type=".  urlencode('rich text')."&value=".urlencode($content);

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: x-www-form-urlencoded'));
        curl_setopt($ch, CURLOPT_POST, 1);
        $response = curl_exec($ch);
        curl_close($ch);
        return json_decode($response);
    }

Upvotes: 1

kelkington
kelkington

Reputation: 411

Token replacement only works with the Request Campaign and Schedule Campaign APIs, you can't replace my tokens with the send sample email API.

Upvotes: 1

Related Questions