先至 Vonage APIs Dashboard (nexmo.com) 註冊一個帳號,註冊過程中需要驗證Email 帳號,並提供電話號碼做驗證,做完驗證後選擇 Developer 【Yes】及開發程式語言【PHP】,並點擊【SMS】。
之後會看到一個基本免費【€2.0】的額度,試著按一下傳送訊息,花費 €0.04 ,也就是說一則 SMS 要 €0.04 ,大概是 1.5 元。( 跟 Amazon SNS 差不多,不過 Amazon 有每個月一百萬通免費的額度之後超過才收費,相較之下,Amazon 提供的更便宜。)
比價參考:
Twilio and Amazon SMS 價格比較 | Inoundnow行銷客 (inboundnow.org)
Amazon Simple Notification Service (SNS) 定價 | 訊息傳遞服務 | AWS
取得 API Key 及 API Secret
安裝 Nexmo/nexmo-laravel 套件
# 安裝這個套件會一起將 nexmo/client 裝起來方便給 Laravel 使用
composer require nexmo/laravel
# 產生設定檔 執行下面指令找到 Provider: Nexmo\Laravel\NexmoServiceProvider
# 或者 複製 vendor/nexmo/laravel/config/nexmo.php 檔案到 config 目錄下
php artisan vendor:publish
修改 .env
NEXMO_KEY=取得的 API ID
NEXMO_SECRET=取得的 API SECRET
NEXMO_FROM=註冊時所登錄的號碼或者付費購買的虛擬號碼
修改 config\app.php
'providers' => [
....
Nexmo\Laravel\NexmoServiceProvider::class,
],
'aliases' => [
...
'Nexmo' => Nexmo\Laravel\Facade\Nexmo::class,
],
新增路由
Route::post('sms/nexmosms','Admin\SMSController@nexmosms')->name('nexmosms');
新增表單
<form id="nexmosms" action="{{ route('admin.nexmosms') }}" method="POST" role="form">
@csrf
<div class="card-body">
<div class="form-group">
<label for="exampleInputEmail1">To</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-phone"></i></span><span class="input-group-text">+886</span>
</div>
<input class="form-control {{ $errors->has('phone') ? ' is-invalid' : '' }}" type="number" name="phone" pattern="[0-9]" placeholder="輸入行動電話號碼">
@if ($errors->has('phone'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('phone') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<label>內容</label>
<textarea class="form-control {{ $errors->has('content') ? ' is-invalid' : '' }} " rows="3" name="content" placeholder="輸入訊息內容"></textarea>
@if ($errors->has('content'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('content') }}</strong>
</span>
@endif
</div>
</div>
<div class="card-footer text-center">
<button type="submit" class="btn btn-primary">發送</button>
</div>
</form>
新增驗證 Request
php artisan make:request Admin\AdminSendNexmosmsRequest
<?php
namespace App\Http\Requests\Admin;
use Illuminate\Foundation\Http\FormRequest;
use Auth;
class AdminSendNexmosmsRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
//必須登入才可以使用這個Request
public function authorize()
{
return Auth::check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
//欄位規則
public function rules()
{
return [
// 'phone'=>'required|digits:10',
// 'phone'=>'required|numeric|between:9,11',
'phone'=>'required|regex:/[0-9]{9,11}/',
// 'phone'=>'required|regex:/(01)[0-9]{9}/',
// 'phone'=>'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:10',
'content'=>'required|string|max:100',
];
}
}
在 SMSController 新增一個 nexmosms function
public function nexmosms(AdminSendNexmosmsRequest $request)
{
$phone = '886' . ltrim($request->phone, "0"); //去除掉左邊 0 並加上國碼
$content = $request->content; //訊息內容
try {
Nexmo::message()->send([ //使用 Nexmo 類 傳送 SMS
'to' => $phone, //傳送對象
'from' => env('NEXMO_FROM'), //從哪邊傳來
'text' => $content, //訊息內容
]);
} catch (Exception $e) {
$message = "簡訊傳送失敗";
Session::put('error', $message);
}
$message = "簡訊已傳送給 $request->phone";
Session::put('success', $message);
return view('admin.sms.adminsendSMSform');
}
測試
由於是免費帳號,所以訊息後面被加上 [FREE SMS DEMO, TEST MESSAGE] 字眼