Skip to content

Instantly share code, notes, and snippets.

@sburlot
Created December 30, 2012 14:09
Show Gist options
  • Save sburlot/4412967 to your computer and use it in GitHub Desktop.
Save sburlot/4412967 to your computer and use it in GitHub Desktop.
PHP script to send SMS via the Infomaniak website. You need to have an account (http://www.infomaniak.ch) and add some credits to send SMS. This script was used to send sms alerts automatically, but you can use to send any sms you want (happy new year, birthday wishes, etc...)
#!/usr/bin/php
# Send SMS messages via Infomaniak.ch
# You need an Infomaniak.ch account and have some credits to send SMS
# Stephan Burlot, http://coriolis.ch Dec, 30 2012
#
<?php
// Username (your email address)
define('USER', '[email protected]');
// Password
define('PWD', 'omg_wtf');
//-----------------------------------------------------------------------
// Open the connection, returns TRUE if OK
//-----------------------------------------------------------------------
function open_connection($curl, $user, $password) {
$curl = curl_init();
$url = "http://webmail1.infomaniak.com/login.php";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_POST, 1);
$post_params = "submited=1&page=&mailbox=". $user . "&password=" . $password . "&language=en_EN&button=Login";
curl_setopt ($curl, CURLOPT_POSTFIELDS, $post_params);
curl_setopt ($curl, CURLOPT_COOKIEJAR, '/tmp/sms_cookie.txt');
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
if (curl_errno($curl)) {
trigger_error( "[sms] curl error: " . curl_error($curl) , E_USER_WARNING);
echo "[sms] curl error: " . curl_error($curl) . "\n";
}
if ($result != "") {
echo "Result : $result\n";
if (preg_match('/Error: Mailbox or password is incorrect/', $result) === 1) {
echo "Error: Mailbox or password is incorrect\n";
return FALSE;
}
}
return ($result == "" ? TRUE : FALSE );
}
//-----------------------------------------------------------------------
// Returns the number of credits, or -1 if an error occurs
//-----------------------------------------------------------------------
function get_credits($curl) {
$url = "http://webmail1.infomaniak.com/sms/index.php";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_POST, 0);
curl_setopt ($curl, CURLOPT_COOKIEJAR, '/tmp/sms_cookie.txt');
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
if (curl_errno($curl)) {
trigger_error( "[sms] curl error: " . curl_error($curl) , E_USER_WARNING);
return -1;
}
if (preg_match('/Actually your SMS credits is <b>([^\<]*)<\/b>/', $result, $matches)) {
$credit = $matches[1];
} else {
trigger_error( "[sms] credit not found" , E_USER_WARNING);
$credit = -1;
}
return $credit;
}
//-----------------------------------------------------------------------
// Send $message to $to, from the name (or number) $from
// if $from starts with '+', it's a number else it's a name
// returns TRUE if OK
//-----------------------------------------------------------------------
function send_sms($curl, $from, $to, $message) {
$url = "http://webmail1.infomaniak.com/sms/index.php?tab=send";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_POST, 1);
// We could send a message longer than 160 chars if we split it.
$message = substr($message, 0, 160);
if (preg_match('/^\+/', $from, $matches)) {
$post_params = "submited=1&sender=number&phone_number=" . urlencode($from) . "&sender_text=&bUnicode=0&message=" . urlencode($message) . "&type=normal&group=&phone=" . urlencode($to);
} else {
// only 11 chars allowed for the "from" name.
$from = substr($from, 0, 11);
$post_params = "submited=1&sender=text&phone_number=&sender_text=" . urlencode($from) . "&bUnicode=0&message=" . urlencode($message) . "&type=normal&group=&phone=" . urlencode($to);
}
curl_setopt ($curl, CURLOPT_POSTFIELDS, $post_params);
curl_setopt ($curl, CURLOPT_COOKIEJAR, '/tmp/sms_cookie.txt');
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
# print $result . "\n";
if (curl_errno($curl)) {
print "curl error: " . curl_error($curl) . "\n";
return -1;
}
if (preg_match('/<TD CLASS="webmail__notification">(Your message has been sent successfully)/', $result, $matches)) {
# echo "Match: " . $matches[1] . "\n";
if ($matches[1] != 'Your message has been sent successfully') {
trigger_error( "[sms] send failed " . $matches[1], E_USER_WARNING);
return FALSE;
} else {
return TRUE;
}
}
}
//-----------------------------------------------------------------------
function sms_alert($message) {
$ok = open_connection(&$curl, USER, PWD);
if ($ok) {
$credit = get_credits($curl);
// print "Credit: $credit\n";
if ($credit > 0) {
send_sms($curl, "Coriolis", "+41790000000", $message);
} else {
trigger_error("[sms] not enough credit", E_USER_WARNING);
}
}
curl_close ($curl);
}
//-----------------------------------------------------------------------
$ok = open_connection(&$curl, USER, PWD);
if ($ok) {
$credit = get_credits($curl);
print "Credit: $credit\n";
if ($credit > 0) {
// The sender will appear as "Coriolis"
// send_sms($curl, "Coriolis", "+4179000000", "Hello");
// The sender will appear as a phone number. You can put *ANY* phone number
// but Infomaniak knows the real sender, so don't send death threats ;-)
// send_sms($curl, "+4179000000", "+4179000000", "Hello");
} else {
trigger_error("[sms] not enough credit", E_USER_WARNING);
}
}
curl_close ($curl);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment