problems with PHPmailer

0

I need to send emails by form through the PHPmailer library. I am working temporarily with localhost with TestMailServer Tool as mail server. I have the php.ini file correctly configured for these purposes. I made a new folder where I put the form, the file send.php and the libraries loaded, in this case only class.phpmailer.php
My example is a simple form with four fields:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta charset="utf-8">
    </head>
    <body>
        <form id="formulario" method="post" action="enviar.php" enctype="multipart/form-data">
            <div class="campos">
                <label>Para:</label>
                <input type="email" name="email" required>
            </div>
            <div class="campos">
                <label>Asunto:</label>
                <input type="text" name="asunto">
            </div>
            <div class="campos">
                <label>Mensaje:</label>
                <textarea name="mensaje"></textarea>
            </div>
            <label>Imagen:</label>
            <input type="file" name="adjunto" id="imagen" />
            <input id="submit" type="submit" name="enviar" value="Enviar mail">
        </form>
    </body>
</html>

send.php

<?php
//Librerías para el envío de mail
require "class.phpmailer.php"; 

//obtener campos de formulario
$para = $_POST['email'];
$asunto = $_POST['asunto'];
$mensaje = $_POST['mensaje'];
$archivo = $_FILES['adjunto'];
 
//configurar host
$mail = new PHPMailer();
$mail->IsMail();
$mail->SMTPAuth = false;
$mail->Host = "localhost";
$mail->Port = 25;
 
$mail->From       = "postmaster@localhost"; //Remitente de Correo
$mail->FromName   = "name"; //Nombre del remitente
$mail->AddAddress($para); //Destinatario de Correo
$mail->Subject = $asunto; //Asunto de Correo
$mail->Body = $mensaje; //Mensaje de Correo
$mail->AddAttachment($archivo['tmp_name'], $archivo['name']); //Para adjuntar archivo
$mail->MsgHTML($mensaje);
$mail->IsHTML(true); // Enviar como HTML

//Avisar si fue enviado o no y dirigir al index
if($mail->Send())
{
    echo'<script type="text/javascript">
            alert("Enviado Correctamente");
            window.location="http://localhost:80/PHPmailer/form.html"
         </script>';
}
else{
    echo'<script type="text/javascript">
            alert("NO ENVIADO, intentar de nuevo");
            window.location="http://localhost:80/PHPmailer/form.html"
         </script>';
}
?>

Every time I run the code the error appears:

  

Parse error: syntax error, unexpected '$ mail' (T_VARIABLE) in C: \ xampp \ htdocs \ PHPmailer \ send.php on line 11

Someone can help me with this ...

    
asked by virtual8870 27.02.2017 в 16:41
source

2 answers

1

Check the path that is the problem, you can not find the class, so the variable $ mail is empty. What is the path of the file "class.smtp.phpmailer"?.

This is my example that I made recently.

require '../../ PHPMailer / PHPMailerAutoload.php';

$ mail = new PHPMailer (true);

since my file called mailer.php (the one that runs) is in this path: C: \ xampp \ htdocs \ birthday \ Views \ Mail

and the file that I need to load is in this one: C: \ xampp \ htdocs \ birthday \ PHPMailer

that's why I pull back two folders and call it.

I hope it serves you, Regards.

    
answered by 09.05.2017 в 22:45
0

This I wanted to include as a comment rather than as an answer. Have you seen that you invoke the PHPMailer constructor with argument (). From what I've seen on forums like this , it's always assigned without parentheses. Greetings.

    
answered by 27.02.2017 в 16:53