Forwarding SMS messages to your email inbox

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

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 “A message comes in – Webhook” field for an SMS-enabled Twilio number you own.
* 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.

/* Your email address */
$to = "";
$subject = "Message from {$_REQUEST['From']} at {$_REQUEST['To']}";
$message = "You have received a message from {$_REQUEST['From']}. Body: {$_REQUEST['Body']}";
$headers = "From:"; // 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.

Have more questions? Submit a request
Powered by Zendesk