Foros del Web » Programando para Internet » PHP »

EMAIL con ADJUNTOS code aqui ^^

Estas en el tema de EMAIL con ADJUNTOS code aqui ^^ en el foro de PHP en Foros del Web. Buenas, estube viendo los post sobre esta pregunta, y veo q suelen contestar: usa una clase ya hecha no me parece una respuesta muy constructiva, ...
  #1 (permalink)  
Antiguo 03/03/2005, 13:14
 
Fecha de Ingreso: octubre-2003
Mensajes: 96
Antigüedad: 20 años, 6 meses
Puntos: 0
EMAIL con ADJUNTOS code aqui ^^

Buenas, estube viendo los post sobre esta pregunta, y veo q suelen contestar: usa una clase ya hecha

no me parece una respuesta muy constructiva, yo vine buscando aprender, no solo a solucionar un problema.

Bueno, al final termine escribiendo una clase yo para enviar mails, no es nada del otro mundo, la voy a disponibilizar aqui asi entre todos la vamos mejorando, o al menos, le saca la duda a algunos:

Código PHP:
<?php

class Mail{
    var 
$asHTML=true;    # para mantener la compatibilidad, nada mas
    
var $subject='';
    var 
$message='';    
    var 
$MimeVersion="MIME-Version: 1.0";
    var 
$ContentType="Content-Type: text/html; charset=iso-8859-1";
    var 
$ContentTransferEnconding="Content-Transfer-Encoding: quoted-printable";
    
    var 
$_for='';
    var 
$_To='';
    var 
$_ReplyTo='';
    var 
$_From='';
    var 
$_Cc='';
    var 
$_Bcc='';
    
    var 
$_filename= Array();
    var 
$_filedata= Array();
    var 
$_filemime= Array();
    
    function 
attachFile($filename,$data=null,$mime=null){
        if (
$data==null){
            if (!
file_exists($filename))
                return 
false;
            
$data=file_get_contents($filename);
            if (
$data===false)
                return 
false;                
        }
        
$this->_filedata[]=$data;
        
$this->_filename[]=$filename;
        if (
$mime==null)
        
$this->_filemime[]=getMimeType($filename);
        else
        
$this->_filemime[]=$mime;
        
        return 
true;
    }
    
    
    
    
    function 
addTo($address,$alias=''){
        if (
$this->_for!='')
            
$this->_for.=', ';
        
$this->_for.=$address;
        
        if (
$this->_To!='')
            
$this->_To.=', ';
            
        if (
$alias==''){
            
$this->_To.=$address;
        }
        else{
            
$this->_To.="$alias <$address>";
        }
    }
    
    function 
setFrom($address,$alias=''){
        if (
$alias==''){
            
$this->_From.=$address;
        }
        else{
            
$this->_From.="$alias <$address>";
        }
    }
    
    function 
addCc($address,$alias=''){        
        if (
$this->_Cc!='')
            
$this->_Cc.=', ';
            
        if (
$alias==''){
            
$this->_Cc.=$address;
        }
        else{
            
$this->_Cc.="$alias <$address>";
        }
    }
    
    function 
addBcc($address,$alias=''){        
        if (
$this->_Bcc!='')
            
$this->_Bcc.=', ';
            
        if (
$alias==''){
            
$this->_Bcc.=$address;
        }
        else{
            
$this->_Bcc.="$alias <$address>";
        }
    }
    
    function 
replyTo($address){
        
$this->_ReplyTo=$address;
    }
    
    function 
send(){
        
$_MHeader='';
        
$_MBody='';
        
        
# se fija si tiene atachados
        
$_attach=sizeof($this->_filename)&&sizeof($this->_filedata);
        
        
# cabeceras normales para mail sin adjuntos
        
if (!$_attach){                    
            
$_MHeader  $this->MimeVersion."\n";
            
$_MHeader .= $this->ContentType.";\n";
        }        
        
        
/* cabeceras adicionales */
        
if ($this->_To!='')
        
$_MHeader .= "To: ".$this->_To."\n";
        if (
$this->_From!='')
        
$_MHeader .= "From: ".$this->_From."\n";
        if (
$this->_Cc!='')
        
$_MHeader .= "Cc: ".$this->_Cc."\n";
        if (
$this->_Bcc!='')
        
$_MHeader .= "Bcc: ".$this->_Bcc."\n";
        if (
$this->_ReplyTo!='')
        
$_MHeader .= "Reply-To: ".$this->_ReplyTo."\n";
        
        
        
$_bound"----=_".md5(uniqid(mt_rand(),true));
         
         
        
# prepara adjuntos
        
if ($_attach){
            
$_MHeader.="\nMime-Version: 1.0\n";            
            
$_MHeader.="Content-Type: multipart/mixed;\n";
            
$_MHeader.="\tboundary=\"{$_bound}\"\n\n";
            
            
$_MBody .= "--{$_bound}\n";            
            
$_MBody .= $this->ContentType."\n";                    
            
$_MBody .= $this->ContentTransferEnconding."\n";
            
$_MBody .= "Content-Disposition: inline\n\n";            
        }
        
        
$_MBody.= $this->message."\n\n";
        
        if (
$_attach){            
            
// archivos anexados
            
foreach ($this->_filename as $_k=>$_fn){
                
$_MBody .= "--{$_bound}\n";
                
$_MBody .= "Content-Type: {$this->_filemime[$_k]}; name={$_fn}\n";                
                
$_MBody .= "Content-Transfer-Encoding: base64\n";                
                
$_MBody .= "Content-Disposition: attachment; filename=\"{$_fn}\"\n\n";
                
                
$_MBody .= chunk_split(base64_encode($this->_filedata[$_k]));
            }
            
$_MBody .= "--{$_bound}--";
        }
        
        
        return 
mail($this->_for$this->subject$_MBody$_MHeader);
    }

?>
el uso es mas o menos asi:

Código PHP:
$mail = new Mail();
$mail->setFrom('[email protected]','JaViS en Rantac');
$mail->_for='[email protected]';
$mail->subject='Teste';
$mail->message='probando';
/*
if ($mail->attachFile('favicon.ico') )
    echo 'Attach Ok';
else 
    echo 'attach NO';
*/
    
if ($mail->attachFile('PHP-1.php'))
    echo 
'yess';
    
if (
$mail->send())
    echo 
'OK';
else 
    echo 
'NO'




El problema es q algunos antivirus automaticos suelen agregar al ultimo de los mails un mensaje del tipo

'email verificado por blabla'

y eso arruina los mails con adjuntos, y no llegan bien a los destinatarios

como puedo solucionarlo??

muchas gracias

Última edición por JaViS; 03/03/2005 a las 13:17 Razón: edito ejemplo
  #2 (permalink)  
Antiguo 03/03/2005, 13:14
 
Fecha de Ingreso: octubre-2003
Mensajes: 96
Antigüedad: 20 años, 6 meses
Puntos: 0
la parte q faltaba:
Código PHP:
function getMimeType($filename) {
  
// get base name of the filename provided by user
  
$filename basename($filename);

  
// break file into parts seperated by .
  
$filename explode('.'$filename);

  
// take the last part of the file to get the file extension
  
$filename $filename[count($filename)-1];   

  
// find mime type
  
return privFindType($filename);
}

function 
privFindType($ext) {
  
// create mimetypes array
  
$mimetypes privBuildMimeArray();
  
  
// return mime type for extension
  
if (isset($mimetypes[$ext])) {
     return 
$mimetypes[$ext];
  
// if the extension wasn't found return octet-stream         
  
} else {
     return 
'application/octet-stream';
  }
     
}

function 
privBuildMimeArray() {
  return array(
     
"ez" => "application/andrew-inset",
     
"hqx" => "application/mac-binhex40",
     
"cpt" => "application/mac-compactpro",
     
"doc" => "application/msword",
     
"bin" => "application/octet-stream",
     
"dms" => "application/octet-stream",
     
"lha" => "application/octet-stream",
     
"lzh" => "application/octet-stream",
     
"exe" => "application/octet-stream",
     
"class" => "application/octet-stream",
     
"so" => "application/octet-stream",
     
"dll" => "application/octet-stream",
     
"oda" => "application/oda",
     
"pdf" => "application/pdf",
     
"ai" => "application/postscript",
     
"eps" => "application/postscript",
     
"ps" => "application/postscript",
     
"smi" => "application/smil",
     
"smil" => "application/smil",
     
"wbxml" => "application/vnd.wap.wbxml",
     
"wmlc" => "application/vnd.wap.wmlc",
     
"wmlsc" => "application/vnd.wap.wmlscriptc",
     
"bcpio" => "application/x-bcpio",
     
"vcd" => "application/x-cdlink",
     
"pgn" => "application/x-chess-pgn",
     
"cpio" => "application/x-cpio",
     
"csh" => "application/x-csh",
     
"dcr" => "application/x-director",
     
"dir" => "application/x-director",
     
"dxr" => "application/x-director",
     
"dvi" => "application/x-dvi",
     
"spl" => "application/x-futuresplash",
     
"gtar" => "application/x-gtar",
     
"hdf" => "application/x-hdf",
     
"js" => "application/x-javascript",
     
"skp" => "application/x-koan",
     
"skd" => "application/x-koan",
     
"skt" => "application/x-koan",
     
"skm" => "application/x-koan",
     
"latex" => "application/x-latex",
     
"nc" => "application/x-netcdf",
     
"cdf" => "application/x-netcdf",
     
"sh" => "application/x-sh",
     
"shar" => "application/x-shar",
     
"swf" => "application/x-shockwave-flash",
     
"sit" => "application/x-stuffit",
     
"sv4cpio" => "application/x-sv4cpio",
     
"sv4crc" => "application/x-sv4crc",
     
"tar" => "application/x-tar",
     
"tcl" => "application/x-tcl",
     
"tex" => "application/x-tex",
     
"texinfo" => "application/x-texinfo",
     
"texi" => "application/x-texinfo",
     
"t" => "application/x-troff",
     
"tr" => "application/x-troff",
     
"roff" => "application/x-troff",
     
"man" => "application/x-troff-man",
     
"me" => "application/x-troff-me",
     
"ms" => "application/x-troff-ms",
     
"ustar" => "application/x-ustar",
     
"src" => "application/x-wais-source",
     
"xhtml" => "application/xhtml+xml",
     
"xht" => "application/xhtml+xml",
     
"zip" => "application/zip",
     
"au" => "audio/basic",
     
"snd" => "audio/basic",
     
"mid" => "audio/midi",
     
"midi" => "audio/midi",
     
"kar" => "audio/midi",
     
"mpga" => "audio/mpeg",
     
"mp2" => "audio/mpeg",
     
"mp3" => "audio/mpeg",
     
"aif" => "audio/x-aiff",
     
"aiff" => "audio/x-aiff",
     
"aifc" => "audio/x-aiff",
     
"m3u" => "audio/x-mpegurl",
     
"ram" => "audio/x-pn-realaudio",
     
"rm" => "audio/x-pn-realaudio",
     
"rpm" => "audio/x-pn-realaudio-plugin",
     
"ra" => "audio/x-realaudio",
     
"wav" => "audio/x-wav",
     
"pdb" => "chemical/x-pdb",
     
"xyz" => "chemical/x-xyz",
     
"bmp" => "image/bmp",
     
"gif" => "image/gif",
     
"ief" => "image/ief",
     
"jpeg" => "image/jpeg",
     
"jpg" => "image/jpeg",
     
"jpe" => "image/jpeg",
     
"png" => "image/png",
     
"tiff" => "image/tiff",
     
"tif" => "image/tif",
     
"djvu" => "image/vnd.djvu",
     
"djv" => "image/vnd.djvu",
     
"wbmp" => "image/vnd.wap.wbmp",
     
"ras" => "image/x-cmu-raster",
     
"pnm" => "image/x-portable-anymap",
     
"pbm" => "image/x-portable-bitmap",
     
"pgm" => "image/x-portable-graymap",
     
"ppm" => "image/x-portable-pixmap",
     
"rgb" => "image/x-rgb",
     
"xbm" => "image/x-xbitmap",
     
"xpm" => "image/x-xpixmap",
     
"xwd" => "image/x-windowdump",
     
"igs" => "model/iges",
     
"iges" => "model/iges",
     
"msh" => "model/mesh",
     
"mesh" => "model/mesh",
     
"silo" => "model/mesh",
     
"wrl" => "model/vrml",
     
"vrml" => "model/vrml",
     
"css" => "text/css",
     
"html" => "text/html",
     
"htm" => "text/html",
     
"asc" => "text/plain",
     
"txt" => "text/plain",
     
"rtx" => "text/richtext",
     
"rtf" => "text/rtf",
     
"sgml" => "text/sgml",
     
"sgm" => "text/sgml",
     
"tsv" => "text/tab-seperated-values",
     
"wml" => "text/vnd.wap.wml",
     
"wmls" => "text/vnd.wap.wmlscript",
     
"etx" => "text/x-setext",
     
"xml" => "text/xml",
     
"xsl" => "text/xml",
     
"mpeg" => "video/mpeg",
     
"mpg" => "video/mpeg",
     
"mpe" => "video/mpeg",
     
"qt" => "video/quicktime",
     
"mov" => "video/quicktime",
     
"mxu" => "video/vnd.mpegurl",
     
"avi" => "video/x-msvideo",
     
"movie" => "video/x-sgi-movie",
     
"ice" => "x-conference-xcooltalk"
  
);

  #3 (permalink)  
Antiguo 03/03/2005, 13:46
 
Fecha de Ingreso: febrero-2004
Ubicación: Bogotá, Colombia
Mensajes: 191
Antigüedad: 20 años, 2 meses
Puntos: 1
Estoy de acuerdo en q es maluco cuando a uno le dicen q use ya cosas hechas (aunque muchas veces es lo mejor, por tiempo, por evitarse dolores de cabeza, etc). Algunas veces es mejor aprender a hacer las cosas q reutilizar lo de otros.

Un saludo, viejo
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:40.