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: [email protected]
# t-mobile: [email protected]
# verizon: [email protected]
# sprint: [email protected]
# 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>' )
@bturnip
Copy link

bturnip commented Jan 5, 2016

Thanks- this worked for me. Notes:

  • Sprint sms gateway for me in Little Rock, AR is "@pm.sprint.com".
  • I had to set up an app specific password to send via Gmail, see Google support notes.

@mc4
Copy link

mc4 commented Feb 22, 2016

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?

@ideabrian
Copy link

@mc4 - you'll probably want to use the Google APIs

https://support.google.com/cloud/answer/6158857?hl=en

@elmouden
Copy link

elmouden commented Apr 1, 2016

I don't know why I have not received a text message, even I used correctly your methods.
On my gmail account, I see that a text message was sent to my phone number but I didn't receive anything.
I'm living in France, so my phone number starts by 0033 or +33.
Thank you in advance,

@Zemaaan
Copy link

Zemaaan commented May 5, 2016

@russelldavis
Copy link

russelldavis commented May 17, 2016

@elmouden I ran into the same problem. I was able to fix it by adding headers to the email, e.g.:

    message = ("From: %s\r\n" % from
             + "To: %s\r\n" % to
             + "Subject: %s\r\n" % subject
             + "\r\n"
             + body)
    server.sendmail(from, to, message)

UPDATE: This fix seemed to help, but tmobile still seems to randomly drop messages. I'd recommend switching carriers or using a service like https://www.tropo.com instead.

@thegnnu
Copy link

thegnnu commented Jul 9, 2016

I have just tried this code and get a error in Python 2 or 3 with Jessie on Raspberry Pi
It reads
File "/root/analogzero/email.py", line 14, in
server = smtplib.SMTP( "smtp.gmail.com", 587 )
AttributeError: 'module' object has no attribute 'SMTP'

Any advise for me please

@vinnuvlsm
Copy link

the above Says that you donot have smptlib module imported for confirmation just do this:
import smtplib

and try compiling or running the code, if you get error then you go to fix that,

@keeganjk
Copy link

What's twomail.net? My phone isn't receiving an SMS...

@sjtindell
Copy link

Worked for me to an att number, thanks!

@juliosandino
Copy link

@keeganjk "@tmomail.net" is the domain ("sms gateway") that T-mobile uses for sending sms from email. This is the domain you would use when sending an sms to a T-mobile phone.

Here are some other popular domains you would use for other carriers (ie. Sprint)

  • Sprint:@pm.sprint.com
  • Verizon:@vtext.com
  • AT&T:@mms.att.net

Shoutout to @bturnip for making clear that different carriers have different sms gateways.

@rockerx108
Copy link

Have you guys had any issues with google blocking the sent message?

@CRO-THEHACKER
Copy link

Hey, I am getting the MSG but, Am getting a black line instead of the MSG.

@muthukumarece5
Copy link

muthukumarece5 commented Jun 2, 2018

HI ALL,

import smtplib
server = smtplib.SMTP( "smtp.gmail.com", 587 )
server.starttls()
server.login( '[email protected]', 'xxxxxxxxxx' )
server.sendmail( '[email protected]', '[email protected]', 'jjjjjjjjjjjjjjjjj' )

Am getting below error.
Could you please help me.
The response was:
550 permanent failure for one or more recipients ([email protected]:blocked)

@SOLO360
Copy link

SOLO360 commented Aug 15, 2018

Traceback (most recent call last):
File "C:/Users/solo/AppData/Local/Programs/Python/Python37-32/Lib/idlelib/idle_test/caluculator.py", line 4, in
server = smtplib.SMTP( "smtp.gmail.com", 587 )
File "C:\Users\solo\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 251, in init
(code, msg) = self.connect(host, port)
File "C:\Users\solo\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\solo\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\Users\solo\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 727, in create_connection
raise err
File "C:\Users\solo\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 716, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

@SOLO360
Copy link

SOLO360 commented Aug 15, 2018

help

@Omega-C
Copy link

Omega-C commented Jan 20, 2019

Is there anyway to recive data back through the same email

@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( '[email protected]', 'xxxxxxxxxx' )
from_mail = '[email protected]'
to = '[email protected]'
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