I’m using PHP Mailer for Mail send process. when i use that i have recieving one error message which is
Error:
Admin: Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host. Failed to connect to serverSMTP server error: Failed to connect to server
all data recieving into database when comming to this part: if ($conn) { $stmt = $conn->prepare("INSERT INTO users (name, email) VALUES (?, ?)"); $stmt->bind_param("ss", $name, $email); if (!$stmt->execute()) { echo "Error: Unable to insert data into the database."; exit(); } $stmt->close(); } else { echo "Error: Unable to connect to the database."; exit(); }
But the problem is occuring when send the email for Admin. Give me a solution and let me know what is happening there, and i have set correct all the credentials.
My full code
require '../phpmailer/src/Exception.php';
require '../phpmailer/src/PHPMailer.php';
require '../phpmailer/src/SMTP.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST['username']) && !empty($_POST['email'])) {
$name = $_POST['username'];
$mail = $_POST['email'];
// Save data to the database
$conn = Connection();
if ($conn) {
$stmt = $conn->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
$stmt->bind_param("ss", $name, $email);
if (!$stmt->execute()) {
echo "Error: Unable to insert data into the database.";
exit();
}
$stmt->close();
} else {
echo "Error: Unable to connect to the database.";
exit();
}
// Send email to admin
try {
$mailAdmin = new PHPMailer(true);
$mailAdmin->isSMTP();
$mailAdmin->Host = 'smtp.gmail.com';
$mailAdmin->SMTPAuth = true;
$mailAdmin->Username = '[email protected]';
$mailAdmin->Password = 'my authentication key';
$mailAdmin->SMTPSecure = 'ssl';
$mailAdmin->Port = 465;
$mailAdmin->setFrom('[email protected]', 'HIII');
$mailAdmin->addAddress('[email protected]');
$mailAdmin->addReplyTo($email, $name);
$mailAdmin->isHTML(true);
$mailAdmin->Subject = "New One Arrived";
$mailAdmin->Body = "A new form submission has been received.<br><br>
Name: $name<br>Email: $email<br>";
$mailAdmin->send();
} catch (Exception $e) {
echo 'Admin: Message could not be sent. Mailer Error: ', $mailAdmin->ErrorInfo;
exit();
}
// Send thank you email to user
try {
$mailUser = new PHPMailer(true);
$mailUser->isSMTP();
$mailUser->Host = 'smtp.gmail.com';
$mailUser->SMTPAuth = true;
$mailUser->Username = '[email protected]';
$mailUser->Password = 'my authentication key';
$mailUser->SMTPSecure = 'ssl';
$mailUser->Port = 465;
$mailUser->setFrom('[email protected]', 'HIII');
$mailUser->addAddress($email);
$mailUser->isHTML(true);
$mailUser->Subject = "Thank You for Your Enquiry";
$mailUser->Body = "Dear $name,<br><br>";
$mailUser->send();
} catch (Exception $e) {
echo 'User: Message could not be sent. Mailer Error: ', $mailUser->ErrorInfo;
exit();
}
// Redirect to dashboard only if both emails are sent successfully
//if ($adminEmailSent && $userEmailSent) {
header('Location: dashboard.php');
exit();
} else {
echo 'An error occurred while processing your request.';
}
}