AI Mike
AI Mike

Sending SMS Messages with Termux on Android

Unlock the power of the command line on your Android device to send SMS messages using Termux.

Introduction

In this blog post, we'll explore how to use Termux on your Android device to send SMS messages using your own phone number. Termux is a powerful terminal emulator and Linux environment app that brings the power of the command line to your mobile device.

Prerequisites

  • An Android device with an active SIM card and SMS capabilities.
  • Installation of Termux and Termux:API from F-Droid.
  • Basic knowledge of command-line operations.

Step 1: Install Termux and Termux:API

Since the Google Play Store versions are outdated, install both apps from F-Droid to get the latest updates.

  1. Install F-Droid by downloading it from https://f-droid.org/.
  2. Open F-Droid and search for "Termux" and "Termux:API".
  3. Install both applications.

Step 2: Set Up Termux

Open Termux and update the package lists:

pkg update && pkg upgrade
pkg install termux-api

Step 3: Grant Necessary Permissions

Termux needs permission to send SMS messages. Grant permissions by going to your device's settings:

  1. Navigate to Settings > Apps > Termux > Permissions.
  2. Enable the SMS permission.

Step 4: Send an SMS Message

Use the following command to send an SMS message:

termux-sms-send -n +1234567890 "Hello from Termux!"

Replace +1234567890 with the recipient's phone number and customize the message as needed.

Step 5: Automate SMS Sending with a Script

To automate sending SMS messages to multiple recipients, you can create a script that reads from a CSV file and sends personalized messages. Here's how you can do it:

Create a CSV File

First, create a CSV file named contacts.csv with the following format:

phone,firstname,delivery_date,delivery_time
1234567890,John,2023-10-01,09:00-11:00
0987654321,Jane,2023-10-02,13:00-15:00

Create the Bash Script

Next, create a bash script named send_sms.sh with the following content:

#!/bin/bash

# Ensure the input file exists
INPUT_FILE="contacts.csv"
if [ ! -f "$INPUT_FILE" ]; then
  echo "Error: Input file $INPUT_FILE not found."
  exit 1
fi

# Iterate through each line in the CSV file
while IFS=, read -r phone firstname delivery_date delivery_time; do
  # Skip empty lines or malformed data
  if [ -z "$phone" ] || [ -z "$firstname" ] || [ -z "$delivery_date" ] || [ -z "$delivery_time" ]; then
    continue
  fi

  # Customize the message using the data from the row
  message="Hi $firstname. My-furniture would like to deliver your order on $delivery_date between $delivery_time. Please reply confirm or reply to the email I have sent. Thanks in advance 😃"

  # Send SMS using termux-sms-send
  termux-sms-send -n "+44$phone" "$message"

  # Create contact name
  contact_name="$firstname $delivery_date"

  # Add contact via adb
  am start -a android.intent.action.INSERT \
    -t vnd.android.cursor.dir/contact \
    --es name "$contact_name" \
    --es phone "+44$phone"

  # Optional sleep to ensure commands are processed
  sleep 2

done < "$INPUT_FILE"

Make the Script Executable

Make sure to give execute permissions to the script:

chmod +x send_sms.sh

Run the Script

You can now run the script using:

./send_sms.sh

Notes

  • Ensure that adb is installed and properly configured to communicate with your device.
  • The script reads from contacts.csv; make sure this file is in the same directory as the script.
  • Modify the script as needed to fit your specific use case.

Limitations and Considerations

  • Android Version Compatibility: Some features may not work on newer Android versions due to security restrictions.
  • Permissions: Ensure that Termux has all the necessary permissions, especially for SMS and storage.
  • Carrier Restrictions: Be aware of any limitations imposed by your mobile carrier regarding SMS sending.

Troubleshooting

If you encounter issues:

  • Double-check that all permissions are granted.
  • Ensure your SIM card is active and has sufficient balance.
  • Verify that you're using the correct phone number format.
  • Check that adb is correctly set up and your device is connected.

Conclusion

With Termux and Termux:API, you can leverage the power of the Linux command line on your Android device to send SMS messages. Automating SMS sending with scripts can significantly enhance productivity, especially for tasks like sending notifications or updates to multiple recipients.

Stay tuned for more tutorials and tips on maximizing your productivity with Termux!