How to Send an MMS with Python

May 14, 2021
Written by
Reviewed by
Diane Phan
Twilion

How to Send an MMS with Python

Twilio is all about powering communication and doing it conveniently and fast in any language.  

With the help of Twilio and Python, you can deliver a quick media message to someone without having to pick up your mobile device.

In this article, you'll be writing a short Python program to send an MMS in an insanely fast manner. So why wait? Let's get started!

Tutorial requirements

Configuration

We’ll start off by creating a directory to store the files of our project. Inside your favorite terminal, enter:

$ mkdir python_mms
$ cd python_mms

Following Python best practices, you are now going to create a virtual environment, where you are going to install the Python dependencies needed for this project.

If you are using a Unix or Mac OS system, open a terminal and enter the following commands:

python3 -m venv venv
source venv/bin/activate

If you are following the tutorial on Windows, enter the following commands in a command prompt window:

python -m venv venv
venv\Scripts\activate

With the virtual environment activated, you are ready to install the only Python dependency required for this project:

pip install twilio

The twilio package will allow us to work with the Twilio APIs for sending MMS.

Buy a Twilio phone number

If you haven't done so already, purchase a Twilio number to send the MMS.

Log in to the Twilio Console, select Phone Numbers, and then click on the red plus sign to buy a Twilio number. Note that if you are using a free account you will be using your trial credit for this purchase.

In the “Buy a Number” screen you can select your country and check MMS in the capabilities field. If you’d like to request a number from your region, you can enter your area code in the “Number” field.

Keep in mind that MMS is only supported in the US and Canada at this time so you might not see the MMS box if you are outside these countries. In that case you can purchase a number that has SMS capability instead.

Buy a phone number

Click the Search button to see what numbers are available, and then click “Buy” for the number that you like from the results. After you confirm your purchase, click the Close button.

Send an MMS with Python

Create a file named send_mms.py in your project directory and paste the following code:

import os
from twilio.rest import Client

def send_mms():
    account_sid = os.environ['TWILIO_ACCOUNT_SID']
    auth_token = os.environ['TWILIO_AUTH_TOKEN']
    client = Client(account_sid, auth_token)

    message = client.messages \
        .create(
            body='Ahoy, world! I love writing Python code.',
            media_url='https://raw.githubusercontent.com/dianephan/flask_upload_photos/main/UPLOADS/DRAW_THE_OWL_MEME.png',
            from_='+<YOUR_TWILIO_NUMBER>',
            to='+<YOUR_PHONE_NUMBER>'
        )

    print(message.sid)

if __name__ == '__main__':
    send_mms()

The code above needs access to your Twilio account credentials. To protect your account details we are going to set environment variables.

You can find your account credentials on the Twilio Console.

Twilio Account Credentials

If you are using Mac OS or Linux, run the commands below to add your account credentials to your environment:

export TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
export TWILIO_AUTH_TOKEN=<YOUR_AUTH_TOKEN>

If you are following this tutorial on a Windows computer, you can assign your credentials to environment variables as seen below:

set TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
set TWILIO_AUTH_TOKEN=<YOUR_AUTH_TOKEN>

The send_mms() function starts by initializing a Twilio client instance, using the TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN variables set above.

We then create a new message instance in order to send an MMS to your personal phone number from your Twilio phone number.

Since we want to send a media message, the media_url argument is called so that Twilio knows how to retrieve and send the media message. The URL that you pass in this argument must be publicly accessible, so keep that in mind when choosing an image to send. The example image above displays Twilio's favorite "Draw the Owl" meme, but feel free to change the URL to point to your favorite image.

After the message is sent, the Message SID is printed on your terminal for your reference.

To complete the script, replace the <YOUR_PHONE_NUMBER> and <YOUR_TWILIO_NUMBER> placeholders with your actual phone numbers, using the E.164 format.

Once you are done editing the file, save it to the project directory.

Run the application with the following command:

python send_mms.py

In just a moment, you will see a ping from your personal phone with the MMS sent directly from your terminal!

MMS screenshot

What's next for sending MMS with Python?

Congratulations on writing a short Python script and using the command line to send a quick MMS to your phone! This was so wickedly quick, can you even believe it was possible?

There's so much more you can do with Twilio and Python code! If you want some ideas, try the tutorials below:

I can’t wait to see what you build!

Miguel Grinberg is a Principal Software Engineer for Technical Content at Twilio. Reach out to him at mgrinberg [at] twilio [dot] com if you have a cool project you’d like to share on this blog!