20SENDING EMAIL, TEXTS, AND PUSH NOTIFICATIONS

At this point, you’ve written programs to work with strings and text pattern recognition, but you’ve been limited to accessing text data on your computer. You can also send text to other people through email, SMS, and push notifications. Python’s rich ecosystem of packages has third-party libraries for all of these features.

A simple drawing of a light bulb. LEARNING OBJECTIVES

  • Be able to log in to your Gmail account using Python to search, read, and send email.
  • Use code to make and download attachments to your email.
  • Send SMS messages from Python scripts by using SMS email gateways, while understanding their limitations.
  • Send and receive push notifications that you can view on your smartphone.

A grey circle with a white question mark at the center Practice Questions

These questions test your understanding of the EZGmail package and ntfy service that your internet-connected computer can use to communicate across the globe.

The Gmail API

Gmail owns close to one-third of the email client market share, and you’ve most likely had at least one Gmail email address. Because of Gmail’s additional security and antispam measures, controlling a Gmail account is best done through the ezgmail module rather than through the smtplib and imaplib modules in Python’s standard library.

  1. 1. What should you do if you accidentally share the credentials or token files for the Gmail API?

  2. 2. What variable in the ezgmail module contains the email address from which you’re sending email?

  3. 3. What does this function call do: ezgmail.send('[email protected]', 'Hello!', 'Here is that graduation photo.', ['grad.jpg'])?

  4. 4. What data type represents a single received email?

  5. 5. What data type represents a series of back-and-forth emails?

  6. 6. Name two attributes of a GmailMessage object.

  7. 7. What function call returns the 50 most recent email threads?

  8. 8. What function call returns email messages that mention “cake recipes”?

  9. 9. What does ezgmail.search('from:[email protected]') return?

  10. 10. What does ezgmail.summary(ezgmail.unread()) do?

  11. 11. If the variable spam contains a GmailMessage object, what code downloads all of the file attachments in that email?

  12. 12. What happens if you download an attachment that has the same filename as a file on your computer?

SMS Email Gateways

People are more likely to be near their smartphones than their computers, so text messages are often a more reliable way of sending immediate notifications than email. Also, text messages are usually shorter, making it more likely that a person will get around to reading them. The easiest, though not most reliable, way to send text messages is by using an SMS email gateway, an email server that a cell phone provider has set up to receive texts via email and then forward them to the recipient as text messages.

  1. 13. What does SMS stand for?

  2. 14. What does MMS stand for?

  3. 15. Can your Python program send text messages by sending email to an SMS email gateway?

  4. 16. Can your Python program receive text messages by receiving email from an SMS email gateway?

  5. 17. What information besides the phone number do you need to send a text through an SMS email gateway?

  6. 18. How much do SMS email gateways cost to use?

  7. 19. What are two disadvantages of using SMS email gateways instead of a dedicated telecommunications API?

Push Notifications

HTTP pub-sub notification services allow you to send and receive short, disposable messages over the internet via HTTP web requests. Installing the ntfy app on your mobile phone allows you to receive these notifications. This open source app can be found in the app stores for Android and iPhone. You can also receive notifications in your web browser by going to https://ntfy.sh/app. Your Python programs can send push notifications using the Requests package covered in Chapter 13 of this workbook.

  1. 20. What module can you use for interacting with the ntfy service?

  2. 21. How can you receive ntfy notifications on your smartphone?

  3. 22. Can you receive ntfy notifications on your laptop without using Python?

  4. 23. What protocol does the ntfy service use to send and receive push notifications?

  5. 24. What function do you use to send a push notification to the ntfy service?

  6. 25. How much does it cost to use the ntfy service?

  7. 26. What are the lowest and highest priority levels for ntfy messages?

  8. 27. What does this code do: requests.post('https://ntfy.sh/hello', 'goodbye')?

  9. 28. What can keyword arguments for the headers parameter in requests.post() do?

A simple drawing of a sharpened pencil. Practice Projects

Automate sending “quote of the day” messages with the following projects.

“Quote of the Day” Email

Create a text file named qotd.txt that has one inspirational or memorable quote per line. Then, create a program that reads this file and randomly selects one quote to send in an email message. Place the recipient’s email address in a variable named RECIPIENT. The program should also print the email recipient and the quote sent, and maintain a text file named qotdLastSent.txt with the last date on which an email was sent.

If it already sent an email today, the program should print “Email already sent today. Exiting . . .” and quit. You can get a string of the current date by calling str(datetime.date.today()).

Save this program in a file named qotdEmail.py.

“Quote of the Day” Push Notification

This project is the same as the previous project, except it should send the quote as an ntfy notification. The RECIPIENT variable should be a string of the topic to which the quote was sent. Unlike with the email version, anyone listening to the ntfy topic can receive this message, so multiple people can enjoy the quote.

Save this program in a file named qotdPush.py.