I'm trying to use PHPMailer to send mails with gmail SMTP server but I get a fatal error and I cannot find any solution
my php file
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require "phpmailer/PHPMailer.php";
require "phpmailer/SMTP.php";
require "phpmailer/Exception.php";
$error_msg=null;
$success_msg=null;
if ($_POST) {
//$name = isset($_POST['name']) ? filter_var($_POST['name'], FILTER_SANITIZE_STRING) : null;
//$email = isset($_POST['email']) ? filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) : null;
//$message = htmlspecialchars($_POST['message']);
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (empty($name) || empty($email) || empty($message)) {
$error_msg = 'Fill out required entry fields!';
}
if (is_null($error_msg)) {
$mail = new PHPMailer(TRUE);
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "mail@gmail.com";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->isHTML(true);
$mail->setFrom($email, $name);
$mail->addAddress("mail@gmail.com");
$mail->Subject = "Nowa wiadomość z formularza kontaktowego";
$mail->Body = $message;
$mail->send();
$success_msg = "Message sent.";
}
}
?>
html form
<div class="h-100 d-flex justify-content-center align-items-center">
<div class="row">
<div class="col-md-12 rounded">
<?php echo $lang['contact_desc']?>
<?php if ($error_msg) : ?>
<div class="alert alert-danger"><?php echo $error_msg; ?></div>
<?php endif; ?>
<?php if ($success_msg) : ?>
<div class="alert alert-success"><?php echo $success_msg; ?></div>
<?php endif; ?>
<form id="myform" method="post">
<label for="name"><?php echo $lang['name']?></label>
<input type="text" name="name" id="name" placeholder="<?php echo $lang['name']?>" required>
<label for="email">Email</label>
<input type="email" name="email" id="email" placeholder="example@example.com" required>
<label for="message"><?php echo $lang['message']?></label>
<textarea name="message" id="message" placeholder="<?php echo $lang['msg_content']?>" required></textarea>
<input type="submit" name="submit" value="<?php echo $lang['submit']?>">
</form>
</div>
</div>
</div>
The error I get:
Fatal error: Uncaught Error: Undefined constant PHPMailer\PHPMailer\FILTER_FLAG_HOST_REQUIRED" in C:\xampp\htdocs\phpmailer\PHPMailer.php:3598 Stack trace: #0 C:\xampp\htdocs\phpmailer\PHPMailer.php(3564): PHPMailer\PHPMailer\PHPMailer::isValidHost('localhost') #1 C:\xampp\htdocs\phpmailer\PHPMailer.php(2304): PHPMailer\PHPMailer\PHPMailer->serverHostname() #2 C:\xampp\htdocs\phpmailer\PHPMailer.php(1421): PHPMailer\PHPMailer\PHPMailer->createHeader() #3 C:\xampp\htdocs\phpmailer\PHPMailer.php(1316): PHPMailer\PHPMailer\PHPMailer->preSend() #4 C:\xampp\htdocs\scripts\form.php(43): PHPMailer\PHPMailer\PHPMailer->send() #5 C:\xampp\htdocs\contact.php(3): include('C:\xampp\htdocs...') #6 {main} thrown in C:\xampp\htdocs\phpmailer\PHPMailer.php on line 3598
Any ideas?
The FILTER_FLAG_HOST_REQUIRED
filter flag was deprecated in PHP 7.3 and there are no longer any references to it in the PHPMailer code base. So a straightforward fix should be to simply update PHPMailer. If you were using composer this would be automatic and very simple, but you're not, so you'll have to download the latest version and install it yourself as per the readme.