Skip to content

Commit 114356b

Browse files
committed
adding new MMS post
1 parent da875bf commit 114356b

4 files changed

Lines changed: 151 additions & 3 deletions

File tree

source/content/posts/160511-send-sms-text-message-python.markdown

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,9 @@ The helper library is now installed and we can use it with the Python code
8585
we create and execute.
8686

8787

88-
## Sending An SMS From Python
88+
## Sending SMS From Python
8989
Fire up the Python interpreter in the terminal using the `python` command,
90-
or create a new file named `send_sms.py`. Enter the following code into
91-
the interpreter or into the new Python file.
90+
or create a new file named `send_sms.py`.
9291

9392
We need to grab our account credentials from the Twilio Console to connect
9493
our Python code to our Twilio account. Go to the
@@ -97,6 +96,8 @@ and Authentication Token into your Python code.
9796

9897
<img src="/source/static/img/160511-send-sms-python/console-tokens.png" width="100%" class="technical-diagram img-rounded">
9998

99+
Enter the following code into the interpreter or into the new Python file.
100+
100101
# we import the Twilio client from the dependency we just installed
101102
from twilio.rest import TwilioRestClient
102103

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
title: How to Send MMS Picture Messages with Python
2+
slug: send-mms-picture-messages-python
3+
meta: A tutorial on how to send MMS (picture multimedia messages) using the Python programming language.
4+
category: post
5+
date: 2016-05-15
6+
7+
8+
Multimedia Message Service (MMS) picture and video messages are a common
9+
extension to the Short Message Service (SMS) system for sending text
10+
messages. Using a
11+
[web application programming interface (API)](/application-programming-interfaces.html)
12+
with Python makes it easy to send MMS messages from a web application or
13+
script. In this short tutorial we'll learn how to add MMS sending capability
14+
to a new or existing Python application.
15+
16+
17+
## Tools We Need
18+
Either [Python 2 or 3](/python-2-or-3.html) works for the code in this
19+
tutorial. Just make sure you have one of those two versions installed on
20+
your system by going to the terminal and typing `python --version`.
21+
The other dependencies for this tutorial include:
22+
23+
* [Python](https://www.python.org/) version [2 or 3](/python-2-or-3.html)
24+
* [pip](https://pip.pypa.io/en/stable/) and
25+
[virtualenv](https://virtualenv.pypa.io/en/latest/) to handle one
26+
[application dependency](/application-dependencies.html)
27+
* A free [Twilio account](https://www.twilio.com/try-twilio) to use their
28+
[MMS web API](https://www.twilio.com/docs/api/rest/sending-messages)
29+
* [Twilio Python helper library](https://pypi.python.org/pypi/twilio)
30+
31+
If you are unsure of how to get pip and virtualenv installed, take a look
32+
at the first few steps of the
33+
[how to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTS](/blog/python-3-flask-green-unicorn-ubuntu-1604-xenial-xerus.html)
34+
guide.
35+
36+
37+
## Twilio Web API
38+
Our simple Python example application will use the Twilio web API to send
39+
picture messages.
40+
Go to the Twilio website
41+
[sign up for a free trial account](https://www.twilio.com/try-twilio). If
42+
you already have a Twilio account (and you should because it makes it easy to
43+
add almost any type of communications to applications!) then sign into
44+
your existing account.
45+
46+
<img src="/source/static/img/160515-mms-python/try-twilio.png" width="100%" class="technical-diagram img-rounded">
47+
48+
In trial mode Twilio can send MMS to a validated phone number associated with
49+
the account. When you're ready to send MMS messages to any phone in any
50+
country then you will have to upgrade your account.
51+
52+
After signing up for a Twilio account, you will receive your own phone
53+
number that'll be used to send messages. That phone number can send outbound
54+
MMS messages without any configuration. It can also receive messages but
55+
that requires
56+
[modifying the Request URL webhook](https://www.twilio.com/docs/quickstart/python/sms/hello-monkey)
57+
in the phone number details screen.
58+
59+
60+
## Installing Our Dependency
61+
We'll use the [twilio helper library](https://pypi.python.org/pypi/twilio)
62+
as a dependency for our Python code. The helper library can be installed
63+
via the `pip` command, which pulls the code from
64+
[PyPI](https://pypi.python.org/pypi) into our local virtualenv. In this
65+
tutorial we'll call our virtualenv `pymms` but you can name it whatever
66+
you want for your application.
67+
68+
We have to create the virtualenv before using it. In your terminal enter:
69+
70+
virtualenv pymms
71+
72+
If you need to install virtualenv take a look at the
73+
[how to set up Python 3, Django and Green Unicorn on Ubuntu 16.04 LTS](/blog/python-3-django-green-unicorn-ubuntu-1604-xenial-xerus.html)
74+
guide.
75+
76+
Activate the virtualenv with the `source` command.
77+
78+
source pymms/bin/activate
79+
80+
81+
The command prompt will change to look like this after it is activated:
82+
83+
<img src="/source/static/img/160515-mms-python/activate-virtualenv.png" width="100%" class="technical-diagram img-rounded">
84+
85+
86+
Now install the Twilio Python helper library.
87+
88+
pip install twilio
89+
90+
91+
Once the helper library installs we can use it in our Python code.
92+
93+
94+
## Sending MMS From Python
95+
Launch the the Python interpreter by executing the `python` command in
96+
your terminal. You can also create a new file named `send_mms.py` if you
97+
want to re-use the code after we give it a try.
98+
99+
100+
We need to grab our account credentials from the Twilio Console to connect
101+
our Python code to our Twilio account. Go to the
102+
[Twilio Console](https://www.twilio.com/console) and copy the Account SID
103+
and Authentication Token into your Python code.
104+
105+
<img src="/source/static/img/160515-mms-python/console-tokens.png" width="100%" class="technical-diagram img-rounded">
106+
107+
Enter the following code into the interpreter or into the new Python file.
108+
109+
# we import the Twilio client from the dependency we just installed
110+
from twilio.rest import TwilioRestClient
111+
112+
# the following line needs your Twilio Account SID and Auth Token
113+
client = TwilioRestClient("ACxxxxxxxxxxxxxx", "zzzzzzzzzzzzz")
114+
115+
# this is the URL to an image file we're going to send in the MMS
116+
media = "http://www.mattmakai.com/source/static/img/work/fsp-logo.png"
117+
118+
# change the "from_" number to your Twilio number and the "to" number
119+
# to the phone number you signed up for Twilio with, or upgrade your
120+
# account to send MMS to any phone number that MMS is available
121+
client.messages.create(to="+19732644152", from_="+12023358536",
122+
body="MMS via Python? Nice!", media_url=media)
123+
124+
125+
All the lines above that start with `#` are comments to give you some
126+
context for what each line is doing. After entering that code into the
127+
interpreter or running the Python script with `python send_mms.py`
128+
Twilio will send your MMS.
129+
130+
In a few seconds you should see a message appear on your phone - note that
131+
MMS can take a little longer because your phone has to download the image.
132+
I use an iPhone so here is what the message looked like when I received it:
133+
134+
<img src="/source/static/img/160515-mms-python/mms-result.jpg" width="100%" class="technical-diagram img-rounded">
135+
136+
That is everything need to send MMS to a phone. Pretty awesome for a few
137+
lines of Python code, right? This code can be added to any Python program
138+
to send outbound MMS.
139+
140+
One final note: keep your Twilio Auth Token secret otherwise anyone who
141+
gets it will be able to send and receive messages through your account.
142+
143+
Questions? Contact me via Twitter
144+
[@fullstackpython](https://twitter.com/fullstackpython)
145+
or [@mattmakai](https://twitter.com/mattmakai). I'm also on GitHub with
146+
the username [makaimc](https://github.com/makaimc).
147+
-65.9 KB
Binary file not shown.
33.6 KB
Loading

0 commit comments

Comments
 (0)