I am using PHPmailer for my contact form and when I open my website with XAMPP it works, but when I upload the website to online server (filezilla) the contact form display an error.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$error = '';
$name = '';
$email = '';
$subject = '';
$message = '';
function clean_text($string)
{
$string = trim($string);
$string = stripslashes($string);
$string = htmlspecialchars($string);
return $string;
}
if(isset($_POST["submit"]))
{
if(empty($_POST["name"]))
{
$error .= '<p><label class="text-danger">*Por Favor Insira o seu Nome</label></p>';
}
else
{
$name = clean_text($_POST["name"]);
if(!preg_match("/^[a-zA-Z ]*$/",$name))
{
$error .= '<p><label class="text-danger">Apenas letras e espaços em branco sao permitidos</label></p>';
}
}
if(empty($_POST["email"]))
{
$error .= '<p><label class="text-danger">*Por Favor Insira o seu Email</label></p>';
}
else
{
$email = clean_text($_POST["email"]);
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$error .= '<p><label class="text-danger">Formato de email inválido</label></p>';
}
}
if(empty($_POST["subject"]))
{
$error .= '<p><label class="text-danger">*Por Favor Insira o seu Assunto</label></p>';
}
else
{
$subject = clean_text($_POST["subject"]);
}
if(empty($_POST["message"]))
{
$error .= '<p><label class="text-danger">Por Favor Insira a sua Mensagem</label></p>';
}
else
{
$message = clean_text($_POST["message"]);
}
if($error == '')
{
$mail = new PHPMailer(true);
$mail->CharSet = "UTF-8";
$mail->IsSMTP();
$mail->Host = 'smtp.sapo.pt';
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = '...@sapo.pt';
$mail->Password = '*****';
$mail->SMTPSecure = 'tls';
$mail->From = $_POST["email"];
$mail->FromName = $_POST["name"];
$mail->addAddress('....@sapo.pt ', 'namecompany');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $_POST["subject"];
if($mail->Send())
{
$error = '<label class="text-success">Obrigado por entrar em contacto connosco!</label>';
}
else
{
$error = '<label class="text-danger">There is an Error</label>';
}
$name = '';
$email = '';
$subject = '';
$message = '';
}
}
ERROR DISPLAYED:
Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting in /home1/carvalho/public_html/vendor/phpmailer/phpmailer/src/PHPMailer.php:1775 Stack trace: #0 /home1/carvalho/public_html/vendor/phpmailer/phpmailer/src/PHPMailer.php(1516): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Thu, 12 S...', '\r\n\r\n ...') #1 /home1/carvalho/public_html/vendor/phpmailer/phpmailer/src/PHPMailer.php(1352): PHPMailer\PHPMailer\PHPMailer->postSend() #2 /home1/carvalho/public_html/contact2.php(119): PHPMailer\PHPMailer\PHPMailer->send() #3 {main} thrown in /home1/carvalho/public_html/vendor/phpmailer/phpmailer/src/PHPMailer.php on line 1775