Skip to content

Instantly share code, notes, and snippets.

@alexle
Last active December 1, 2023 01:01
Show Gist options
  • Save alexle/1294495 to your computer and use it in GitHub Desktop.
Save alexle/1294495 to your computer and use it in GitHub Desktop.
How to send a text message with python
# sms.py
# Sends sms message to any cell phone using gmail smtp gateway
# Written by Alex Le
import smtplib
# Use sms gateway provided by mobile carrier:
# at&t: number@mms.att.net
# t-mobile: number@tmomail.net
# verizon: number@vtext.com
# sprint: number@page.nextel.com
# Establish a secure session with gmail's outgoing SMTP server using your gmail account
server = smtplib.SMTP( "smtp.gmail.com", 587 )
server.starttls()
server.login( '<gmail_address>', '<gmail_password>' )
# Send text message through SMS gateway of destination number
server.sendmail( '<from>', '<number>@tmomail.net', '<msg>' )
@jolllof
Copy link

jolllof commented Apr 1, 2019

Worked for me! Thank you!

@gnahum12345
Copy link

@muthukumarece5 I was getting the same error! This is what fixed it for me.
T-Mobile likes it if it is in an email type format. Here is the T-Mobile help form (https://support.t-mobile.com/thread/141399)
The following code worked for me

import smtplib
server = smtplib.SMTP( "smtp.gmail.com", 587 )
server.starttls()
server.login( 'xxxxx@gmail.com', 'xxxxxxxxxx' )
from_mail = 'xxxxxxxxx@gmail.com'
to = '9xxxxxxx@tmomail.net'
body = '<body>'
message = ("From: %s\r\n" % from_mail + "To: %s\r\n" % to + "Subject: %s\r\n" % '' + "\r\n" + body)
server.sendmail(from_mail, to, message)

@edvinmolla
Copy link

I tried this method. Google blocked my sign in attempt because the app(smtp, I think) "doesn't meet modern security standards."

Is there a workaround for this?

You have to enable less secure devices in your Google account,

@Triobro3
Copy link

Triobro3 commented Jul 1, 2020

I used this method and It worked great! Is there any way though that I can get the script to respond to a message in the conversation?

@acamso
Copy link

acamso commented Apr 2, 2021

For those interested, here is an implementation with multiple carriers + and async support: https://bit.ly/send-txt-msg

@mcmullinboy15
Copy link

Do you guys know how to remove the subject when you send the text? Like the forward slashes? I just get / no subject / when I leave it blank

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment