Foros del Web » Programando para Internet » PHP »

Envio de correo

Estas en el tema de Envio de correo en el foro de PHP en Foros del Web. Hola, resulta que estoy haciendo una pagina web, yo uso ASP pero el cliente requiere que sea en PHP y me mando un archivo php ...
  #1 (permalink)  
Antiguo 05/08/2006, 17:40
Avatar de sjam7  
Fecha de Ingreso: diciembre-2001
Ubicación: Guadalajara, Mexico
Mensajes: 3.672
Antigüedad: 22 años, 3 meses
Puntos: 16
Envio de correo

Hola, resulta que estoy haciendo una pagina web, yo uso ASP pero el cliente requiere que sea en PHP y me mando un archivo php con el codigo que recibe para un formulario de contacto
el codigo es este:
Código PHP:
<?php
        
// El archivo class.smtp.inc no necesita cambios.
        // El archivo PHP es el que debe de ser modificado para recibir 
        // las variables de la forma en HTML.

    
include('class.smtp.inc');
    
header('Content-Type: text/plain');

        
// Poner todas las variables HTML sobre la variable $message.

        
$hoy date ("d Y");

        
$message "\t\t\t$hoy\n\n";
        
$message $message "---------------------------------\n";
        
$message $message "Gender: $gender\n";
    
$message $message "First: $firstname\n";
    
$message $message "Last: $lastname\n";
        
$message $message "EMAIL: $email\n"
    
$message $message "Country: $country\n";
        
$message $message "Tel: $tel\n";
    
$message $message "MessageInfo: $body\n";

                 


//$miDireccion="[email protected][email protected]";
$miDireccion="[email protected]";
$asunto="Contact us Prospects DB\n";

$mensaje=$message;


if(!@
mail($miDireccion,
          
$asunto,
          
$mensaje,
          
'From: [email protected]',
          
'[email protected]')){
    
      
Header("Location: http://www.tefl-online.com/notsent.html"); 
      exit; 
}else{
  
Header("Location: http://www.tefl-online.com/ok.html");
  exit; 
}



?>
Pero me da este error:
Warning: Cannot modify header information - headers already sent by (output started at D:\Domains\creandowebs.com\wwwroot\itto\class.smtp .inc:361) in D:\Domains\creandowebs.com\wwwroot\itto\contact.ph p on line 7

Warning: Cannot modify header information - headers already sent by (output started at D:\Domains\creandowebs.com\wwwroot\itto\class.smtp .inc:361) in D:\Domains\creandowebs.com\wwwroot\itto\contact.ph p on line 42

Puse en negritas las lineas de los errores, ojala me puedan ayudar, gracias
  #2 (permalink)  
Antiguo 06/08/2006, 01:44
 
Fecha de Ingreso: agosto-2006
Ubicación: Baja California
Mensajes: 6
Antigüedad: 17 años, 8 meses
Puntos: 0
Sonrisa

Revisa las líneas:
Código:
    include('class.smtp.inc');
    header('Content-Type: text/plain');
No sé qué haya en el archivo "class.smtp.inc", pero si el script manda información a la página antes que la instrucción "header", entonces te mandará un mensaje de error. Ese error significa que otra parte de tu script ya envió información en los headers y estás queriendo volver a enviar la misma u otra nuevamente, algo que no es permitido.

Quizá puedas cambiar de lugar el header con el include, pero eso no ayudará en la línea 42 donde vuelves a utilizar la instrucción header.

Si no puedes hacer los cambios pertinentes por algún motivo de logística o diseño, entonces prueba utilizar "output buffering".

Justo en la línea después de <?php escribe la función "ob_start()" y justo la línea antes de ?> escribe la función "ob_end_flush()" (ambas sin comillas) y vuelve a probar.

Saludos,

Lalo Márquez
  #3 (permalink)  
Antiguo 06/08/2006, 08:44
Avatar de sjam7  
Fecha de Ingreso: diciembre-2001
Ubicación: Guadalajara, Mexico
Mensajes: 3.672
Antigüedad: 22 años, 3 meses
Puntos: 16
ok, intente lo que me comentas al final pero me da este error:

Parse error: parse error, unexpected T_INCLUDE in D:\Domains\creandowebs.com\wwwroot\itto\contact.ph p on line 7

El archivo class.smtp.inc es este:
Código PHP:
<?php
    define
('SMTP_STATUS_NOT_CONNECTED'1TRUE);
    
define('SMTP_STATUS_CONNECTED'2TRUE);

    class 
smtp{

        var 
$authenticated;
        var 
$connection;
        var 
$recipients;
        var 
$headers;
        var 
$timeout;
        var 
$errors;
        var 
$status;
        var 
$body;
        var 
$from;
        var 
$host;
        var 
$port;
        var 
$helo;
        var 
$auth;
        var 
$user;
        var 
$pass;


        function 
smtp($params = array()){

            if(!
defined('CRLF'))
                
define('CRLF'"\r\n"TRUE);

            
$this->authenticated    FALSE;            
            
$this->timeout            5;
            
$this->status            SMTP_STATUS_NOT_CONNECTED;
            
$this->host                'localhost';
            
$this->port                25;
            
$this->helo                'localhost';
            
$this->auth                FALSE;
            
$this->user                '';
            
$this->pass                '';
            
$this->errors           = array();

            foreach(
$params as $key => $value){
                
$this->$key $value;
            }
        }


        function &
connect($params = array()){

            if(!isset(
$this->status)){
                
$obj = new smtp($params);
                if(
$obj->connect()){
                    
$obj->status SMTP_STATUS_CONNECTED;
                }

                return 
$obj;

            }else{
                
$this->connection fsockopen($this->host$this->port$errno$errstr$this->timeout);
                
//if(function_exists('socket_set_timeout')){
                //    socket_set_timeout($this->connection, 5, 0);
                //}

                
$greeting $this->get_data();
                if(
is_resource($this->connection)){
                    return 
$this->auth $this->ehlo() : $this->helo();
                }else{
                    
$this->errors[] = 'Failed to connect to server: '.$errstr;
                    return 
FALSE;
                }
            }
        }


        function 
send($params = array()){

            foreach(
$params as $key => $value){
                
$this->set($key$value);
            }

            if(
$this->is_connected()){

                
// Do we auth or not? Note the distinction between the auth variable and auth() function
                
if($this->auth AND !$this->authenticated){
                    if(!
$this->auth())
                        return 
FALSE;
                }

                
$this->mail($this->from);
                if(
is_array($this->recipients))
                    foreach(
$this->recipients as $value)
                        
$this->rcpt($value);
                else
                    
$this->rcpt($this->recipients);

                if(!
$this->data())
                    return 
FALSE;

                
// Transparency
                
$headers str_replace(CRLF.'.'CRLF.'..'trim(implode(CRLF$this->headers)));
                
$body    str_replace(CRLF.'.'CRLF.'..'$this->body);
                
$body    $body[0] == '.' '.'.$body $body;

                
$this->send_data($headers);
                
$this->send_data('');
                
$this->send_data($body);
                
$this->send_data('.');

                
$result = (substr(trim($this->get_data()), 03) === '250');
                
//$this->rset();
                
return $result;
            }else{
                
$this->errors[] = 'Not connected!';
                return 
FALSE;
            }
        }
        

        function 
helo(){
            if(
is_resource($this->connection)
                    AND 
$this->send_data('HELO '.$this->helo)
                    AND 
substr(trim($error $this->get_data()), 03) === '250' ){

                return 
TRUE;

            }else{
                
$this->errors[] = 'HELO command failed, output: ' trim(substr(trim($error),3));
                return 
FALSE;
            }
        }
        

        function 
ehlo(){
            if(
is_resource($this->connection)
                    AND 
$this->send_data('EHLO '.$this->helo)
                    AND 
substr(trim($error $this->get_data()), 03) === '250' ){

                return 
TRUE;

            }else{
                
$this->errors[] = 'EHLO command failed, output: ' trim(substr(trim($error),3));
                return 
FALSE;
            }
        }
        

        function 
rset(){
            if(
is_resource($this->connection)
                    AND 
$this->send_data('RSET')
                    AND 
substr(trim($error $this->get_data()), 03) === '250' ){

                return 
TRUE;

            }else{
                
$this->errors[] = 'RSET command failed, output: ' trim(substr(trim($error),3));
                return 
FALSE;
            }
        }
        

        function 
quit(){
            if(
is_resource($this->connection)
                    AND 
$this->send_data('QUIT')
                    AND 
substr(trim($error $this->get_data()), 03) === '221' ){

                
fclose($this->connection);
                
$this->status SMTP_STATUS_NOT_CONNECTED;
                return 
TRUE;

            }else{
                
$this->errors[] = 'QUIT command failed, output: ' trim(substr(trim($error),3));
                return 
FALSE;
            }
        }
        

        function 
auth(){
            if(
is_resource($this->connection)
                    AND 
$this->send_data('AUTH LOGIN')
                    AND 
substr(trim($error $this->get_data()), 03) === '334'
                    
AND $this->send_data(base64_encode($this->user))            // Send username
                    
AND substr(trim($error $this->get_data()),0,3) === '334'
                    
AND $this->send_data(base64_encode($this->pass))            // Send password
                    
AND substr(trim($error $this->get_data()),0,3) === '235' ){

                
$this->authenticated TRUE;
                return 
TRUE;

            }else{
                
$this->errors[] = 'AUTH command failed: ' trim(substr(trim($error),3));
                return 
FALSE;
            }
        }

        
        function 
mail($from){

            if(
$this->is_connected()
                AND 
$this->send_data('MAIL FROM:<'.$from.'>')
                AND 
substr(trim($this->get_data()), 02) === '250' ){

                return 
TRUE;

            }else
                return 
FALSE;
        }

        
        function 
rcpt($to){

            if(
$this->is_connected()
                AND 
$this->send_data('RCPT TO:<'.$to.'>')
                AND 
substr(trim($error $this->get_data()), 02) === '25' ){

                return 
TRUE;

            }else{
                
$this->errors[] = trim(substr(trim($error), 3));
                return 
FALSE;
            }
        }


        function 
data(){

            if(
$this->is_connected()
                AND 
$this->send_data('DATA')
                AND 
substr(trim($error $this->get_data()), 03) === '354' ){
 
                return 
TRUE;

            }else{
                
$this->errors[] = trim(substr(trim($error), 3));
                return 
FALSE;
            }
        }


        function 
is_connected(){

            return (
is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED));
        }


        function 
send_data($data){

            if(
is_resource($this->connection)){
                return 
fwrite($this->connection$data.CRLFstrlen($data)+2);
                
            }else
                return 
FALSE;
        }


        function &
get_data(){

            
$return '';
            
$line   '';
            
$loops  0;

            if(
is_resource($this->connection)){
                while((
strpos($returnCRLF) === FALSE OR substr($line,3,1) !== ' ') AND $loops 100){
                    
$line    fgets($this->connection512);
                    
$return .= $line;
                    
$loops++;
                }
                return 
$return;

            }else
                return 
FALSE;
        }

        
        function 
set($var$value){

            
$this->$var $value;
            return 
TRUE;
        }

    } 
// End of class
?>
Quite algunos comentarios por que el texto era muy grande, espero que siga siendo comprensible
  #4 (permalink)  
Antiguo 06/08/2006, 08:51
 
Fecha de Ingreso: febrero-2005
Ubicación: Tamaulipas - México
Mensajes: 328
Antigüedad: 19 años, 2 meses
Puntos: 0
el header debe ir siempre al inicio sino te tirara ese error xD
__________________
Facebook Like
  #5 (permalink)  
Antiguo 06/08/2006, 09:04
Avatar de sjam7  
Fecha de Ingreso: diciembre-2001
Ubicación: Guadalajara, Mexico
Mensajes: 3.672
Antigüedad: 22 años, 3 meses
Puntos: 16
lo que quiero hacer es que se envie el correo y se redireccione a una pagina indicada en el codigo del contactus.php que es el primero que puse
  #6 (permalink)  
Antiguo 06/08/2006, 09:38
Avatar de sjam7  
Fecha de Ingreso: diciembre-2001
Ubicación: Guadalajara, Mexico
Mensajes: 3.672
Antigüedad: 22 años, 3 meses
Puntos: 16
a ver, mi servidor es windows y uso el panel de control helm, tenia configurado el uso de PHP 5 lo cambie por PHP 4 y funciono bien, ahora solo me redirecciona siempre a ok.html aunque no se envien datos pero bueno, por lo menos ya se envia
  #7 (permalink)  
Antiguo 06/08/2006, 12:05
 
Fecha de Ingreso: agosto-2006
Ubicación: Baja California
Mensajes: 6
Antigüedad: 17 años, 8 meses
Puntos: 0
Hola de nuevo,

¿Podrías poner las 10 primeras líneas de código de tu archivo "contact.php" por favor?

Un saludo,

Lalo.
  #8 (permalink)  
Antiguo 06/08/2006, 17:29
Avatar de sjam7  
Fecha de Ingreso: diciembre-2001
Ubicación: Guadalajara, Mexico
Mensajes: 3.672
Antigüedad: 22 años, 3 meses
Puntos: 16
pues es el codigo que esta en el primer mensaje del post, ahi esta todo el archivo
  #9 (permalink)  
Antiguo 06/08/2006, 17:42
Avatar de DjFaramir  
Fecha de Ingreso: febrero-2003
Ubicación: La Plata, Buenos Aires, Argentina
Mensajes: 131
Antigüedad: 21 años, 1 mes
Puntos: 0
lo que podrias probar es de comentar los headers y ejecutar la pagina... fijate que no se imprima nada en pantalla antes de los headers... porque de hacerlo obviamente dara error...

edit: no hablo muy seguro ahora, pero creo que no puedes enviar 2 veces los headers... fijate que al comienzo del script envias y luego otra vez en el Location...
__________________
See you... y Tolkien... mucho Tolkien...
  #10 (permalink)  
Antiguo 06/08/2006, 19:10
Avatar de sjam7  
Fecha de Ingreso: diciembre-2001
Ubicación: Guadalajara, Mexico
Mensajes: 3.672
Antigüedad: 22 años, 3 meses
Puntos: 16
como decia arriba se soluciono cuando cambie en mi servidor el soporte a PHP4 ahora el problema es que SIEMPRE me redirecciona a ok.html, aunque se envie el formulario en blanco, no se si el codigo tenga una validacion para esto, crei que si pero como de php no entiendo mucho... pense que era esto:

if(!@mail($miDireccion,
$asunto,
$mensaje,
'From: [email protected]',
'[email protected]')){

Header("Location: http://www.tefl-online.com/notsent.html");
exit;
}else{
Header("Location: http://www.tefl-online.com/ok.html");
exit;
}
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

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 22:45.