- Forums
- laravel
- How To Use Twilio Api With Laravel To Send Sms Messages In Php
use this code to send sms messages in your laravel application with twilio [5223], Last Updated: Mon Jun 24, 2024
edwin
Sun Oct 22, 2023
0 Comments
260 Visits
I used this code in a controller of a laravel application to send sms messages on registration users:
.env
# SMS TEXT MESSAGES
TWILIO_SID=1234567890abcABC
TWILIO_TOKEN=1234567890abcABC
TWILIO_FROM=+1234567890
example_controller.php
//https://www.twilio.com/blog/create-sms-portal-laravel-php-twilio
$message = $upload->recipient_fname.', View Files: https://secure.example.com/download Secret Code: '.$upload->secret_key;
try {
$account_sid = getenv("TWILIO_SID");
$auth_token = getenv("TWILIO_TOKEN");
$twilio_number = getenv("TWILIO_FROM");
$client = new Client($account_sid, $auth_token);
$client->messages->create($upload->recipient_phone,[
'from' => $twilio_number,
'body' => $message
]);
return response()->json($token, 200);
}catch (Exception $e) {
//https://stackoverflow.com/questions/70377269/laravel-twilio-twilio-exceptions-configurationexception-credentials-are-requi
//dd("Error: " . $e->getMessage());
// TO SEE EXEMPTION ERROR USE: $e->getMessage()
return response(['error'=>$e->getMessage()], 404);
//return response()->json($e, 404);
}