Website URL
carlosalbertoalegre.com.ar
Error Message
I have a contact form with phpmailer. Everything seems to work fine, except that the form values can’t be fetched, so an empty email is sent to my account. When i test it locally it works but when i upload the site the email it sends is empty.
The code is this:
<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require '../vendor/autoload.php';
$CONFIG = require("./config.php");
$to = $CONFIG["TO"];
$from = $CONFIG["FROM"];
$pass = $CONFIG["PASSAPP"];
$port = $CONFIG["PORT"];
$host = $CONFIG["HOST"];
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$subject = htmlspecialchars($_POST['subject']);
$message = htmlspecialchars($_POST['message']);
$message = str_replace("\n.", "\n..", $message);
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->Mailer = "smtp";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = $host;
$mail->Username = $from;
$mail->Password = $pass;
$mail->Port = $port;
// DATOS MENSAJE
$mail->setFrom($from);
$mail->addAddress($to);
$mail->isHTML(true);
$mail->CharSet = "UTF-8";
$mail->Subject = $subject;
$mail->Body = "<strong>From:</strong> $name<br><strong>Email:</strong> $email:<br><hr>$message";
$mail->send();
$res = ["status" => true, "message" => "El mensaje ha sido enviado."];
die(json_encode($res));
} catch (Exception $e) {
$res = ["status" => false, "message" => "Error de servidor. Intentar más tarde."];
die(json_encode($res));
}