///////////////////////////////////////////////////////////////////////////////
// Módulo: ag_email_class.inc.php
// Objeto: Define la clase Mime que permite mandar correo en formatos HTML y plano
// Descripción: Permite anexar ficheros e incrustar imágenes
//
// Parámetros de llamada:
//
// Módulos a los que invoca:
//
// Ultima modificación: 14 - Diciembre - 2004
// Autor: Mashha
/////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
class Mime
{
var $mixboundary; //private: Delimitador del protocolo Multipart/Mixed (para enviar más de un tipo de dato distinto)
var $relboundary; //private: Delimitador del protocolo Multipart/Related (para enviar más de un tipos de datos que se hacen referencia de uno a otro)
var $textboundary; //private: Delimitador del protocolo Multipart/Alternative (para enviar de más de una forma el mismo tipo de datos)
var $buildembeded=array(); //private: array que alberga los protocolos de las imagenes a incrustar
var $buildattached=array();//private: array que alberga los protocolos de los fichers a adjuntar
var $emailheader = ""; //private: string que reune las cabeceras del correo(from,cc,bcc,..etc)
var $textbody = ""; //private: string que reune el cuerpo del email tanto texto plano como html
var $template; //private: variable auxiliar
var $image_types; //private: array que contiene los tipos de imágenes soportados (gif, jpg, etc.)
var $datos_mail=array(); //public: Array asociativo que contiene los datos pasados como variables php al mensaje en formato plain o html
var $tipo_mail; //es decir html o plano
var $txt_version; //public: Mensaje del mail en formato texto plano
var $html_version; //public: Mensaje del mail en formato html
var $adjuntos ; //array que contiene los caminos de los ficheros a adjuntar
var $body ; //public el mensaje en caso que no se disponga de plantillas de mensaje o simplemente no se quiere
//utilizarlos.,en cuyo caso se da el mensaje como valor de esta variable de la clase
var $images_dir; // Directorio por defecto donde encontrar las imágenes a incrustar (utilizado en mailer.php)
var $Cc_errors=array();//guarda detalles de los correos cc rechazados
var $Bcc_errors=array();//guarda detalles de los correos bcc rechazados
var $Principal_errors=array();//guarda detalles de los correos de destinatarios principalles rechazados
// constructor
function Mime($headers,$datosmail,$adjuntos,$tipo)
{ if($tipo)
$this->tipo_mail= $tipo;
if($datosmail)
$this->datos_mail=$datosmail;
if(count($adjuntos)>0)
$this->adjuntos=$adjuntos;
if(count($headers)>0)
{
if($headers['from_address']||$headers['from_name']) $this->emailheader .=$this->HeaderField('From',$headers['from_address'], $headers['from_name'])."\n";
$this->emailheader .=$this->carbonCopy($headers['cc'],'cc') ;
$this->emailheader .=$this->carbonCopy($headers['bcc'],'bcc') ;
if($headers['reply']) $this->emailheader .=$this->HeaderField('reply-to',$headers['reply'],'')."\n";
$this->emailheader .= "MIME-Version: 1.0\n";
}// fin de if(count($headers)>0)
$this->relboundary = md5('relatedboundary');
$this->mixboundary = md5('mixedboundary');
$this->textboundary = md5('textboundary');
$this->image_types = array(
'gif' => 'image/gif',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'bmp' => 'image/bmp',
'png' => 'image/png',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'swf' => 'application/x-shockwave-flash'
);
}
function HeaderField ($header,$email, $name = '') {
if (!is_string ($email) || empty ($email)) {
return false;
} elseif (empty ($name)) {
$content= $email;
} else {
$name = str_replace ('"', "'", $name);
$content= "\"$name\" <$email>";
}
return ("$header: $content");
}
// string FUNCION carbonCopy() private
// Descripción: toma un array cc o bcc y devuelve sus elementos de forma adecuado para adjuntar al correo
// Parámetros :
// $copy: array qye contiene las direcciones cc o bcc
// Devuelve : devuelve un string de los elementos del array parametro separados por comas
function carbonCopy($copy,$c)
{
if(count($copy) > 0)
{
$destino=($c=='cc')?'Cc':'Bcc';
$addcopy .=$destino.': ';
$i=0;
for($i=0;$iValidateEmail($copy[$i]['EMAIL']))
{
if($i > 0) $addcopy .= ',';
$addcopy .=$copy[$i]['NOMBRE'].'<'.$email.'>';
$i++;
}
else
{
$this->{$destino.'_errors'}[$s][0]=$copy[$i]['NOMBRE'];
$this->{$destino.'_errors'}[$s][1]=$copy[$i]['EMAIL'];
$this->{$destino.'_errors'}[$s][2]=$destino;
$s++;
}
}//FIN for($i=0;$i 0)
return $addcopy;
}
// string FUNCION validateEmail() private
// Descripción: como su nombre aclara :valida un email
// Parámetros :
// $to_address: la direccion del correo a enviar
// Devuelve : devuelve el mismo correo caso de ser correcto
function validateEmail($address)
{
if(!eregi('^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$',$address))
{
return false;
}
return $address;
}
//string FUNCION Get() public
// Descripción: Obtiene el valor del miembro de la clase pasado como argumento
// Parámetros :
// $variable: Miembro de datos de la clase, del cuál se desea obtener su valor
// Devuelve : El valor del miembro pasado como argumento
function Get($variable)
{
return $this->$variable;
}
// void FUNCION Set public
// Descripción: Establece el valor del miembro de la clase pasado como argumento
// Parámetros :
// $variable: Miembro de datos de la clase, del cuál se desea establecer su valor
// $value: Valor a asignar a $variable
// Devuelve :
function Set($variable,$value)
{
$this->$variable=$value;
}
// string FUNCION GetFile() private
// Descripción: Lee un fichero y lo devuelve como salida
// Parámetros :
// $filename: path del fichero a leer
// Devuelve : El contenido del fichero si es correcto y FALSE en caso contrario
function getFile($filename)
{
$fp = @fopen($filename, 'rb');
$file = fread($fp, filesize($filename));
fclose($fp);
return $file;
}
// string FUNCION EncodeFile() private
// Descripción: Codifica el contenido de un fichero en base64
// Parámetros :
// $filepath: Path completo del fichero
// Devuelve : Contenido del fichero en codificación base64
function encodeFile($filepath)
{
$file =$this->getFile($filepath);
$file = chunk_split(base64_encode($file),72);
return $file;
}
// string FUNCION getTemplate() private
// Descripción: Protocolo de mail utilizado en la cabecera
// Parámetros :
// Devuelve: $this->html ,que es el mensaje que se quiere mandar
function getTemplate($version) { //aqui se decide que tempplate se va a utilizar y se sustituyen los variables del email por sus valores
$this->template =($version=='html')? $this->html_version:$this->txt_version;
if(!$this->template)
return false;
$message = $this->getFile($this->template);
if(is_array($this->datos_mail)&& count($this->datos_mail)>0)
{
foreach ($this->datos_mail as $key => $value) {
$placeholder = '';
$message = str_replace ($placeholder, $value, $message);
}
}
return $message;
}
//void FUNCION setText() private
// Descripción: prepara el texto plano como el html del mensaje con los protocolos adecuados
// Parámetros :
// Devuelve:
function setText()
{
if($this->tipo_mail=='html')
$html=($this->body)?$this->body:$this->getTemplate('html');
$plain=($this->txt_version)?$this->getTemplate('plain'):"";
$this->textbody .= "Content-Type: multipart/alternative; boundary=\"$this->textboundary\"\n\n";
$this->textbody .= "--$this->textboundary\n";
$this->textbody .= "Content-Type: text/plain; charset=\"ISO-8859-1\"\n";
$this->textbody .= "Content-Transfer-Encoding: 7bit\n\n";
$this->textbody .= $plain."\n\n";
if($html)
{
$this->textbody .= "--$this->textboundary\n";
$this->textbody .= "Content-Type: text/html; charset=\"ISO-8859-1\"\n";
$this->textbody .= "Content-Transfer-Encoding: 7bit\n\n";
$this->textbody .= $html."\n\n";
}
$this->textbody .= "--$this->textboundary--\n\n";
}
//void FUNCION embed() public
// Descripción: prepara las imagenes a incrustar con los protocolos adecuados
// Parámetros :
// Devuelve:
function embed($images)
{
for($i=0;$iimage_types[substr($name, strrpos($name, '.') + 1)];
$data=$this->encodeFile($embeded);
$toembed = "--$this->relboundary\n";
$toembed .= "Content-Type: ".$type.";\n name=\"".$name."\"\n";
$toembed .= "Content-Transfer-Encoding: base64\n";
$toembed .= "Content-ID: <".$cid.">\n\n";
$toembed .= $data."\n\n";
$this->buildembeded[]=$toembed;
$this->textbody= str_replace($name,'cid:'.$cid,$this->textbody );
}
}
// string FUNCION findImagesToEmbed() public
// Descripción: Busca en el cocumento html las imagenes a incrustar y los incluye como los elementos de un array que devuelve
// Parámetros :
// Devuelve: un string con los nombres (incluido path) de las imágenes
function findImagesToEmbed ()
{
while(list($key,) = each($this->image_types))
$extensions[] = $key;
if($this->body)
$html = $this->body;
else
{
$fp= fopen($this->html_version, "r");
$html = fread($fp, filesize($this->html_version));
}
//preg_match_all('/"([^"\']+\.('.implode('|', $extensions).'))"/Ui', $html, $images);
preg_match_all('/["\']([^"\']+\.('.implode('|', $extensions).'))["\']/Ui', $html, $images);
for($i=0; $iimages_dir."/".$images[1][$i]))
$images_arr[]=$this->images_dir."/".$images[1][$i];
}
if(!empty($images_arr))
{
$images_arr= array_unique($images_arr);
sort($images_arr);
}
return $images_arr;
}
//void FUNCION attach() public
// Descripción: prepara los ficheros a adjuntar con los protocolos adecuados
// Parámetros :
// Devuelve:
function attach($attach)
{
if(is_array($attach) && count($attach)>0)
foreach($attach as $value)
{
$attached=(string)$value;
$name=basename($attached);
$type= 'application/octet-stream';
$data=$this->encodeFile($attached);
$toattach = "--$this->mixboundary\n";
$toattach .= "Content-Type: ".$type.";\n name=\"".$name."\"\n";
$toattach .= "Content-Transfer-Encoding: base64\n";
$toattach .= "Content-Disposition: attachment; filename=\"".$name."\"\n\n";
$toattach .= $data."\n\n";
$this->buildattached[] = $toattach;
}
else
return false;
}
//string FUNCION buildMail() private
// Descripción: construye el correo a mandar
// Parámetros :
// Devuelve: el mensaje que se quiere mandar con los protocolos adecuados
function buildMail()
{
$this->setText();
if($this->tipo_mail=='html' )
{
$toembed=$this->findImagesToEmbed();
if($toembed !="")
$this->embed($toembed);
}
$this->attach($this->adjuntos) ;
if($this->tipo_mail=='plain')
{
if((count($this->buildattached)>0))
{ //el tipo es text plano con adjuntos
$header= "Content-type: multipart/mixed; boundary=\"".$this->mixboundary."\"\n\n";
$header .= "--".$this->mixboundary."\n";
$header .= $this->textbody ;
//$header .= ($this->body !="")?$this->body:$this->getTemplate('plain');
for($i=0;$ibuildattached);$i++)
{
$header .= $this->buildattached[$i];
}
$header .= "--".$this->mixboundary."--\n";
}
else
{//el tipo es texto oplano sin adjuntos
$header = $header .= $this->textbody ;
}
}// fin if($this->tip_mail=='plain')
else
{//si el tipo es html
if(count($this->buildattached)<=0)
{ //manda correo html solo o html con incrustaciones
$header .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"$this->relboundary\"\n\n";
$header .= "--$this->relboundary\n";
$header .= $this->textbody;
for($i=0;$ibuildembeded);$i++)
{
$header .= $this->buildembeded[$i];
}
$header .= "--$this->relboundary--";
}//fin if((count($this->buildattached)<=0))
elseif(count($this->buildembeded)<=0 && count($this->buildattached)>0)
{ // manda correo html sin incrustaciones y con attachments
$header= "Content-type: multipart/mixed; boundary=\"".$this->mixboundary."\"\n\n";
$header .= "--$this->mixboundary\n";
$header .= $this->textbody;
for($i=0;$ibuildattached);$i++)
{
$header .= $this->buildattached[$i];
}
$header .= "--".$this->mixboundary."--\n";
}//fin if((count($this->buildembeded)<0))
else
{////manda correo html con incrustaciones y attachments
$header= "Content-type: multipart/mixed; boundary=\"".$this->mixboundary."\"\n\n";
$header .= "--$this->mixboundary\n";
$header .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"$this->relboundary\"\n\n";
$header .= "--$this->relboundary\n";
$header .= $this->textbody;
for($i=0;$ibuildembeded);$i++)
{
$header .= $this->buildembeded[$i];
}
$header .= "--$this->relboundary--\n\n";
for($i=0;$ibuildattached);$i++)
{
$header .= $this->buildattached[$i];
}
$header .= "--".$this->mixboundary."--\n";
}//fin manda correo html con incrustaciones y attachments
}//fin else si el tipo es html
return $header;
}//buildMail()
//bool FUNCION send() public
// Descripción: envia el mensaje
// Parámetros :
// Devuelve: true si el correo se manda con exito y false en caso de error
function send($to_address,$to_name,$subject)
{
$header = $this->emailheader;
$header .=$this->buildMail();
$sent=false;
if($email=$this->validateEmail($to_address))
{
if($to_name !="")
$recipient='"'.$to_name.'"<'.$email.'>';
else
$recipient=$email;
if(mail($recipient,$subject,'',$header))
$sent=true;
}
else
{
$this->Principal_errors[0]=$to_name;
$this->Principal_errors[1]=$to_address;
$this->Principal_errors[2]='Principal';
}
$this->cleanMemory();
return $sent;
}
//void FUNCION cleanMemoryv() private
// Descripción: borra los arrays utilizados durante el envio del correo de la memoria despues del envio.
// Parámetros :
// Devuelve:
function cleanMemory()
{
unset($this->emailheader);
unset($this->textbody);
unset($this->buildembeded);
unset($this->buildeattached);
}
}//fin clase Mime
?>
inicio.php