How to send emails and texts for free with Python

Outline

You may be asking yourself, "Isaac, why not just send an email from Gmail? Why from Python?" and that is a good question to ask. There are a few use cases where you may want to send emails from Python vs. the browser. First, you may have a script that runs regularly in the background (scraping, file monitoring, etc...) and you need to be notified if that script fails -or- an event occurs. Secondly, you may have some service that needs to send notifications to several users in an automated fashion and while there are several other paid services out there such as SendGrid and Postmark, the SMTP library method is free and capable of normal daily updates.

Setup

What you need

  1. A Gmail account with Less Secured App access enabled. Go here to enable it on your Gmail profile
  2. Python
  3. The Email and Messenger classes found here
  4. Someone to send an email to

Getting Started

We will be using your Gmail account as our SMTP provider for this demonstration. Other providers such as Outlook will also work but you will need to change the line smtplib.SMTP("smtp.gmail.com",587) to match your desired host. Alright, once you have your Gmail account set up, check here to make sure Less Secure App access has been enabled. If so you should be presented with the following screen.

Less Secure App Access

Adding the email/texting file to your project

I created three classes to help simplify sending emails and text messages from Python. They are:

  1. Email
  2. SMS
  3. Messenger

You will want to go here to download the source code and then place it in a file called PyMessenger.py. Drop this file in your favorite working directory. Next, in the same directory, create another file called run.py where we will right our example usage code.

You should now have a file structure like this:

<your favorite working directory>
- PyMessenger.py
- run.py

Sending Emails

Set up

Let's get started by sending our very first email to ourselves! in the run.py file we want to import the Email and Messenger classes mentioned above from our PyMessenger.py.

from PyMessenger import Email, SMS, Messenger

Next, we will want to create a connection to the SMTP library using the Messenger class.

my_messenger = Messenger(<your gmail username>, <your gmail password>)

Finally, we build our message and send it. Start by creating an instance of the Email class and then use the Messenger class to send it.

# Build the message
to      = '<your friends email address>'
subject = 'test subject'
body    = 'Hello World!'
msg     = Email(to, subject, body, is_HTML=False)

# Send the message
my_messenger.send_email(msg, one_time=True)

Run

To run, open your favorite terminal console and navigate to the run.py file. In the console type python run.py for Windows and python3 run.py for Linux. After running you should receive an email like:

The result

Sending Text Messages

Set up

The setup for sending text messages is very similar to sending emails. We will import the SMS class instead of the Email class though.

from PyMessenger import Email, SMS, Messenger

my_messenger = Messenger(<your gmail username>, <your gmail password>)

Next, we will build our text message to send. To send a text message using the SMTP library, we actually send an email to what is called a gateway. Each network provider has its own gateway so all you need to do is match up the network provider of the number you wish to send a message to and the respective gateway from the table below.

Provider Gateway
AT&T @txt.att.net
Sprint @messaging.sprintpcs.com or @pm .sprint.com
T-Mobile @tmomail.net
Verizon @vtext.com
Boost Mobile @myboostmobile.com
Cricket @sms.mycricket.com
Metro PCS @mymetropcs.com
Tracfone @mmst5.tracfone.com
U.S. Cellular @email.uscc.net
Virgin Mobile @vmobl.com
# Build the message
number    = '12222222222' # Example US phone number
gateway   = '@messaging.sprintpcs.com' # For a Sprint number
subject   = 'test subject'
body      = 'Hello World!\n'
msg       = SMS(number, gateway, subject, body)

# Send the message
my_messenger.send_sms(msg, one_time=True)

Run

To run, like above, open your favorite terminal console and navigate to the run.py file. In the console type python run.py for Windows and python3 run.py for Linux. After running you should receive a text message like:

The result

Conclusion

Through this post, we have learned how to send Emails and Text Messages for free using Python and a Gmail account. Add this to your project for easy and free notifications or read the PyMessenger docs on how to send more messages at once and broadcast emails for a group. If you like this, leave a comment on your use case and share how you integrated emails and texting into your Python project!

Comments

Login to Add comments.