Open In App

Send an SMS Message with Python

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In today’s fastest-growing world, SMS is still a powerful tool by which we can reach billions of users and one can establish a connection globally. In this new world when instant messaging and social media are dominating you can feel our humble SMS outdated but you don’t underestimate its power, it will remain an omnipresent tool for communication even in areas where internet access is limited. Integration of SMS in a Python application will open various exciting possibilities. With the help of Python, we can automate SMS services from automated notifications to two-factor authentication.

Send SMS Using Python

Below are some ways and techniques to send SMS using Python:

  • Twilio API Integration
  • Vonage API Integration

Send SMS Using Twilio API Integration in Python

Below are some of the ways by which we use Twilio API integration to send SMS in Python:

Step 1: Create a Twilio Account

If you don’t have an account, then sign up at Twilio.

CompressJPEGOnline_1_70kb_5400

Twilio sign up windows

Step 2: Get Twilio Credentials

After login you will receive your Account SID and Auth Token from its dashboard.

CompressJPEGOnline_1_70kb_893

Step 3: Get a Twilio Phone Number

Get a Twilio phone number from the Twilio console. This will be the number by which your SMS will be sent.

CompressJPEGOnline_1_70kb_7189

Step 4: Install the Twilio Python Library

Install the Twilio Python library in your system using the following command in your terminal.

pip install twilio

Step 5: Write Python Code (send_sms.py)

Now you have to write this code and replacing directed credential with your Twilio credentials and contacts.

Python




from twilio.rest import Client
 
# in this part you have to replace account_sid
# auth_token, twilio_number, recipient_number with your actual credential
 
account_sid = 'account_sid'
auth_token = 'auth_token'
twilio_number = 'twilio_number'
recipient_number = 'recipient_number'
 
# Create Twilio client
client = Client(account_sid, auth_token)
 
# Send SMS
# in body part you have to write your message
message = client.messages.create(
    body='This is a new message',
    from_=twilio_number,
    to=recipient_number
)
 
print(f"Message sent with SID: {message.sid}")


Step 6: Run the script

Now, save the above python script with extension .py and run it.

python file_name.py

After running successfully, you can check your console for the message SID, and you can verify your Twilio dashboard whether the SMS was sent successfully or not.

image4

output in terminal

Output on Target Device

Screenshot_2024-01-10-18-02-35-333_comandroidmms

output on target device

Video Demonstration

Send SMS in Python Using Vonage API Integration

Below are some of the ways by which we use Vonage API integration to send SMS in Python:

Step 1: Create a Vonage Account

If you don’t have any account, then sign up at Vonage.

image1

sign up window

Step 2: Get Vonage API Key and Secret

After login you will receive your Account SID and Auth Token from its dashboard.

image2

Step 3: Install the Vonage Python Library

Install the Vonage Python library in your system.

pip install vonage

Step 4: Write Python Code (file.py)

Now you have to write this code and replacing directed credential with your Vonage credentials and contacts.

Python




import vonage
 
# Replace with your Vonage API key and secret
api_key = 'your_api_key'
api_secret = 'your_api_secret'
 
# Create a Vonage client
client = vonage.Client(key=api_key, secret=api_secret)
 
# Create a Vonage SMS client
sms = vonage.Sms(client)
 
# Replace with your phone number and the message you want to send
to_number = 'recipient_phone_number'
message = 'This is a message from vonage'
 
# Send SMS
response = sms.send_message({
    'from': 'Vonage API',
    'to': to_number,
    'text': message
})
 
# Check the response
if response['messages'][0]['status'] == '0':
    print(
        f"SMS sent successfully to {to_number}. Message ID: {response['messages'][0]['message-id']}")
else:
    print(
        f"Failed to send SMS. Error: {response['messages'][0]['error-text']}")


Step 5: Run the Script

Now, save the above python script with extension .py and run it.

python file_name.py

After running successfully, you can check your console for the message SID, and you can verify your Twilio dashboard whether the SMS was sent successfully or not.

image3

Output

Screenshot_2024-01-10-19-00-06-708_comandroidmms

Conclusion

In conclusion, we can definitely say that sending SMS with Python or SMS automation can unease true potential of SMS, as one can have global reach, easy integration, enhance user management, easy advertisement with low cost and developer friendly ecosystem.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads