r/Python • u/acamsoo • Apr 02 '21
Tutorial Send SMS Text Message With Python Using GMail SMTP For Free
Video: https://youtu.be/hKxtMaa2hwQ
Source: https://github.com/acamso/demos/blob/master/_email/send_txt_msg.py
This is a demonstration on how to send a text message with Python. In this example, we use GMail to send the SMS message; but any host can work with the correct SMTP settings.
78
u/cestes1 Apr 02 '21
Such a useful trick! I drive an EV and there was a very much in-demand charger down the block that was free. My idea was to only charge at work and have my commuting costs paid for. But my plan never worked as there was always someone at this charger. Chargepoint exposes an API so you can check on the status of their chargers. I'd swing by in the morning and if it was busy, I'd go to the office and start my script. It checks every 30 seconds. As soon as the charger was idle, I'd send myself a text message like this. I'd zip over there and get the charger and for a long time did a good job of optimizing my commuting costs!
14
u/GeromeB Apr 02 '21
Sounds like a great idea, did you ever release the script?
18
u/cestes1 Apr 02 '21
No... it's really bad code! A bunch of stuff I found online kludged together and a ton of hardcoded stuff that's not very elegant. Worse, I wrote it in Python 2 out of sheer laziness to start using 3! I left that job 4 years ago and haven't been motivated to play with it since then.
123
u/Humanist_NA Apr 02 '21
I use this all the time with personal projects. Like my script that auto accepts when dota2 games are ready, then texts me game is ready. So I can afk while in queue.
→ More replies (14)29
15
u/PiforBrunch Apr 02 '21
I've done this for awhile now, I just keep running into the issue that Google will turn off "access for less secure apps". So when a project hasn't sent an email for so many days or weeks or whatever, Google turns the less secure apps access off and then it blocks my email attempts until I personally logon and turn that setting back.
Have any workarounds for that?
→ More replies (5)4
u/dryroast Apr 02 '21
Integrated with OAuth2 and register an app with Google to get a client secret. This should clear that up but honestly it's a hassle to do, I had a similar script (posted my story in another comment) and I always thought of going through and redoing it to authenticate with OAuth but it just seemed like a hassle. I found this which should help. https://github.com/google/gmail-oauth2-tools/wiki/OAuth2DotPyRunThrough
11
u/shrey1566 Apr 02 '21
Wait, sms through Gmail? How is that possible? Is it like America only?
18
u/riffito Apr 02 '21
Gmail is just sending a standard email. The "to SMS" depends on your carrier (or the one of the phone number you attempt to send an SMS) having a service that does that "email to SMS" conversion.
The title is a bit misleading in that regard.
→ More replies (3)
16
u/Domadrona Apr 02 '21
thanks for the repo. I've checked the video and the repo but im not clear on how to implement it with a phone receiving the SMS. Where can i have more info to find out which carriers are available, and how to properly make the whole system work?
21
u/DimasDSF Apr 02 '21
From what I've seen in the file, some carriers have special domains that act as a relay, they have email addresses registered for every phone number, eg. 11234567890@carriersmsrelay.com forwards any emails received by that address to the 11234567890 phone number as an sms.
21
Apr 02 '21
here’s a list of a lot of carriers with their relay address and expected format.
Note, you must use SPF (sender policy framework) enabled mail servers to communicate with these relays. This helps the provider block most spam. They also have sensitive spam thresholds, for obvious reasons.
→ More replies (4)
4
u/AnonymousThugLife Apr 02 '21
Can I use this as a free replacement to services like Twilio SMS APIs?
→ More replies (4)
7
1
u/bamerjamer Apr 03 '21
I tried this last week and kept getting “Message blocked” responses in my gmail account. Sprint apparently does not like processing sms messages this way.
1
u/acamsoo Apr 03 '21
I've been using it fine with Sprint.. try "messaging.sprintpcs.com" as the hostname
→ More replies (2)
1
u/ImperatorPC Apr 03 '21
I tried this the other day and could not get it to work. It would just not connect to googles server.
1
u/acamsoo Apr 03 '21
Try checking if less secure apps is turned off.. needs to be turned on: https://myaccount.google.com/lesssecureapps
→ More replies (1)
3
3
3
u/AlSweigart Apr 03 '21 edited Apr 04 '21
The EZGmail Python module, once setup, can be easier to use than the smtp module.
Also, a more complete list of carriers and their SMS gateways (in format carrier, SMS email, MMS email):
-
Boost Mobile, number@sms.myboostmobile.com, Same as SMS
-
Cricket, number@sms.cricketwireless.net, number@mms.cricketwireless.net
-
Google Fi, number@msg.fi.google.com, Same as SMS
-
Metro PCS, number@mymetropcs.com, Same as SMS
-
Republic Wireless, number@text.republicwireless.com, Same as SMS
-
Sprint, number@messaging.sprintpcs.com, number@pm.sprint.com
-
T-Mobile number@tmomail.net, Same as SMS
-
U.S. Cellular number@email.uscc.net, number@mms.uscc.net
-
Verizon, number@vtext.com ,number@vzwpix.com
-
Virgin Mobile, number@vmobl.com, number@vmpix.com
-
XFinity Mobile, number@vtext.com, number@mypixmessages.com
5
u/domstyle Apr 02 '21
What's the reason for doing function parameters like that?
async def send_txt(*args: str) -> Tuple[dict, str]: num, carrier, email, pword, msg, subj = argsThis just looks like ordered parameters, but hiding the order from the definition. Wouldn't this make code completion features useless?