Hi
I am trying to use phpmailer without the smtp credentials as the client does not know the email password or they don’t have it to hand so trying to use phpmailer without the smtp info, I am trying the following code and not getting any errors and it’s going to the enquiry confirmation page but the email is not coming through
<?php
session_start();
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Include PHPMailer 6.x autoloader
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$name = '';
$phone = '';
$email = '';
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$error='';
if(!$name || !$phone || !$email || !$message){
$error.="**Please fill the fields.!".'<br>';
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error.="**Please enter valid email address.".'<br>';
}
if(!$error){
// Initialize PHPMailer
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 0; // Enable verbose debug output / Enable SMTP debug information (for testing)
//$mail->isSMTP(); // Set mailer to use SMTP
//$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
//$mail->SMTPAuth = true; // Enable SMTP authentication
//$mail->Username = ''; // SMTP username
//$mail->Password = ''; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
//$mail->Port = 587; // TCP port to connect to
$mail->setFrom('noreply@domain.co.uk', "Company Name");
$mail->addAddress('ian@domainname.co.uk', "Company Name"); // Add a recipient
$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'New Website Enquiry';
$mail->Body = "A new website enquiry has been made. The enquiry information is below\r\n\r\n"
. "Name: " . $_POST["name"] . "\r\n"
. "Phone: " . $_POST["phone"] . "\r\n"
. "Email: " . $_POST["email"] . "\r\n"
. "Message: " . $_POST["message"];
$mail->send();
// Redirect upon successful sending
header("Location: http://domainname.co.uk/enquiry-confirmation.php");
exit();
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
}
?>
<form action="#form-section" method="post">
<div class="row">
<div class="col-sm-6 col-lg-6">
<div class="form-group">
<input type="text" value="<?php echo $name; ?>" name="name" id="name" class="form-control" required placeholder="Name">
</div>
</div>
<div class="col-sm-6 col-lg-6">
<div class="form-group">
<input type="text" value="<?php echo $phone; ?>" name="phone" id="phone" class="form-control" placeholder="Phone">
</div>
</div>
<div class="col-sm-12 col-lg-12">
<div class="form-group">
<input type="email" value="<?php echo $email; ?>" name="email" id="email" class="form-control" required placeholder="Email">
</div>
</div>
<div class="col-md-12 col-lg-12">
<div class="form-group">
<textarea name="message" class="form-control" cols="30" rows="8" required placeholder="Message" value="<?php echo $message; ?>"></textarea>
</div>
</div>
<div class="col-md-12 col-lg-12">
<button type="submit" id="submit" name="submit" class="contact-btn btn">Send Message</button>
<div class="clearfix"></div>
</div>
</div>
</form>
Can anyone see where I have gone wrong please as been trying for hours and can’t see what the issue is