DEV Community


Posted on

Sending SMS using Python

Whenever we talk about sending an SMS to our phone using any programming language the first thing that comes into our head is Twilio SMS library.But in this post we are going to avoid twilio and use python builtin module SMTPLIB I know it sounds crazy since SMTPLIB is used to send emails but we can also send text messages using that module. In order to send text message to your phone all you need to know is sms-gateway. So every carrier has their own sms-gateways and you can read more about it here SMS GATEWAYS.

SMS Gateways for each Carrier

  • AT&T: [number]
  • Sprint: [number] or [number]
  • T-Mobile: [number]
  • Verizon: [number]
  • Boost Mobile: [number]
  • Cricket: [number]
  • Metro PCS: [number]
  • Tracfone: [number]
  • U.S. Cellular: [number]
  • Virgin Mobile: [number]

So lets begin writing the script

import smtplib 
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

email = "Your Email"
pas = "Your Pass"

sms_gateway = ''
# The server we use to send emails in our case it will be gmail but every email provider has a different smtp 
# and port is also provided by the email provider.
smtp = "" 
port = 587
# This will start our email server
server = smtplib.SMTP(smtp,port)
# Starting the server
# Now we need to login

# Now we use the MIME module to structure our message.
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = sms_gateway
# Make sure you add a new line in the subject
msg['Subject'] = "You can insert anything\n"
# Make sure you also add new lines to your body
body = "You can insert message here\n"
# and then attach that body furthermore you can also send html content.
msg.attach(MIMEText(body, 'plain'))

sms = msg.as_string()


# lastly quit the server
Enter fullscreen mode Exit fullscreen mode

So this was our simple python script that will allow you to send text messages to your phone. I hope you enjoyed this post if you think i missed any thing feel free to dm me on twitter

Furthermore, here's the python module that I wrote that allows you to send text messages without twilio.
Checkout my Website

Top comments (0)