You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Django集成Twilio API短信通知:仅能向自身号码发送短信问题咨询

Troubleshooting Twilio SMS Issue in Django: Can't Send to Numbers Other Than Your Own

Hey David, sorry to hear you're hitting this roadblock with your Twilio-Django integration. Let's walk through the most likely causes and fixes for why you can only send SMS to your own number:

1. Twilio Trial Account Restrictions (Most Common!)

If you're using a free Twilio trial account, this is almost certainly the issue. Trial accounts have a strict limitation: you can only send SMS to phone numbers that you've manually verified in your Twilio Console.

To fix this:

  • Log into your Twilio Dashboard
  • Navigate to Phone Numbers > Verified Caller IDs
  • Add the target phone number(s) you want to send messages to—Twilio will send a verification code to that number to confirm ownership.

Once verified, you should be able to send messages to those numbers. If you need to send to arbitrary numbers, you'll need to upgrade to a paid Twilio account.

2. Invalid Phone Number Format

Twilio requires all phone numbers to use the E.164 standard format (e.g., +12025551234 for a US number, +447911123456 for UK). If you're passing numbers without the country code or with extra characters (like spaces, hyphens), Twilio will reject them.

In your Django code, make sure you're formatting the recipient number correctly. You can add a quick validation step or use Twilio's built-in helpers:

from twilio.base.exceptions import TwilioRestException

def format_phone_number(number):
    # Remove non-digit characters and prepend country code if missing
    cleaned = ''.join(filter(str.isdigit, number))
    if not cleaned.startswith('+'):
        # Replace with your default country code if needed (e.g., +1 for US)
        cleaned = f'+1{cleaned}'
    return cleaned

# Use it in your send function with error handling
try:
    formatted_to = format_phone_number(recipient_number)
    message = client.messages.create(body="Test message", from_=settings.TWILIO_FROM_NUMBER, to=formatted_to)
except TwilioRestException as e:
    print(f"Twilio Error: {e.msg}")

3. Accidentally Hardcoded Recipient in Code

Double-check your Django code to make sure you aren't leaving a test value (your own number) hardcoded as the recipient. It's an easy mistake to make! For example:

# ❌ Bad: Hardcoded recipient
message = client.messages.create(
    body="Hello",
    from_=settings.TWILIO_FROM_NUMBER,
    to="+15551234567"  # Your own number
)

Instead, use a dynamic variable that accepts the intended recipient number:

# ✅ Good: Dynamic recipient
def send_sms(to_number, message_body):
    client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
    return client.messages.create(
        body=message_body,
        from_=settings.TWILIO_FROM_NUMBER,
        to=to_number
    )

4. Check Twilio Error Logs

If none of the above fixes work, head to your Twilio Console's Monitor > Logs > Messages page. Here you'll see detailed error messages for every failed SMS attempt. Common errors include:

  • Invalid To Number: The number format is wrong or not verified
  • Permission Denied: Your account doesn't have access to send to that country/region
  • Authentication Error: Your Twilio Account SID or Auth Token is incorrect

These logs will point you directly to the root cause.

Start with checking the trial account restrictions—this is the #1 reason for this specific issue. Let me know if you hit any other snags!

内容的提问来源于stack exchange,提问作者David KG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 21:22:47