PHP | How to update all email templates for the companies of an account

Publié sur un an il y a par  Julien Pauthier

Publier un sujet
Julien Pauthier
Julien Pauthier Administrateur

You can extract all templates from a list of locations, and do a search&replace for each email template:

$token = "";
$apikey = "";

// Fetching list of companies/locations for the account
$data = file_get_contents("$apikey&token=$token");
$results = json_decode($data);

// Table header for output
echo "Company name\tCompany ID\tEmail template name\tEmail template ID\n";

foreach ($results->items as $company) {
    // Fetching list of email templates for the current company/location
    $data2 = file_get_contents("$company->id/settings/messages/emails?apiKey=$apikey&token=$token");

    $ct = json_decode($data2);
    foreach ($ct->items as $key => $value) {
        // Table row for output
        echo $company->name . "\t" . $company->id . "\t" . $value->name . "\t" . $value->id . "\n";
        $ch = curl_init("$company->id/settings/messages/emails/$value->id?apiKey=$apikey&token=$token");

        //replace default color with new color
        $updatedTemplate = str_replace('#30AF93', '#194867', $value->html);

        //setup request to send json via PUT
        $data3 = array(
            'name' => $value->name,
            'html' => $updatedTemplate
        $payload = json_encode($data3);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

        //set the content type to application/json
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

        //execute the POST request
        $result = curl_exec($ch);

        //close cURL resource

0 Votes

0 Commentaires

Connexion ou S'inscrire pour poster un commentaire