Optimización mayor en carga de modulos.
[xmensajitos.php.git] / mail2sms.php
blobfc7f4b991f18f11213c7cdb0649748031663dec6
1 #!/usr/bin/php -q
2 <?php
3 //sms.todosv.com wrapper
4 error_reporting(0);
6 function do_post_request($url, $data, $optional_headers = null)
8 $params = array('http' => array('method' => 'POST','content' => $data));
9 if ($optional_headers !== null) {
10 $params['http']['header'] = $optional_headers;
12 $ctx = stream_context_create($params);
13 $fp = @fopen($url, 'rb', false, $ctx);
14 if (!$fp) {
15 echo "Problem with $url, $php_errormsg". "\n";
17 $response = @stream_get_contents($fp);
18 if ($response === false) {
19 echo "Problem reading data from $url, $php_errormsg". "\n";
21 return $response;
24 function tsv_sms_enviar($telefono, $mensaje, $firma){
25 $datos = array('telefono' => $telefono, 'mensaje' => $mensaje, 'firma' => $firma, 'enviar' => '¡Enviar Mensaje!');
26 return do_post_request('http://sms.todosv.com/index.php?visual=estado', http_build_query($datos));
29 // read from stdin
30 $email = file_get_contents("php://stdin");
32 // handle email
33 $lines = explode("\n", $email);
35 // empty vars
36 $from = "";
37 $to = "";
38 $subject = "";
39 $headers = "";
40 $message = "";
41 $splittingheaders = true;
43 for ($i=0; $i < count($lines); $i++) {
44 if ($splittingheaders) {
45 // this is a header
46 $headers .= $lines[$i]."\n";
48 // look out for special headers
49 if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
50 $subject = $matches[1];
52 if (preg_match("/^From: .*<(.*)>.*/", $lines[$i], $matches) && !$from) {
53 $from = $matches[1];
55 if (preg_match("/^To: [<]{0,1}(.*)@/", $lines[$i], $matches) && !$to) {
56 $to = $matches[1];
58 if (preg_match("/^X-Forwarded-For: (.*) .*/", $lines[$i], $matches)) {
59 $from = $matches[1];
61 if (preg_match("/^X-Forwarded-To: (.*)@/", $lines[$i], $matches)) {
62 $to = $matches[1];
65 } else {
66 // not a header, but message
67 $message .= $lines[$i]."\n";
70 if (trim($lines[$i])=="") {
71 // empty line, header section has ended
72 $splittingheaders = false;
76 $message = substr(str_replace("\n"," ",$message),0,110);
78 //@file_put_contents("d_".time(),$email);
79 //echo "FROM:" . $from . "\n";
80 //echo "TO:" . $to . "\n";
81 //echo "SUBJECT:" . $subject . "\n";
82 //echo $message . "\n";
83 if ( stristr($to,"_r") ) {
84 $flag_mail = true;
85 $to = str_replace("_r","",$to);
86 } else {
87 $flag_mail = false;
90 $body = tsv_sms_enviar($to, $subject, $from);
91 $headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=iso-8859-1' . "\r\n" . 'From: robot@sms.todosv.com' ."\r\n" . 'Reply-To: no_responder_aqui@todosv.com' . "\r\n";
92 if ($flag_mail) @mail($from,"Estado del mensaje a $to","<html><head><title>Correo de respuesta solicitado por su mensaje enviado a $to</title></head><body>".$body."</body></html>", $headers);
93 exit(0);