Skip to main content

Python library for sending SMS messages via email-to-SMS gateways

Project description

smslib

A very simple library for sending very simple text messages via SMS-to-email gateways

I often run code on a remote computer cluster, and sometimes this code takes a very long time to run. This project was inspired by a desire to receive a notification on my phone when my programs wrap up, without using a fancy API, installing a third-party messaging program, and (most importantly) paying money. After all, I already pay to send and recieve texts, and email is free these days. Most common US phone providers have a way to send texts via an email. For example, if your phone number is +1-(123)-456-7890, and your service provider is AT&T, you can, right now, send yourself a text message by emailing 1234567890@txt.att.net. This is the core idea behind the library.

Installation

This package is available via PyPI:

$ pip install smslib

Alternatively, clone the repo and install it manually:

$ git clone git@github.com:denehoffman/smslib.git
$ cd smslib
$ pip install .

Usage

In a python script

There are several ways to use the package. It contains three submodules, sender, receiver, and notify. To manually use the library inside python, ignore the notify submodule and write something like the following:

import smslib
sender = smslib.Sender("my_email@gmail.com", "mypassword", "smtp.gmail.com")
receiver = smslib.Receiver("123-456-7890", "at&t")
sender.send_message("Hello world!", receiver)

That's pretty much all of the core functionality. Note that for GMail, if you have 2FA turned on, you might need to create an app password. If everything works correctly, you should get a text message that says "Hello world"! The package uses smtplib's SMTP.send_message function (under TLS encryption), so feel free to use an email.message.Message object instead of a string. There is also the option to send a regular email instead of a text (or maybe it's a text to a phone provider that isn't in the default list) by specifying an email address in to the Receiver:

receiver = smslib.Receiver(None, None, email="my.friends.email@university.edu")

You can also specify an ssl.SSLContext using a context keyword argument in send_message. Additionally, a port can be specified using the port keyword argument in smslib.Sender, although the default, 587, should be sufficient for most use cases.

In a shell script

The other way to use the library is through a simple command-line interface. This gets built automatically when installed, and the command is called notify. I realize this has the potential to conflict with some other programs, which is why I would advise the use of a virtual python environment.

$ notify --help
usage: notify [-h] [--sender SENDER] [--receiver RECEIVER] [--email EMAIL] [--password PASSWORD]
              [--smtp-server SMTP_SERVER] [--port PORT] [--phone-number PHONE_NUMBER]
              [--provider PROVIDER] [--confirm]
              message

Send an SMS via email Available SMS providers: verizon, at&t, att, tmobile, sprint, boost

positional arguments:
  message               Message string to send

optional arguments:
  -h, --help            show this help message and exit
  --sender SENDER       Sender name from ~/.smslibrc.toml
  --receiver RECEIVER   Receiver name from ~/.smslibrc.toml
  --email EMAIL         Sender's email address
  --password PASSWORD   Sender's password
  --smtp-server SMTP_SERVER
                        Sender's SMTP server
  --port PORT           Sender's SMTP server port
  --phone-number PHONE_NUMBER
                        Receiver's phone number
  --provider PROVIDER   Receiver's phone provider
  --confirm             Print confirmation after sending message

This contains all of the fields above (note that --port is completely optional and has a default value of 587) with two additional arguments, --sender and --receiver. I set this up to cut down on the hastle of entering in all the information in a long shell command every time. By default, it will look for a TOML file located in your home directory. An example is as follows:

# ~/.smslibrc.toml

[senders]
[senders.myself]
email = "my.email@gmail.com"
password = "mypassword"
smtp_server = "smtp.gmail.com"
port = 587

[receivers]
[receivers.myfriend]
phone_number = "123-456-7890"
provider = "att"

Then we can just call the notify command as

$ notify "Hello world!" --sender myself --receiver myfriend

This file can be located anywhere, and the default location can be overwritten by setting the $SMSLIBRC environment variable to point to the desired path. Any command-line arguments here will have precedence over the .smslibrc.toml file. For example

$ notify "Hello world!" --sender myself --receiver myfriend --port 123

will send the message over port 123 rather than the 587 specified in the file. Furthermore, while there was an option to send a message to any custom email address via the email keyword in smslib.Receiver, there is no command-line option for this. However, adding an email field to the corresponding reciever in .smslibrc.toml will give you this functionality in the shell.

Finally, any fields which are not specified explicitly will be acquired by prompting the user in shell. All of these are raw input fields, with the exception of the sender's password, which uses getpass to hide the password input. It is usually not the best practice to store your passwords in code or in a configuration file, but I allow this as an option because GMail's "app passwords" are randomly generated strings which are not easy to type or remember.

Changelog

  • v1.0.0
    • Initial commit with base functionality

Planned Features

None so far, but I'm sure I'll think of a few things. If anyone reading this has suggestions, let me know!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smslib-1.0.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

smslib-1.0.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file smslib-1.0.0.tar.gz.

File metadata

  • Download URL: smslib-1.0.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.7

File hashes

Hashes for smslib-1.0.0.tar.gz
Algorithm Hash digest
SHA256 90b9b529caa0e64a314b6a62b43ddcb930996c4a7502aed421898a3a22e9b453
MD5 35246ef4c0a46919cc9105f99151fa57
BLAKE2b-256 25343b372a78952b58069b1b960d5815dbc39243e86fa9e5fa20e46e2d3d745c

See more details on using hashes here.

File details

Details for the file smslib-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: smslib-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.7

File hashes

Hashes for smslib-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 00bbefafebf03b55467872c844186cec7ae8145d1b29adcda4c25c5fef733d3f
MD5 0c5571f8b63e9bf3c21c6be239df8f8e
BLAKE2b-256 02a7571bdc9ca7f1b340d54656c64e342197402392de67873086795a9baec58b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page