How can I forward SMS to my email inbox?

The following example shows how SMS messages sent to a specific Twilio number can be forwarded to an email account.

To make this code work:

  1. Modify the code below to update the ‘From’ and ‘To’ email addresses.
  2. Publish this file to a publicly accessible URL on your web server.
  3. Copy the URL into the “SMS Request URL” for an SMS enabled Twilio number you own.
<?php
/**
 * This section ensures that Twilio gets a response.
 */
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<Response></Response>'; //Place the desired response (if any) here

/**
 * This section actually sends the email.
 */
$to      = "your-email@example.com"; // Your email address
$subject = "Message from {$_REQUEST['From']} at {$_REQUEST['To']}";
$message = "You have received a message from {$_REQUEST['From']}.
Body: {$_REQUEST['Body']}";
$headers = "From: webmaster@example.com"; // Who should it come from?

mail($to, $subject, $message, $headers);

Any of the parameters which are part of the Twilio Request can also be used.