How to Send an Email Using SMS | SendGrid
In a world of online communication, it’s easy to forget that channels like SMS don’t need an internet connection to function. Because 3.5 billion people around the world utilize smartphones, online communications can feel like the default. However, there are millions of feature phones still in use around the world. India alone accounts for 400 million of these feature phones.
Imagine for a moment that you need to send an email without internet access. What do you do, where do you start?
Email doesn’t exist solely online. Offline sending is a great alternative that enables senders to have flexible sending practices and provides them the space to be creative with their communications.
What if you could send an email using SMS? In this tutorial, we’re going to coach you through how to send an email using Twilio SMS in PHP with Twilio SendGrid.
What you’ll need
To send email using SMS, your computer will need to be set up with the following tools and packages:
Prepare your development environment
Start by creating a folder in your computer where you will keep all of the necessary files to start sending emails using SMS. For this tutorial, we’ll call the folder sms_email.
After creating this folder, create the following empty files inside of it:
Run the command below to complete all actions simultaneously:
NOTE: The .env file is a hidden file used to store secret or private keys on your server. Because it is not accessible from the browser, API keys and credentials can be safely stored inside and accessed from anywhere within the app on the server-side.
Install the necessary packages
To enable the support of the .env file, you’ll need to install the PHP dotenv package using Composer. Run the following command in your terminal inside of the project directory:
Now, you’ll need to install the Twilio PHP SDK in order to connect your project to Twilio’s APIs. Run the following command in your terminal:
Set up your Twilio phone number and credentials
If you haven’t already done so, please take a moment to create a new phone number on your Twilio account. To send and receive SMS from your project, you’ll need to collect the following information from the Twilio Console:
- Twilio Account SID
- Twilio Auth Token
- A phone number in E. 164 format
Once you have located these items, add them to your .env file.
Set up your Twilio SendGrid credentials
By using Twilio SendGrid, a cloud-based SMTP provider, you can send email without having to maintain your own email servers.
All you’ll need to enable sending email within your app is the SendGrid PHP package and a SendGrid API key.
Once you have your key, update the .env as follows:
Next, install SendGrid’s PHP package by running the following command in your terminal:
Create your email sending helper function
Now we’re going to create a simple helper function that accepts a few parameters, processes them, and then sends messages to the recipient using SendGrid.
Below is a simple email sending function using SendGrid. Add the following code to the functions.php file:
NOTE: The FROM_EMAIL and FROM_NAME variables will need to be added to your .env file using your respective values.
Establish a Twilio webhook
When text messages are sent to your Twilio designated number, Twilio sends a response containing information about your SMS back to the application using a Twilio webhook.
You will need to create your own webhook to receive and process the email sending requests. In the webhook.php file, copy and paste the following code into it.
Congratulations! You’re only a few steps away from sending email using SMS.
Assign the webhook URL
After setting up the webhook.php function, you’ll need to assign a URL to the phone number on your Twilio settings page. Now, every time you send an SMS to your Twilio designated-number, Twilio will send data to this URL and your functions.php for processing.
For this tutorial, we’re working from our local system and there’s no way to route our local URL to Twilio because the local dev environment is not connected to the internet. This is where ngrok comes in, allowing the application to be accessed online.
If you don’t have ngrok set up on your computer, their official download page has detailed instructions on how to do so.
For our app, we’re going to run our PHP application using its built-in server. From the terminal, run the following command to start the server:
In a new terminal window, start ngrok by assigning it to the same port as the active PHP server:
Your terminal window should look like this:
Your Twilio project will use Forwarding URL provided above, followed by the path to the webhook file. For example, ours is:
The final step is connecting your Twilio number to the webhook we created. Navigate to the active numbers on your dashboard and click on the number you would like to use. In the Messaging section, find the input labeled “A MESSAGE COMES IN.” Replace the URL with your webhook URL and click “Save.”
Test your app
Send an SMS with the following format to your Twilio number:
TO:email@example.com+SUBJ:Hello+MSG:I’m sending this email using Twilio SMS
Great job! You can now send emails using SMS. When you’re ready for a new challenge, this project can be expanded to make the SMS seem more interactive (similar to a chatbot) as well as allowing sending to multiple addresses.
Regardless of whether you send online or offline, SMS and email are forces to be reckoned with in your communications strategy. Combining these forces can maximize your capacity to reach customers wherever they are, on the platform that they prefer. For a comprehensive guide on all things email and SMS, check out Using SMS and Email to Engage Your Customers in 2020.