Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Problema PHPMailer

Estas en el tema de Problema PHPMailer en el foro de PHP en Foros del Web. Hola muy buenas, estoy haciendo una página web en la que necesito un formulario en el cual los visitantes puedan adjuntar una foto y que ...
  #1 (permalink)  
Antiguo 21/01/2013, 04:39
 
Fecha de Ingreso: noviembre-2012
Mensajes: 5
Antigüedad: 11 años, 5 meses
Puntos: 0
Pregunta Problema PHPMailer

Hola muy buenas, estoy haciendo una página web en la que necesito un formulario en el cual los visitantes puedan adjuntar una foto y que el formulario llegue a mi correo. Todo me llega correctamente menos el archivo adjunto, voy a utilizar PHPMailer pero no se donde puedo colocar el código PHPMailer en mi formulario. Por favor que alguien me ayude.
Esta es mi formulario en HTML con el código PHP que me lo manda al correo:
Código PHP:
<?php
if (!isset($_POST['email'])) {
?>            
            <form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
              <table border="5">
                <tr>
                  <th width="217" scope="col">Nombre</th>
                  <th width="145" scope="col"><input type="text" name="nombre" id="nombre" /></th>
                </tr>
                <tr>
                  <th width="217">Apellidos</th>
                  <td width="145"><input type="text" name="apellidos" id="apellidos" /></td>
                </tr>
                <tr>
                  <th width="217" style="font-weight: bold">Teléfono</th>
                  <td width="145"><input type="text" name="telefono" id="telefono" /></td>
                </tr>
                <tr>
                  <th width="217">Localidad </th>
                  <td width="145"><input type="text" name="localidad" id="localidad" /></td>
                </tr>
                <tr>
                  <th width="217">C.P.</th>
                  <td width="145"><input type="text" name="cp" id="cp" /></td>
                </tr>
                <tr>
                  <th width="217">Provincia</th>
                  <td width="145"><input type="text" name="provincia" id="provincia" /></td>
                </tr>
                <tr>
                  <th width="217">Dirección</th>
                  <td width="145"><input type="text" name="direccion" id="direccion" /></td>
                </tr>
                <tr>
                  <th width="217">Comentario</th>
                  <td width="145"><textarea name="mensaje" id="mensaje" cols="16" rows="5"></textarea></td>
                </tr>
                <tr>
                  <th>Logotipo</th>
                  <td width="145"><label for="fileField"></label>
                  <input type="file" name="fileField" id="fileField" /></td>
                </tr>
              </table>
              <p>
                <label for="select"></label>
                <label><br />
                </label>
                <label><br />
                </label>
                <input type="submit" value="Enviar" />
</p>
            </form>
<?php
}else{
  
$mensaje="Mensaje del formulario de contacto";
  
$mensaje.= "\nNombre: "$_POST['nombre'];
  
$mensaje.= "\nApellidos: "$_POST['apellidos'];
  
$mensaje.= "\nTelefono: "$_POST['telefono'];
  
$mensaje.= "\nLocalidad: "$_POST['localidad'];
  
$mensaje.= "\nC.P.: "$_POST['cp'];
  
$mensaje.= "\nProvincia: "$_POST['provincia'];
  
$mensaje.= "\nDirección: "$_POST['direccion'];
  
$mensaje.= "\nMensaje: \n".$_POST['mensaje'];
  
$destino"[email protected]";
  
$remitente $_POST['email'];
  
$asunto "Mensaje enviado por: ".$_POST['nombre'];
  
mail($destino,$asunto,$mensaje,"FROM: $remitente");
  echo 
"Muchas gracias, datos enviados";
?>
  
<?php
}
?>
el problema viene con el codigo PHPMailer que no se si es correcto ni donde debo colocarlo

Código PHP:
<?php

include_once('../class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body             $mail->getFile('contents.html');
$body             eregi_replace("[\]",'',$body);

$mail->From       "[email protected]";
$mail->FromName   "First Last";

$mail->Subject    "PHPMailer Test Subject via mail()";

$mail->AltBody    "To view the message, please use an HTML compatible email viewer!"// optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAddress("[email protected]""John Doe");

$mail->AddAttachment("images/phpmailer.gif");             // attachment

if(!$mail->Send()) {
  echo 
"Mailer Error: " $mail->ErrorInfo;
} else {
  echo 
"Message sent!";
}

?>
Gracias anticipadas!
  #2 (permalink)  
Antiguo 21/01/2013, 05:08
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 1 mes
Puntos: 574
Respuesta: Problema PHPMailer

Código PHP:
Ver original
  1. <?php
  2. require_once('../class.phpmailer.php');
  3.  
  4. if (!isset($_POST['email'])) {   ///Aqui debes canviar algo
  5. ?>            
  6.             <form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
  7. //// Cambia el action por el nombre del fichero, la constante tambien sirve...
  8.               <table border="5">
  9.                 <tr>
  10.                   <th width="217" scope="col">Nombre</th>
  11.                   <th width="145" scope="col"><input type="text" name="nombre" id="nombre" /></th>
  12.                 </tr>
  13.                 <tr>
  14.                   <th width="217">Apellidos</th>
  15.                   <td width="145"><input type="text" name="apellidos" id="apellidos" /></td>
  16.                 </tr>
  17.                 <tr>
  18.                   <th width="217" style="font-weight: bold">Teléfono</th>
  19.                   <td width="145"><input type="text" name="telefono" id="telefono" /></td>
  20.                 </tr>
  21.                 <tr>
  22.                   <th width="217">Localidad </th>
  23.                   <td width="145"><input type="text" name="localidad" id="localidad" /></td>
  24.                 </tr>
  25.                 <tr>
  26.                   <th width="217">C.P.</th>
  27.                   <td width="145"><input type="text" name="cp" id="cp" /></td>
  28.                 </tr>
  29.                 <tr>
  30.                   <th width="217">Provincia</th>
  31.                   <td width="145"><input type="text" name="provincia" id="provincia" /></td>
  32.                 </tr>
  33.                 <tr>
  34.                   <th width="217">Dirección</th>
  35.                   <td width="145"><input type="text" name="direccion" id="direccion" /></td>
  36.                 </tr>
  37.                 <tr>
  38.                   <th width="217">Comentario</th>
  39.                   <td width="145"><textarea name="mensaje" id="mensaje" cols="16" rows="5"></textarea></td>
  40.                 </tr>
  41.                 <tr>
  42.                   <th>Logotipo</th>
  43.                   <td width="145"><label for="fileField"></label>
  44.                   <input type="file" name="fileField" id="fileField" /></td>
  45.                 </tr>
  46.               </table>
  47.               <p>
  48.                 <label for="select"></label>
  49.                 <label><br />
  50.                 </label>
  51.                 <label><br />
  52.                 </label>
  53.                 <input type="submit" value="Enviar" />
  54. </p>
  55.             </form>
  56. <?php
  57. }else{
  58.  
  59. //////Porcesamos el mail///////////
  60.  
  61. $mail             = new PHPMailer(); // defaults to using php "mail()"
  62.  
  63. $body             = $mail->getFile('contents.html');
  64. $body             = eregi_replace("[\]",'',$body);
  65.  
  66. $mail->From       = "[email protected]";
  67. $mail->FromName   = "First Last";
  68.  
  69. $mail->Subject    = "PHPMailer Test Subject via mail()";
  70.  
  71. $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
  72.  
  73. $mail->MsgHTML($body);
  74.  
  75. $mail->AddAddress("[email protected]", "John Doe");
  76.  
  77. $mail->AddAttachment("images/phpmailer.gif");             // attachment Aqui debes adjuntar el fichero del cliente
  78.  
  79. if(!$mail->Send()) {
  80.   echo "Mailer Error: " . $mail->ErrorInfo;
  81. } else {
  82.   echo "Message sent!";
  83.  
  84.  
  85. ////////////mostramos mensaje de cortesia//////////////
  86.   $mensaje="Mensaje del formulario de contacto";
  87.   $mensaje.= "\nNombre: ". $_POST['nombre'];
  88.   $mensaje.= "\nApellidos: ". $_POST['apellidos'];
  89.   $mensaje.= "\nTelefono: ". $_POST['telefono'];
  90.   $mensaje.= "\nLocalidad: ". $_POST['localidad'];
  91.   $mensaje.= "\nC.P.: ". $_POST['cp'];
  92.   $mensaje.= "\nProvincia: ". $_POST['provincia'];
  93.   $mensaje.= "\nDirección: ". $_POST['direccion'];
  94.   $mensaje.= "\nMensaje: \n".$_POST['mensaje'];
  95.   $destino= "[email protected]";
  96.   $remitente = $_POST['email'];
  97.   $asunto = "Mensaje enviado por: ".$_POST['nombre'];
  98.   mail($destino,$asunto,$mensaje,"FROM: $remitente");
  99.   echo "Muchas gracias, datos enviados";
  100.  
  101. /////////////////////////////////////////////////////////////////////
  102.  
  103. }
  104. ?>
  105.  
  106. <?php
  107. }
  108. ?>

Sin corregirte el codigo lo que debes hacer es ponerlo en el propio fichero del formulario con un condicional que muestre el formulario si no recibe datos o procese el mail y muestre la respuesta si se ha llenado el formulario.

Yo cambiaria <?=$_SERVER['PHP_SELF']?> por el nombre del mismo fichero donde tienes el codigo....

if (!isset($_POST['email'])) {

el formulario no tiene ningun campo llamado email por lo que este condicional no te sirve para comprovar si debe mostrar el formulario o procesar el correo.

$mail->AddAttachment("images/phpmailer.gif"); esto efectivamente agrega un adjunto pero siempre el mismo "images/phpmailer.gif"... debes adjuntar el fichero elegido por el usuario ... que esta en $_FILES.

Repasa el API de phpmailer para ver como debes hacerlo.
__________________
Quim
--------------------------------------------------
Ayudar a ayudar es una buena práctica!!! Y da buenos resultados.
  #3 (permalink)  
Antiguo 22/01/2013, 04:15
 
Fecha de Ingreso: noviembre-2012
Mensajes: 5
Antigüedad: 11 años, 5 meses
Puntos: 0
Respuesta: Problema PHPMailer

Gracias por contestar quimfv. He hecho los cambios que me has dicho pero me sigue sin funcionar...
  #4 (permalink)  
Antiguo 22/01/2013, 08:30
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 1 mes
Puntos: 574
Respuesta: Problema PHPMailer

Repasa el API de phpmailer para ver como debes hacerlo.
__________________
Quim
--------------------------------------------------
Ayudar a ayudar es una buena práctica!!! Y da buenos resultados.

Etiquetas: formulario, phpmailer
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:15.