gr8shivam / laravel-sms-api

Laravel package to provide SMS API integration.
MIT License
99 stars 30 forks source link

send sms and intergrate it into laravel 7 #19

Closed mulatha closed 3 years ago

mulatha commented 4 years ago

Hello, I hope you are doing okay. I am using this the following code to send SMS to my members.

`public function storeSms(Request $request) { $body = ""; $recipients = 1; if (Setting::where('setting_key', 'sms_enabled')->first()->setting_value == 1) { if ($request->send_to == 0) {

            $active_sms = Setting::where('setting_key', 'active_sms')->first()->setting_value;
            foreach (Borrower::all() as $borrower) {
                $body = $request->message;

//lets build and replace available tags $body = str_replace('{borrowerTitle}', $borrower->title, $body); $body = str_replace('{borrowerFirstName}', $borrower->first_name, $body); $body = str_replace('{borrowerLastName}', $borrower->last_name, $body); $body = str_replace('{borrowerAddress}', $borrower->address, $body); $body = str_replace('{borrowerMobile}', $borrower->mobile, $body); $body = str_replace('{borrowerEmail}', $borrower->email, $body); $body = str_replace('{borrowerTotalLoansDue}', round(GeneralHelper::borrower_loans_total_due($borrower->id), 2), $body); $body = str_replace('{borrowerTotalLoansBalance}', round((GeneralHelper::borrower_loans_total_due($borrower->id) - GeneralHelper::borrower_loans_total_paid($borrower->id)), 2), $body); $body = str_replace('{borrowerTotalLoansPaid}', GeneralHelper::borrower_loans_total_paid($borrower->id), $body); $email = $borrower->email; $body = trim(strip_tags($body)); if (!empty($borrower->mobile)) { GeneralHelper::send_sms($borrower->mobile, $body); } $recipients = $recipients + 1; } $sms = new Sms(); $sms->user_id = Sentinel::getUser()->id; $sms->message = $body; $sms->gateway = $active_sms; $sms->branch_id = session('branch_id');; $sms->recipients = $recipients; $sms->send_to = 'All borrowers'; $sms->save(); GeneralHelper::audit_trail("Sent SMS to all borrower"); Flash::success("SMS successfully sent"); return redirect('communication/sms'); } else { $body = $request->message; $borrower = Borrower::find($request->send_to); //lets build and replace available tags $body = str_replace('{borrowerTitle}', $borrower->title, $body); $body = str_replace('{borrowerFirstName}', $borrower->first_name, $body); $body = str_replace('{borrowerLastName}', $borrower->last_name, $body); $body = str_replace('{borrowerAddress}', $borrower->address, $body); $body = str_replace('{borrowerMobile}', $borrower->mobile, $body); $body = str_replace('{borrowerEmail}', $borrower->email, $body); $body = str_replace('{borrowerTotalLoansDue}', round(GeneralHelper::borrower_loans_total_due($borrower->id), 2), $body); $body = str_replace('{borrowerTotalLoansBalance}', round((GeneralHelper::borrower_loans_total_due($borrower->id) - GeneralHelper::borrower_loans_total_paid($borrower->id)), 2), $body); $body = str_replace('{borrowerTotalLoansPaid}', GeneralHelper::borrower_loans_total_paid($borrower->id), $body); $body = trim(strip_tags($body)); if (!empty($borrower->mobile)) { $active_sms = Setting::where('setting_key', 'active_sms')->first()->setting_value; GeneralHelper::send_sms($borrower->mobile, $body); $sms = new Sms(); $sms->user_id = Sentinel::getUser()->id; $sms->message = $body; $sms->gateway = $active_sms; $sms->recipients = $recipients; $sms->branch_id = session('branch_id'); $sms->send_to = $borrower->first_name . ' ' . $borrower->last_name . '(' . $borrower->unique_number . ')'; $sms->save(); Flash::success("SMS successfully sent"); return redirect('communication/sms'); }

        }
        GeneralHelper::audit_trail("Sent SMS   to borrower");
        Flash::success("Sms successfully sent");
        return redirect('communication/sms');
    } else {
        Flash::warning('SMS service is disabled, please go to settings and enable it');
        return redirect('setting/data')->with(array('error' => 'SMS is disabled, please enable it.'));
    }

}`

My general help code for sending SMS is

`public static function send_sms($to, $msg) { if (Setting::where('setting_key', 'sms_enabled')->first()->setting_value == 1) { if (!empty(SmsGateway::find(Setting::where('setting_key', 'active_sms')->first()->setting_value)) ) { $active_sms = SmsGateway::find(Setting::where('setting_key', 'active_sms')->first()->setting_value); $append = "&"; $append .= $active_sms->to_name . "=" . $to; $append .= "&" . $active_sms->msg_name . "=" . $msg; $url = $active_sms->url . $append; //send sms here $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_VERBOSE, true); $curl_scraped_page = curl_exec($ch); curl_close($ch); } }

}`

Also, my functions send bulk SMS to many people.

I have attached my SMS sending API documentation with the hope that you u help me with how to send SMS. Because I have been using General helper API send SMS and I would like to start using your package thanks

edited_API V1.14.pdf

gr8shivam commented 4 years ago

Please share the configuration you're using. Make sure to mask the sensitive information like username and password.

mulatha commented 4 years ago

'egosms' => [ 'url' => 'https://egosms.co/api/v1/plain/', 'params' => [ 'send_to_param_name' => 'number', //Send to Parameter Name 'msg_param_name' => 'message', //Message Parameter Name 'others' => [ 'username' => 'username', 'password' => 'password', 'sender' => 'saccosms', ], ], 'add_code' => false, //Include Country Code ],

gr8shivam commented 4 years ago

This seems to be good. You can send the SMS like:

smsapi()->gateway('egosms')->sendMessage("TO", "Message");

Try it once and let me know the error you get. Make sure to check the Laravel logs to find the error, if any.

gr8shivam commented 3 years ago

Hi @mulatha , I hope the issue has been resolved. Please reopen the issue if there's any concern.