Skip to content

Instantly share code, notes, and snippets.

@marchawkins
Created February 22, 2013 16:15
Show Gist options
  • Save marchawkins/5014535 to your computer and use it in GitHub Desktop.
Save marchawkins/5014535 to your computer and use it in GitHub Desktop.
How-to send an email to any sms-enabled mobile phone. More info here: http://www.marchawkins.com/notes/send-email-to-sms
<!-- form to send sms -->
<form id="send-email-sms" onsubmit="emailSMS(this); return false;">
<fieldset>
<legend>Email -> SMS Demo</legend>
<div class="row"><div id="response"></div></div>
<div class="row">
<div class="six columns">
<label for="phone_num">Phone #</label><input type="text" name="phone_num" id="phone_num" placeholder="10-digit number, no dashes" maxlength="15" class="inline"/>
</div><!-- .six -->
<div class="six columns">
<label for="carrier">Carrier</label><select name="carrier" id="carrier" size="1" class="inline">
<option value="vtext.com">Verizon</option>
<option value="txt.att.net">AT&amp;T</option>
<option value="messaging.sprintpcs.com">Sprint</option>
<option value="tmomail.net">T-Mobile</option>
<option value="vmobl.com">Virgin Mobile</option>
<option value="messaging.nextel.com">Nextel</option>
<option value="email.uscc.net">US Cellular</option>
<option value="message.alltel.com">Alltel</option>
</select>
</div><!-- .six -->
</div><!-- .row -->
<div class="row">
<div class="eight columns">
<textarea name="message" id="message" placeholder="Message"></textarea>
</div><!-- .eight -->
<div class="four columns">
<input type="submit" value="send it" class="round button small"/>
</div><!-- .four -->
</div><!-- .row -->
</fieldset>
</form>
<!-- javascript to process form -->
<script>
function emailSMS(form) {
var phone_num = $(form).find('#phone_num').val();
if(!phone_num) {
alert('Enter a phone number to send a message');
} else {
var carrier = $(form).find('#carrier').val();
var recipient = phone_num+'@'+carrier;
var message = $(form).find('#message').val();
var url = "/path/to/php/email/code/below/email.php";
$.post(url, {recip: recipient, msg: message}, function(data) {
if(data) {
$(form).find('#response').html(data);
}
});
}
}
</script>
<!-- generic php emailer (put in its own file and reference path in js above) -->
<?php
// general function to send email
$recip = $_POST['recip'];
$msg = $_POST['msg'];
if(!$recip || $recip=="" || !$msg || $msg=="") {
echo('<p class="error">You must specify both a recipient and a message.</p>');
} else {
$email_to = $recip;
$email_subject = 'Test SMS via email';
$email_msg = substr($msg,0,140);
if($ok = mail($email_to,$email_subject,$email_msg)) {
echo('<p>&ldquo;<i>'.$msg.'</i>&rdquo; sent to <strong>'.$recip.'</strong>.</p>');
} else {
echo('<p class="error">Sorry, there was a problem. Reload the page to try again.</p>');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment