Bfaculo123
New Member
- Messages
- 6
- Reaction score
- 0
- Points
- 1
html.
<!-- Contact Box -->
<div class="contact-box text-center">
<?php
if (isset($_SESSION['message'])) {
echo '<div class="alert alert-success">' . $_SESSION['message'] . '</div>';
unset($_SESSION['message']); // Clear the message after displaying it
}
?>
<!-- Contact Form -->
<form action="send_contact.php" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-12">
<div class="form-group">
<input type="text" class="form-control" name="name" placeholder="Name" required="required">
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" required="required">
</div>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Phone" required="required">
</div>
</div>
<div class="col-12">
<div class="form-group">
<textarea class="form-control" name="message" placeholder="Message" required="required"></textarea>
</div>
</div>
<div class="col-12">
<button type="submit" class="btn btn-bordered active btn-block mt-3" name="save"><span class="text-white pr-3"><i class="fas fa-paper-plane"></i></span>Send Message</button>
</div>
</div>
</form>
<p class="form-message"></p>
</div>
</div>
</div>
now here the script
<?php
session_start(); // Start the session
// Include PHPMailer classes
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form data
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$phone = htmlspecialchars($_POST['phone']);
$message = htmlspecialchars($_POST['message']);
$mail = new PHPMailer(true);
try {
// Server settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = ''; // Your Gmail address
$mail->Password = ''; // Your App Password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Recipients
$mail->setFrom('noreply@example.com', 'Website Email'); // Change to your noreply email address
$mail->addAddress('my@gmail.com', 'MPHROfficer'); // Your Gmail account
// Content
$mail->isHTML(true);
$mail->Subject = 'MPHROfficer Email from Website';
$mail->Body = "<strong>Name:</strong> $name<br>
<strong>Email:</strong> $email<br>
<strong>Phone:</strong> $phone<br>
<strong>Message:</strong><br>$message";
$mail->AltBody = "Name: $name\nEmail: $email\nPhone: $phone\nMessage:\n$message";
$mail->send();
$_SESSION['message'] = 'Message has been sent'; // Store success message in session
} catch (Exception $e) {
$_SESSION['message'] = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; // Store error message
}
// Redirect back to the original page
if (!empty($_SERVER['HTTP_REFERER'])) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
} else {
header('Location: index.php'); // Fallback in case HTTP_REFERER is not set
}
exit; // Ensure no further code is executed
} else {
echo 'Invalid request method.';
}
?> ptrtty simple sends the mail to myself my gmail. here is error received when using it on website 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 SMTP code: 101 Additional SMTP info: Network is unreachable. please help. ps works fine local server
<!-- Contact Box -->
<div class="contact-box text-center">
<?php
if (isset($_SESSION['message'])) {
echo '<div class="alert alert-success">' . $_SESSION['message'] . '</div>';
unset($_SESSION['message']); // Clear the message after displaying it
}
?>
<!-- Contact Form -->
<form action="send_contact.php" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-12">
<div class="form-group">
<input type="text" class="form-control" name="name" placeholder="Name" required="required">
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" required="required">
</div>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Phone" required="required">
</div>
</div>
<div class="col-12">
<div class="form-group">
<textarea class="form-control" name="message" placeholder="Message" required="required"></textarea>
</div>
</div>
<div class="col-12">
<button type="submit" class="btn btn-bordered active btn-block mt-3" name="save"><span class="text-white pr-3"><i class="fas fa-paper-plane"></i></span>Send Message</button>
</div>
</div>
</form>
<p class="form-message"></p>
</div>
</div>
</div>
now here the script
<?php
session_start(); // Start the session
// Include PHPMailer classes
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form data
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$phone = htmlspecialchars($_POST['phone']);
$message = htmlspecialchars($_POST['message']);
$mail = new PHPMailer(true);
try {
// Server settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = ''; // Your Gmail address
$mail->Password = ''; // Your App Password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Recipients
$mail->setFrom('noreply@example.com', 'Website Email'); // Change to your noreply email address
$mail->addAddress('my@gmail.com', 'MPHROfficer'); // Your Gmail account
// Content
$mail->isHTML(true);
$mail->Subject = 'MPHROfficer Email from Website';
$mail->Body = "<strong>Name:</strong> $name<br>
<strong>Email:</strong> $email<br>
<strong>Phone:</strong> $phone<br>
<strong>Message:</strong><br>$message";
$mail->AltBody = "Name: $name\nEmail: $email\nPhone: $phone\nMessage:\n$message";
$mail->send();
$_SESSION['message'] = 'Message has been sent'; // Store success message in session
} catch (Exception $e) {
$_SESSION['message'] = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; // Store error message
}
// Redirect back to the original page
if (!empty($_SERVER['HTTP_REFERER'])) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
} else {
header('Location: index.php'); // Fallback in case HTTP_REFERER is not set
}
exit; // Ensure no further code is executed
} else {
echo 'Invalid request method.';
}
?> ptrtty simple sends the mail to myself my gmail. here is error received when using it on website 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 SMTP code: 101 Additional SMTP info: Network is unreachable. please help. ps works fine local server