如何将多个SMS消息发送到唯一的电话号码?

时间:2019-01-28 06:05:35

标签: php twilio

当我迭代数据库中的数字时,它不会发送多条短信。

我尝试使用电话号码所在的变量,但是它一直失败。

 if ($result->num_rows > 0) {
      while($row = $result->fetch_assoc()) {

            $email = $row['email'];
            $phoneNum = $row['phone'];


                  $client = new Client($sid, $token);

                  // Use the client to do fun stuff like send text messages!
                  $client->messages->create(
                      // the number you'd like to send the message to
                      $phoneNum,
                      array(
                          // A Twilio phone number you purchased at twilio.com/console
                          'from' => '+83939393',
                          // the body of the text message you'd like to send
                          'body' => 'The message'
                      )
                  );


      } 

1 个答案:

答案 0 :(得分:0)

 //All that needed to be done is move problem query out of this while loop into its own.


// moved to here

if ($result->num_rows > 0) {
      while($row = $result->fetch_assoc()) {

          $email = $row['email'];
          $phoneNum = $row['phone'];

          $to = $email;

          $message.= "
                <html>
                <head>
                <title>Email</title>
                </head>
                <body>

                <h1>Data I wanted to show in email</h1>";
                 // problem query below

                  $message.= "<ul>";
                  // moved $sql = "SELECT DISTINCT query here";
                  $result = $db->query($sql);
                  $countUrl = 0;
                  $message .= "<ul>";
                if ($result->num_rows > 0) {

                while($row = $result->fetch_assoc()) 
                {

                   $message .= "<li><a href=".$row['url'].">". $row['url']."</a></li>";


                }

                $message .= "</ul>"; 
                $message .= "</li>";
               // moved this outside the while loop and everything was fine.

                $message .="


                </div>


                </body>
                </html>
                ";
                $headers = "MIME-Version: 1.0" . "\r\n";
                $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
                $headers .= 'From: no-repley@blah.com' . "\r\n" .
                 'Reply-To: ' . $e_ . "\r\n" .
                'X-Mailer: PHP/' . phpversion();
                $headers .= 'From: name' . "\r\n";

           if( mail($to, $subject, $message, $headers) ){
              echo "success mailed to ".$to;

                  $client = new Client($sid, $token);

                  // Use the client to do fun stuff like send text messages!
                  $client->messages->create(
                      // the number you'd like to send the message to
                      $phoneNum,
                      array(
                          // A Twilio phone number you purchased at twilio.com/console
                          'from' => '+83838388',
                          // the body of the text message you'd like to send
                          'body' => '$url variables from that query'
                      )
                  );

           }

        }

      }
  }else{
     echo "no emails set";
  }

}