Foros del Web » Programando para Internet » PHP »

Problema con envio de mails php

Estas en el tema de Problema con envio de mails php en el foro de PHP en Foros del Web. Buenas a todos... tengo un problemilla que no me explico por que puede pasar, tengo la siguiente función que envía emails con un archivo adjunto: ...
  #1 (permalink)  
Antiguo 01/07/2008, 02:35
 
Fecha de Ingreso: septiembre-2007
Mensajes: 106
Antigüedad: 16 años, 7 meses
Puntos: 1
Problema con envio de mails php

Buenas a todos... tengo un problemilla que no me explico por que puede pasar, tengo la siguiente función que envía emails con un archivo adjunto:

function sendmsg($to, $subject, $msgtext, $from, $file, $type)
{ $body = '';
$fp = fopen($file,"rb");
$fcontent = fread($fp ,filesize($file));
fclose($fp);
$content = chunk_split(base64_encode($fcontent));
$sep = strtoupper(md5(uniqid(time())));
$name = basename($file);
$header = "From: $from\nReply-To: $from\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=$sep
\n";
$body .= "--$sep\n";
$body .= "Content-Type: text/plain\n";
$body .= "Content-Transfer-Encoding: 8bit\n\n";
$body .= "$msgtext\n";
$body .= "--$sep\n";
$body .= "Content-Type: $type; name=\"$file\"\n";
$body .= "Content-Transfer-Encoding: base64\n";
$body .= "Content-Disposition: attachment; filename=
\"$file
\"\n";
$body .= "$content\n";
$body .= "--$sep--";
if (mail($to, $subject, $body, $header))
{
//echo "<br>Se envio<br>";
return true;
}
else
{
return false;
}
}

La utilizo de la siguiente forma:

sendmsg($email,$subject,'$body','$from',$file,'JPE G');

Lo curioso es que funciona perfectamente cuando envio los correos electrónicos con imágenes adjuntas a una dirección de gmail y no funciona bien cuando la dirección de destino es de hotmail por ejemplo... una foto de 1mega por ejemplo la envia con un tamaño de 150 k y encima no deja abrirla...

Alguien le ha pasado algo parecido??? sugerencias???

Gracias anticipadas!!

Saludos!!
  #2 (permalink)  
Antiguo 01/07/2008, 04:01
Avatar de ZiTAL  
Fecha de Ingreso: marzo-2004
Ubicación: Bermio (Bizkaia)
Mensajes: 1.545
Antigüedad: 20 años, 1 mes
Puntos: 62
Respuesta: Problema con envio de mails php

http://phpmailer.codeworxtech.com/
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan
  #3 (permalink)  
Antiguo 01/07/2008, 04:54
 
Fecha de Ingreso: septiembre-2007
Mensajes: 106
Antigüedad: 16 años, 7 meses
Puntos: 1
Respuesta: Problema con envio de mails php

Gracias!! ya me lo baje pero no lo conseguí hacer funcionar...
  #4 (permalink)  
Antiguo 01/07/2008, 06:12
 
Fecha de Ingreso: septiembre-2007
Mensajes: 106
Antigüedad: 16 años, 7 meses
Puntos: 1
Respuesta: Problema con envio de mails php

Al final encontre esta clase que va perfecta, un poco larga pero funciona muy bien:
<?
/**
* class: sendmail.class.php
* description: class for sending HTML mails with attachments
* created: 21.02.2003
* last change: 19.12.2003
* author: Günther Bauer <[email protected]>
* copyright: Günther Bauer
*/
class sendmail
{
// Variable deklarieren
var $emailheader = "";
var $textheader = "";
var $textboundary = "";
var $emailboundary = "";
var $charset = "";
var $betreff = "";
var $empfaenger = "";
var $attachment = array();
var $cc = array();
var $bcc = array();

// Konstruktor
function sendmail()
{
$this->textboundary = uniqid(time());
$this->emailboundary = uniqid(time());
$this->charset = "ISO-8859-1";
}

// Funktion zum setzen des CharSet´s
function SetCharSet($char)
{
$this->charset = $char;
}


// Funktion die überprüft ob die E-Mailadresse korrekt ist
function Validate_email($mailadresse)
{
if(!preg_match("/[a-z0-9_-]+(\.[a-z0-9_-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})/i",$mailadresse))
{
die('Die E-Mailadresse '.$mailadresse.' ist nicht gültig!!!');
}
return $mailadresse;
}

// Von wem die Email kommt in den Header setzen
function from($name,$email)
{
$this->emailheader .= 'From: '.$name.'<'.$email.'>'."\r\n";
}

// Funktion um den Adressaten anzugeben
function to($to)
{
$this->empfaenger = $this->Validate_email($to);
}

// Funktion zum senden einer Kopie an Cc Empfänger
function cc($kopie_an_empfaenger)
{
$this->cc[] = $kopie_an_empfaenger;
}

// Funktion zum senden einer Kopie an Bcc Empfänger
function bcc($kopie_an_empfaenger)
{
$this->bcc[] = $kopie_an_empfaenger;
}

// Erstellt den Header der Mime-Mail
function makeMimeMail()
{
if(count($this->cc) > 0)
{
$this->emailheader .= 'Cc: ';
for($i=0;$i<count($this->cc);$i++)
{
if($i > 0) $this->emailheader .= ',';
$this->emailheader .= $this->Validate_email($this->cc[$i]);
}
$this->emailheader .= "\r\n";
}

if(count($this->bcc) > 0)
{
$this->emailheader .= 'Bcc: ';
for($j=0;$j<count($this->bcc);$j++)
{
if($j > 0) $this->emailheader .= ',';
$this->emailheader .= $this->Validate_email($this->bcc[$j]);
}
$this->emailheader .= "\r\n";
}
$this->emailheader .= 'MIME-Version: 1.0'."\r\n";
}

// Funktion für den Betreff anzugeben
function subject($subject)
{
$this->betreff = $subject;
}

// Textdaten in Email Header packen
function text($text)
{
$this->textheader .= 'Content-Type: multipart/alternative; boundary="'.$this->textboundary.'"'."\r\n\r\n";
$this->textheader .= '--'.$this->textboundary."\r\n";
$this->textheader .= 'Content-Type: text/plain; charset="'.$this->charset.'"'."\r\n";
$this->textheader .= 'Content-Transfer-Encoding: quoted-printable'."\r\n\r\n";
$this->textheader .= strip_tags($text)."\r\n\r\n";
$this->textheader .= '--'.$this->textboundary."\r\n";
$this->textheader .= 'Content-Type: text/html; charset="'.$this->charset.'"'."\r\n";
$this->textheader .= 'Content-Transfer-Encoding: quoted-printable'."\r\n\r\n";
$this->textheader .= '<html><body>'.$text.'</body></html>'."\r\n\r\n";
$this->textheader .= '--'.$this->textboundary.'--'."\r\n\r\n";
}

// Funktion zum anhängen für Attachments in der Email
function attachment($datei)
{
// Überprüfen ob File Existiert
if(is_file($datei))
{
// Header für Attachment erzeugen
$attachment_header = '--'.$this->emailboundary."\r\n" ;
$attachment_header .= 'Content-Type: application/octet-stream; name="'.basename($datei).'"'."\r\n";
$attachment_header .= 'Content-Transfer-Encoding: base64'."\r\n";
$attachment_header .= 'Content-Disposition: attachment; filename="'.basename($datei).'"'."\r\n\r\n";

// Daten der Datei einlesen, in das BASE64 Format formatieren und auf max 72 Zeichen pro Zeile
// aufteilen
$file['inhalt'] = fread(fopen($datei,"rb"),filesize($datei));
$file['inhalt'] = base64_encode($file['inhalt']);
$file['inhalt'] = chunk_split($file['inhalt'],72);

// Attachment mit Header in der Klassenvariable speichern
$this->attachment[] = $attachment_header.$file['inhalt']."\r\n";
}
else
{
die('Die Datei "'.$datei.'" existiert nicht...'."\r\n");
}
}

// Funktion zum erstellen des Kompletten Headers der Email
// Senden der Email
function send()
{
$this->makeMimeMail();

$header = $this->emailheader;

// Überprüfen ob Attachments angehängt wurden
if(count($this->attachment)>0)
{
$header .= 'Content-Type: multipart/mixed; boundary="'.$this->emailboundary.'"'."\r\n\r\n";
$header .= '--'.$this->emailboundary."\r\n";
$header .= $this->textheader;

if(count($this->attachment) > 0) $header .= implode("",$this->attachment);

$header .= '--'.$this->emailboundary.'--';
}
else
{
$header .= $this->textheader;
}
// Versenden der Mail
mail("$this->empfaenger",$this->betreff,"",$header);

$this->deletememory();
}

// Diese Funktion ist nur zum testen
function deletememory()
{
unset($this->emailheader);
unset($this->textheader);
unset($this->attachment);
}
}
?>

Y se usa así:

$mail->SetCharSet("ISO-8859-1");
$mail->from("xxx","[email protected]");
$mail->to("[email protected]");
$mail->subject("Asunto");
$mail->text("El tesxto del mensaje con html");
$mail->attachment($fichero);
$mail->send();

Saludos!
  #5 (permalink)  
Antiguo 01/07/2008, 06:17
Avatar de ZiTAL  
Fecha de Ingreso: marzo-2004
Ubicación: Bermio (Bizkaia)
Mensajes: 1.545
Antigüedad: 20 años, 1 mes
Puntos: 62
De acuerdo Respuesta: Problema con envio de mails php

no se pero el phpmailer es de lo mejorcito para enviar e-mails desde php. Facil, sencillo y para toda la familia ;)
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan
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 15:26.