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 ...