Hace rato estoy tratando de crear un servicio web al cual se acceda a través del correo electronico (ya que los cubanos tenemos Nauta), ya aqui los hay, pero son de pago, he querido crearlo totalmente gratuito... Todavia no he logrado que funcione al 100%, y ahora se suma que el hosting en que estaba (gratuito, por supuesto), ha cancelado la funcion imap_open() y no puedo ver a traves del codigo los correos que se envian al servicio, bueno, voy a poner el codigo de la aplicacion a ver si alguien me ayuda y creamos un servicio de calidad.
procesar.php
curl.php
procesar.php
- Código:
<?php
ini_set('display_errors', 1);
include("curl.php");
include("adjuntos.php");
include("config.php");
//Configuracion del servidor
//Fin de la configuracion
$inbox = imap_open($host,$user,$pass);// or die("Ha fallado la conexion: " . imap_last_error()); //Abrir la conexion
$emails = imap_search($inbox,'ALL');
foreach($emails as $email_number)
{
$overview = imap_fetch_overview($inbox,$email_number,0);
$asunto = strtolower($overview[0]->subject);
$parametros = explode("|",$asunto);
$URL = $parametros[0];
$post = $parametros[1];
$direccionReturn = $overview[0]->from;
$fechaEnvio = $overview[0]->date;
$direccionEmail = "";
echo $direccionReturn;
$EmailAdd = "/<.*?>/msi"; // Anchor
preg_match_all($EmailAdd, $direccionReturn, $coincidencias);
foreach ($coincidencias as $actual)
{
$direccionEmail = $actual[0];
}
$direccionEmail = str_replace("<","",$direccionEmail);
$direccionEmail = str_replace(">","",$direccionEmail);
if ($direccionEmail == "")
{
$direccionEmail = $direccionReturn;
}
$input = "";
if (!file_exists("cookie-" . $direccionEmail . ".txt"))
{
file_put_contents("cookie-" . $direccionEmail . ".txt","");
}
if ($post == "")
{
echo "Haciendo Peticion Get</br>";
$input = curl_get($URL,$direccionEmail);
}
else
{
echo "Haciendo Peticion Post</br>";
$input = curl_post($URL,$post,$direccionEmail);
}
$input = curl_get($URL,$direccionEmail);
echo "Lista de los archivos a comprimir:<ul>";
preg_match('/<title>([^>]*)<\/title>/si', $input, $matchTitle );
$title = "";
if (isset($matchTitle) && is_array($matchTitle) && count($matchTitle) > 0)
{
$title = strip_tags($matchTitle[1]);
}
$tempnameZip = $title . ".zip";
$enzipado = new ZipArchive();
$enzipado->open($tempnameZip, ZIPARCHIVE::CREATE );
$enzipado->addEmptyDir("recursos");
$pattern = '/src="[^"]*"/i';
preg_match_all($pattern, $input, $matches);
echo "Lista de los archivos a comprimir:<ul>";
foreach ($matches as $curr)
{
for($i = 0; $i < sizeof($curr); ++$i)
{
$img = $curr[$i];
$img = str_ireplace('src="','',$img);
$img = str_ireplace('"','',$img);
$imgPath = pathinfo($img);
$input = str_ireplace($img,"recursos/". $img,$input);
$input = str_ireplace("recursos/".$imgPath['dirname']."/","recursos/",$input);
$imgFile = $imgPath['basename'];
$thisPath = pathinfo($URL);
$actDir = $thisPath['dirname'];
$urlPath = parse_url($URL);
$urlHost = $urlPath['host'];
$lastPath = pathinfo($urlPath['path']);
$imag = "";
$dirname = $lastPath['dirname'];
$nombreUrlSolicitado = $lastPath['basename'];
echo "Nombre " .$nombreUrlSolicitado . "</br>";
if ($dirname == "\\")
{
$imag = $urlPath['scheme'] . "://" . $urlHost . $urlPath['path'] . realpath($img);
}
else
{
$imag = $urlPath['scheme'] . "://" . $urlHost . str_ireplace($nombreUrlSolicitado,"", $urlPath['path']) . realpath($img);
}
echo "<p>Imagen: $imag/" . str_replace(" ","%20",$img)."</p>";
$lines = file("$imag/" . str_replace(" ","%20",$img));
$ContenidoActual = "";
foreach ($lines as $line_num => $line) {
$ContenidoActual .= $line;
}
echo "<li>$imag/$img</li>";
$enzipado->addFromString('recursos/'.$imgFile, $ContenidoActual);
}
}
$pattern = '/href="[^"]*.css"/i';
preg_match_all($pattern, $input, $matches);
foreach ($matches as $curr)
{
for($i = 0; $i < sizeof($curr); ++$i)
{
$img = $curr[$i];
$img = str_ireplace('href="','',$img);
$img = str_ireplace('"','',$img);
$imgPath = pathinfo($img);
$input = str_ireplace($img,"recursos/". $img,$input);
$input = str_ireplace("recursos/".$imgPath['dirname']."/","recursos/",$input);
$imgFile = $imgPath['basename'];
$thisPath = pathinfo($URL);
$actDir = $thisPath['dirname'];
$urlPath = parse_url($URL);
$urlHost = $urlPath['host'];
$lastPath = pathinfo($urlPath['path']);
$imag = "";
$dirname = $lastPath['dirname'];
$nombreUrlSolicitado = $lastPath['basename'];
echo "Nombre " .$nombreUrlSolicitado . "</br>";
if ($dirname == "\\")
{
$imag = $urlPath['scheme'] . "://" . $urlHost . $urlPath['path'] . realpath($img);
}
else
{
$imag = $urlPath['scheme'] . "://" . $urlHost . str_ireplace($nombreUrlSolicitado,"", $urlPath['path']) . realpath($img);
}
echo "<p>Recurso: $imag/" . str_replace(" ","%20",$img)."</p>";
$lines = file("$imag/" . str_replace(" ","%20",$img));
$ContenidoActual = "";
foreach ($lines as $line_num => $line) {
$ContenidoActual .= $line;
}
echo "<li>$imag/$img</li>";
$enzipado->addFromString('recursos/'.$imgFile, $ContenidoActual);
}
}
echo "</ul>";
$patternAB = '/a href="[^"]*"/i';
preg_match_all($patternAB, $input, $matchesAB);
foreach ($matchesAB[0] as $currAB)
{
echo "$currAB</br>";
$link = str_ireplace("a href=","",$currAB);
$link = str_ireplace("\"","",$link);
$urlPath = parse_url($URL);
$urlHost = $urlPath['host'];
$lastPath = pathinfo($urlPath['path']);
$dirname = $lastPath['dirname'];
$nombreUrlSolicitado = $lastPath['basename'];
echo "URL PATH: " . $lastPath['extension'] . "</br>";
if (isset($lastPath['extension']))
{
$link = $urlPath['scheme'] . "://" . $urlHost . str_ireplace($nombreUrlSolicitado,"", $urlPath['path']). $link ;
}
else
{
$link = $urlPath['scheme'] . "://" . $urlHost . $urlPath['path'] . "/". $link ;
}
echo "Link: $link </br>";
$input = str_replace($currAB,"a href=\"javascript:window.location = 'mailto:servicios@w32m.com?subject=$link'\"",$input);
}
$enzipado->addFromString('pagina.html', $input);
$enzipado->close();
$mensajeAEnviar = "Adjunto esta un archivo comprimido con la pagina solicitada: $URL - GRACIAS POR USAR W32M by Ferning";
mail_attachment($tempnameZip,"",$direccionEmail,"internet@w32m.netai.net","W32M","ferning98@gmail.com","Navegar por Internet - W32M",$mensajeAEnviar);
unlink($tempnameZip);
imap_delete($inbox,$email_number);
}
imap_expunge($inbox);
imap_close($inbox);
?>
curl.php
- Código:
<?php
function curl_get($url, $direccionEmail, array $options = array())
{
$defaults = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_COOKIE => realpath( "cookie-" . $direccionEmail . ".txt"),
CURLOPT_COOKIEFILE => realpath("cookie-" . $direccionEmail . ".txt"),
CURLOPT_COOKIEJAR => realpath("cookie-" . $direccionEmail . ".txt"),
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
CURLOPT_TIMEOUT => 4
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}
function curl_post($url, $post, $direccionEmail, array $options = array())
{
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_COOKIE => realpath( "cookie-" . $direccionEmail . ".txt"),
CURLOPT_COOKIEFILE => realpath("cookie-" . $direccionEmail . ".txt"),
CURLOPT_COOKIEJAR => realpath("cookie-" . $direccionEmail . ".txt"),
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
CURLOPT_TIMEOUT => 4 ,
CURLOPT_POSTFIELDS => $post
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}
?>
- Código:
<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/html; charset=utf-8\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/zip; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
echo "<p>Envio del Correo a $mailto... OK</p>"; // or use booleans here
} else {
echo "<p>Envio del Correo a $mailto... ERROR!</p>";
}
echo "<p>Error:" . var_dump(error_get_last()) . "</p>";
}
?>
- Código:
<?php
$host = "{mx.000webhost.com/notls}"; //Host a conectar
$user = "internet@w32m.netau.net"; //Nombre de Usuario
$pass = "****************"; //Contraseña
?>
- P.D.:
- Espero la colaboracion de todos