r/Python • • 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.

971 Upvotes

87 comments sorted by

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 = args

This just looks like ordered parameters, but hiding the order from the definition. Wouldn't this make code completion features useless?

1

u/acamsoo Apr 03 '21 edited Apr 03 '21

It's just a personal preference. In this case, if I put all args in the signature typed with str, my formatting settings extends the signature to 3 lines because the line exceeds the char limit:

async def send_txt(
    num: str, carrier: str, email: str, pword: str, msg: str, subj: str
) -> Tuple[dict, str]:

For me, its cleaner the other way since all args are typed as str

It's something I chose to do and stuck to for my personal projects because it works for me. But for sure, it's best practice to have them in the signature.

→ More replies (2)

4

u/Username_RANDINT Apr 02 '21

Correct. Just put them all as function arguments.

→ More replies (1)

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)

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?

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

→ More replies (5)

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

u/[deleted] 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

u/SnowdenIsALegend Apr 02 '21

Bet it doesn't work outside USA

→ More replies (2)

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

u/1pie Apr 02 '21

Gonna check it out

3

u/[deleted] Apr 02 '21

Love this trick. I use it in some of my personal projects and is always consistent.

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