Full Stack Python logo Full Stack Python

全部主题 | Blog | 时讯 | @fullstackpython | Facebook | 源码 # How to Send MMS Picture Messages with Python Posted by Matt Makai on 五月 15, 2016. Last updated 八月 10, 2016.

Multimedia Message Service (MMS) picture and video messages are a common extension to the Short Message Service (SMS) system for sending text messages. Using a web application programming interface (API) with Python makes it easy to send MMS messages from a web application or script. In this short tutorial we'll learn how to add MMS sending capability to a new or existing Python application.

Tools We Need

Either Python 2 or 3 works for the code in this tutorial. Just make sure you have one of those two versions installed on your system by going to the terminal and typing python --version. The other dependencies for this tutorial include:

If you are unsure of how to get pip and virtualenv installed, take a look at the first few steps of the how to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS guide.

Twilio Web API

Our simple Python example application will use the Twilio web API to send picture messages. Go to the Twilio website sign up for a free trial account. If you already have a Twilio account (and you should because it makes it easy to add almost any type of communications to applications!) then sign into your existing account.

How to Send MMS Picture Messages with Python - 图2

In trial mode Twilio can send MMS to a validated phone number associated with the account. When you're ready to send MMS messages to any phone in any country then you will have to upgrade your account.

After signing up for a Twilio account, you will receive your own phone number that'll be used to send messages. That phone number can send outbound MMS messages without any configuration. It can also receive messages but that requires modifying the Request URL webhook in the phone number details screen.

Installing Our Dependency

We'll use the twilio helper library as a dependency for our Python code. The helper library can be installed via the pip command, which pulls the code from PyPI into our local virtualenv. In this tutorial we'll call our virtualenv pymms but you can name it whatever you want for your application.

We have to create the virtualenv before using it. In your terminal enter:

  1. virtualenv pymms

If you need to install virtualenv take a look at the how to set up Python 3, Django and Green Unicorn on Ubuntu 16.04 LTS guide.

Activate the virtualenv with the source command.

  1. source pymms/bin/activate

The command prompt will change to look like this after it is activated:

How to Send MMS Picture Messages with Python - 图3

Now install the Twilio Python helper library.

  1. pip install twilio

Once the helper library installs we can use it in our Python code.

Sending MMS From Python

Launch the the Python interpreter by executing the python command in your terminal. You can also create a new file named send_mms.py if you want to re-use the code after we give it a try.

We need to grab our account credentials from the Twilio Console to connect our Python code to our Twilio account. Go to the Twilio Console and copy the Account SID and Authentication Token into your Python code.

How to Send MMS Picture Messages with Python - 图4

Enter the following code into the interpreter or into the new Python file.

  1. # we import the Twilio client from the dependency we just installed
  2. from twilio.rest import TwilioRestClient
  3.  
  4. # the following line needs your Twilio Account SID and Auth Token
  5. client = TwilioRestClient("ACxxxxxxxxxxxxxx", "zzzzzzzzzzzzz")
  6.  
  7. # this is the URL to an image file we're going to send in the MMS
  8. media = "http://www.mattmakai.com/source/static/img/work/fsp-logo.png"
  9.  
  10. # change the "from_" number to your Twilio number and the "to" number
  11. # to the phone number you signed up for Twilio with, or upgrade your
  12. # account to send MMS to any phone number that MMS is available
  13. client.messages.create(to="+19732644152", from_="+12023358536",
  14. body="MMS via Python? Nice!", media_url=media)

All the lines above that start with # are comments to give you some context for what each line is doing. After entering that code into the interpreter or running the Python script with python send_mms.py Twilio will send your MMS.

In a few seconds you should see a message appear on your phone - note that MMS can take a little longer because your phone has to download the image. I use an iPhone so here is what the message looked like when I received it:

How to Send MMS Picture Messages with Python - 图5

That is everything need to send MMS to a phone. Pretty awesome result for a few lines of Python code, right? This code can be added to any Python program to send outbound MMS.

One final note: keep your Twilio Auth Token secret otherwise anyone who gets it will be able to send and receive messages through your account.

Questions? Contact me via Twitter @fullstackpython or @mattmakai. I'm also on GitHub with the username mattmakai.

Something wrong with this post? Fork this page's source on GitHub.


#### 在这里注册以便每月能收到一份邮件资料,内容包含本站的主要更新、教程和 Python 书籍的打折码等。

Learn more about these concepts

Twilio and Python logos. Copyright their respective owners. APIs API Integration Twilio …or view all topics.


This site is based on Matt Makai's project Full Stack Python, thanks for his excellent work!

此网站由 @haiiiiiyun开源爱好者们 共同维护。 若发现错误或想贡献,请访问: Github fullstackpython.cn 项目