2021-05-21

Sending text (SMS) messages with python

I got curious about how to send texts and/or emails via python, for possible applications with auto-responder scripts. I already knew there's an easy way to send emails from bash/Linux; and it's not much harder to do similar with python.

In this method, you'll be sending text messages to a phone number(s) by piggy-backing off your email address. (Since I use google email, my instructions here might need to be amended a bit if you use some other email service.) By "piggy-backing," I mean that the "text message" will reach your intended recipient as a text message (what you wanted to happen), but it will appear to them as if it was sent from your email address. This is shown in the example below.

The code is short and simple. Here is it:

import smtplib #REQUIRED FOR SMS.


server = smtplib.SMTP("smtp.gmail.com", 587) #CONNECT YOUR GMAIL TO GOOGLE'S SMTP SERVER.

server.starttls()


server.login('youraddress@gmail.com', 'xxxxxxxxxxxxxxxx') #GMAIL ADDRESS, "APP PASSWORD".


from_email = 'youraddress@gmail.com' #REGULAR OR ALIAS EMAIL ADDRESS.

to = '##########@tmomail.net' #<PHONE_NUMBER>@<SPECIAL_CARRIER_DOMAIN>

body = 'This is not spam. This is Ryan! I sent this text for free from a jupyter notebook using python, piggy-backing off my gmail account! Don\'t text back to this message though; it goes to my email and not my phone.'

subject = ''

message = ("From: %s\r\n" % from_email

+ "To: %s\r\n" % to

+ "Subject: %s\r\n" % subject

+ "\r\n" + body)


server.sendmail(from_email, to, message)

Important notes:

  • You need to create a special Google "app password," (will be 16 characters) which is used in place of the xxxxxxxxxxxxxxxx on the server.login() line. Your regular gmail password does NOT work here.

  • While the server.login() line requires your actual *@gmail.com address, if you have set up an alias email address (e.g. ryan@tryanrogers.com), you can use that alias in the from_email line. The SMS recipient receives the text from whatever from_email you use.

  • The ########## phone number in to = above is simply the 10-digit phone number. In the U.S., I found that the country code was not necessary.

  • Different cellular carriers have their own <SPECIAL_CARRIER_DOMAIN>. Here are the one's I've found:

      • T-mobile: tmomail.net (the one I used in the above code block)

      • Sprint: pm.sprint.com

      • Verizon: vtext.com

      • AT&T: mms.att.net

  • The text will contain "/ no subject /" for either case: using subject = '' or commenting out the + "Subject line of the message = code. (If you know how to get rid of the "/ no subject /" in the text, share it with me!)


If you get it all right, you can expect a python output of something like {}, and the phone you "texted" should get a message that looks like this:

The next thing I wanted to test was sending mass/batch texts. As it turns out, this is just as trivial as you'd hope: use a python "list," and all other code identical as in the above code block:

to = ['##########@tmomail.net', #PHONE NUMBER 1

'##########@tmomail.net', #PHONE NUMBER 2

'##########@tmomail.net' #PHONE NUMBER 3

]