---
# System prepended metadata

title: '[PYTHON] 簡訊發送 TWSMS'
tags: [PYTHON, SMS, TWSMS, 簡訊]

---

# PYTHON 簡訊發送 TWSMS

簡訊程式執行時遇到IP權限問題，要先到TWSMS內做設定。

![](https://i.imgur.com/HDAZGaj.png)

1. 把發送簡訊的電腦加入twsms的信任ip名單中(初次登入會跳出視窗，收驗證碼完成驗證即可)
![](https://i.imgur.com/vYtiq0x.png)

2. 將電腦的IP加到API來源IP設定裡。
![](https://i.imgur.com/PrEM6Ma.png)


**TWSMS的API只支援單筆簡訊發送，要傳給多人需寫成function，再用迴圈執行。**

# sendSMS<span/>.py
```python=
# -*- coding: utf-8 -*-

import urllib.request

def sendSMS(tel):
    username = "twsms帳號"
    password = "twsms密碼"
    mobile = tel
    message = "SMS_test"

    msg = 'username='+username+'&password='+password+'&mobile='+mobile+'&message='+message
    url = 'http://api.twsms.com/json/sms_send.php?'+msg


    resp = urllib.request.urlopen(url)
    print (resp.read())


phone=["電話號碼1","電話號碼2"]
for i in phone:
    sendSMS(i)
```

###### tags: `python` `SMS` `簡訊` `TWSMS`