Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to send a text message with python
import smtplib
# 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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Feb 23, 2016

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

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

@elmouden

This comment has been minimized.

Copy link

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

This comment has been minimized.

@russelldavis

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented May 6, 2017

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

This comment has been minimized.

Copy link

commented Jun 19, 2017

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

@sjtindell

This comment has been minimized.

Copy link

commented Jul 11, 2017

Worked for me to an att number, thanks!

@rat395

This comment has been minimized.

Copy link

commented Sep 9, 2017

@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

This comment has been minimized.

Copy link

commented Dec 7, 2017

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

@CRO-THEHACKER

This comment has been minimized.

Copy link

commented May 23, 2018

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

@muthukumarece5

This comment has been minimized.

Copy link

commented Jun 2, 2018

HI ALL,

import smtplib
server = smtplib.SMTP( "smtp.gmail.com", 587 )
server.starttls()
server.login( 'xxxxx@gmail.com', 'xxxxxxxxxx' )
server.sendmail( 'xxxxxxxxx@gmail.com', '9xxxxxxx@tmomail.net', 'jjjjjjjjjjjjjjjjj' )

Am getting below error.
Could you please help me.
The response was:
550 permanent failure for one or more recipients (9xxxxxxxxxx@tmomail.net:blocked)

@SOLO360

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Aug 15, 2018

help

@Omega-C

This comment has been minimized.

Copy link

commented Jan 20, 2019

Is there anyway to recive data back through the same email

@jolllof

This comment has been minimized.

Copy link

commented Apr 1, 2019

Worked for me! Thank you!

@gnahum12345

This comment has been minimized.

Copy link

commented Apr 4, 2019

@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)

@elegantcha0s

This comment has been minimized.

Copy link

commented Aug 18, 2019

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,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.