How to send text messages with Python for Free

This week, I am going to be showing you how to send text messages with Python for free. It’s actually surprisingly easy to do and I thought it could be useful to share this with all of you!

Advertisements

What you’ll need

For this post, I’ll be using the following:

It should be noted that if you’re using gmail, like me, you’ll need to setup and use an application password with your account. You can read more information and security warnings on that here: https://support.google.com/accounts/answer/185833?p=InvalidSecondFactor&visit_id=637700239874464736-1954441174&rd=1

Advertisements

Code

import smtplib
import sys
CARRIERS = {
"att": "@mms.att.net",
"tmobile": "@tmomail.net",
"verizon": "@vtext.com",
"sprint": "@messaging.sprintpcs.com"
}
EMAIL = "EMAIL"
PASSWORD = "PASSWORD"
def send_message(phone_number, carrier, message):
recipient = phone_number + CARRIERS[carrier]
auth = (EMAIL, PASSWORD)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(auth[0], auth[1])
server.sendmail(auth[0], recipient, message)
if __name__ == "__main__":
if len(sys.argv) < 4:
print(f"Usage: python3 {sys.argv[0]} <PHONE_NUMBER> <CARRIER> <MESSAGE>")
sys.exit(0)
phone_number = sys.argv[1]
carrier = sys.argv[2]
message = sys.argv[3]
send_message(phone_number, carrier, message)
Advertisements

Running Code

If you copied my code sample above, you can run this script from any terminal within the same directory as this file with:

python3 PythonTextMessage.py 1234567891 att "This is a sample text message"

Of course you’ll want to specify the carrier that you actually use in order for this to work.

Advertisements

Conclusion

This week’s blog post was short and sweet, but it’s something that I use pretty often. Sometimes I use this method to send me text messages whenever I need a project to notify me of anything. Whenever I do this, I typically use a throwaway email because I don’t want to open application passwords on any account I might consider to be important.

Hopefully this neat trick is as useful for you as it is for me!

As always, if you liked what you read and you’d like to support me, please consider buying me some coffee! Every cup of coffee sent my way helps me stay awake after my full-time job so that I can produce more high quality blog posts! Any and all support would be greatly appreciated!

Ballad Serial — Trans Arsonist Art Network
Advertisement

3 thoughts on “How to send text messages with Python for Free

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s