jueves, 9 de febrero de 2023

Sending SMS from csv

 


<?php


require __DIR__ . "/vendor/autoload.php";

 

use Telnyx\Api\ApiClient;

use Telnyx\Api\Message;


// Read the csv file

$file = fopen('file.csv', 'r');


// Set the api key

$telnyx = new ApiClient("YOUR_API_KEY");


while (($line = fgetcsv($file)) !== FALSE) {

    // Retrieve data from the csv

    $phoneNumber = $line[0];

    $message = $line[1];


    // Send the SMS message

    $message = Message::create([

        "from" => "+18881234567",

        "to" => $phoneNumber,

        "text" => $message

    ]);

    echo "\n Sent SMS to {$phoneNumber}: {$message->text}\n";

}


// Close the file

fclose($file);



?>


--------------------------------------------------------------------------------------------------------


This code assumes that the csv file contains data structured like this:

  Name, Phone Number

  John, +19998881234

  Sam, +14447778899


<?php

$csvFile = fopen('contactList.csv', 'r');

$telnyx = new Telnyx\Client('YOUR_TELNYX_API_KEY');


// Iterate over each row in the CSV file

while (($row = fgetcsv($csvFile)) !== FALSE) {

  // Set recipient name and number

  $name = $row[0];

  $number = $row[1];


  // Set message body

  $message = new Telnyx\SMSMessage(

    ["from" => "+15551234567",

     "to" => $number,

     "text" => "Hey, $name! Test message from Telnyx."]

  );


  // Send the message

  $response = $telnyx->SMS->messages->create($message);

}


fclose($csvFile);

?>

No hay comentarios:

Publicar un comentario