PHP: Parse error: syntax error, unexpected '='

0

I try to create a contact form that will send your data to a gmail email. For this I use the library PHPMailer , but first I am trying to get the mail with a simple text, that is without passing any variable. The problem is that it gives an error (apparently of syntax) as if it were not recognizing the parameters of class phpMailer .

This is the error

  

Parse error: syntax error, unexpected '=' in C: \ wamp \ www \ CU \ sendbymail.php on line 11

This is the php code:

require_once("class.phpmailer.php");
require_once('class.smtp.php');
$mail = new PHPMailer();
$mail­->IsSMTP();
$mail-­>SMTPDebug = 2;
$mail-­>SMTPAuth = true;
$mail-­>SMTPSecure = "ssl";
//indico el servidor de Gmail para SMTP
$mail-­>Host = "smtp.gmail.com";
//indico el puerto que usa Gmail
$mail-­>Port = 465;
//indico un usuario / clave de un usuario de gmail
$mail­->Username = "usuarioX";
$mail-­>Password = "passwordX";
$mail­->SetFrom('[email protected]', 'Nombre');
$mail­->AddReplyTo("[email protected]","Nombre");
$mail­->Subject = "Asunto del correo";
$mail-­>MsgHTML("Hola, este es el cuerpo del mensaje!");
//indico destinatario
$address = "[email protected]";
$mail­->AddAddress($address, "Mi nombre");
if(!$mail­->Send()) {
echo "Error al enviar: " . $mail-­>ErrorInfo;
} 
else {
echo "Mensaje enviado!";
} 

Then, line 11 of the code to which the error refers is this:

$mail­->SMTPDebug = 2;

I read again and again and I do not see any error in that line or in any other of that file. However when I remove it or comment then the error gives me in the next where the next operator = is, that is, in this:

$mail­->SMTPAuth = true;

And so on with the other lines. However, I perform any simple operation using this operator ( = ) and that does not require the use of class PHPMailer , and, in this case, it is recognized and the operation is executed. So I suspect that the error has to do something that is not loading well the class phpMailer or can not access their methods, but can not see where the error is.

    
asked by Adriana Hernández 08.09.2016 в 22:25
source

3 answers

2

I did the experiment of copying and pasting your code to Notepad ++ and it looks like this:

$mail--­>IsSMTP();
$mail­-->SMTPDebug = 2;

Look at the double hyphens . On the other hand, if I copy / paste the code published by @Jorgesys, the scripts appear correctly:

$mail->SMTPAuth = true;     
$mail->SMTPSecure = "tls";

I did the test using link . I copied / pasted your code and it gave me the same error you mentioned. I wrote the code directly and it did not give problem.

So, if the code was not written by you but you copied and pasted it from another site, it is likely that you are injecting a character that looks "same" to the script, but when it is not, it triggers the error.

    
answered by 17.04.2017 / 16:36
source
2

A short time ago I did the same thing and it worked for me, I send it to you as it is:

 <!--ENVIO DE EMAIL-->  
        <?php
        require '../correo/PHPMailerAutoload.php';
        $mail = new PHPMailer();

     //Variables recibidas:
     $nombre = $_POST['nombre'];
     $email = $_POST['email'];
     $mensaje = $_POST['mensaje'];

    //Permite modo debug para ver mensajes de las cosas que van ocurriendo
    //$mail->SMTPDebug = 2;

    //Hacer autenticación SMTP:
    $mail->SMTPAuth = true;     
    $mail->SMTPSecure = "tls";//CON SSL NO ME FUNCIONO

    //Indico el servidor de Gmail para SMTP:
    $mail->Host = "smtp.gmail.com"; 

    //Indico el puerto que usa Gmail:
    $mail->Port = 587;

    //Indico email de emisor:
    $mail->Username = "[email protected]";

    //Indico nombre de ususario de emisor:
    $mail->FromName = "Anonimo";

    //Indico contraseña de emisor:
    $mail->Password  = "xxxxxxxx";

     //Cuerpo del mensaje por defecto.
     $body=$mensaje;        
     $mail->Body = $body;

    //Email al que puede responderte el usuario: '**Esto lo ve el       usuario**': 
    $mail->addReplyTo($email,$nombre);

    //Asunto del mensaje:
   $mail->Subject = 'Contacto: Pagina_Web';

   //Cuerpo del mensaje: 'html/txt'.
   $mail->msgHTML($mensaje);
   $mail->AltBody = $mensaje;

   //Indico destinatario:
    $address = "[email protected]";
    $mail->addAddress($address, "Correo");

   //Envio el email:
    if(!$mail->Send()) {
             //error en mensaje:'saco mensaje'
             echo "<p>Error al enviar mensaje.</p>"; /*. $mail->ErrorInfo;*/
             echo('<a href="javascript:history.back(1)">Volver Atrás</a>');
   } else {
             //Mensaje enviado:'saco mensaje'
             echo "<p>Mensaje enviado!!.</p>";
             echo('<a href="javascript:history.back(1)">Volver Atrás</a>');
    }
 ?>

Out part of configuring in my case the configuration file 'php.ini' since I use Xampp and put the following in the area of [mail function]:

[mail function]
sendmail_from = [email protected]
sendmail_path ="\"C:\xampp\sendmail\sendmail.exe\" -t"

The problem I had was the sendmail_path that puts only unix and of that nothing, also Windows needs it. This has worked for me.

On the other hand, in your gmail email you must allow 'unsafe' emails, so to speak.

I hope it works for you.

    
answered by 09.09.2016 в 11:58
0

This is correct since you enable "DEBUG" mode:

$mail­>SMTPDebug = 2;

The problem is the syntax that is incorrect, it should be with ->

$mail-­>IsSMTP();
$mail­->SMTPDebug = 2;
$mail­->SMTPAuth = true;
$mail­->SMTPSecure = "ssl";
//indico el servidor de Gmail para SMTP
$mail-­>Host = "smtp.gmail.com";
//indico el puerto que usa Gmail
$mail­->Port = 465;
//indico un usuario / clave de un usuario de gmail
$mail­->Username = "usuarioX";
$mail­->Password = "passwordX";
$mail­->SetFrom('[email protected]', 'Nombre');
$mail­->AddReplyTo("[email protected]","Nombre");
$mail­->Subject = "Asunto del correo";
$mail­->MsgHTML("Hola, este es el cuerpo del mensaje!");
    ...
    ...

In the methods it does not cause a problem (apparently), but when you read the properties and try to assign a value as in $mail­>SMTPDebug = 2; marks the error.

    
answered by 08.09.2016 в 22:41