DEV Community

php21
php21

Posted on

regarding php script is not getting executed

Hi,

When I am trying to execute below php script using a html webpage but it's not getting executed instead of this it points to the script itself.

here is that php script:-

``
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'E:/SW/XAMPP Installation/htdocs/fl1/PHPMailer-6.9.1/PHPMailer-6.9.1/src/Exception.php';
require 'E:/SW/XAMPP Installation/htdocs/fl1/PHPMailer-6.9.1/PHPMailer-6.9.1/src/PHPMailer.php';
require 'E:/SW/XAMPP Installation/htdocs/fl1/PHPMailer-6.9.1/PHPMailer-6.9.1/src/SMTP.php';

// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Form data
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];

// Initialize PHPMailer
$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com'; // Change this to your SMTP server hostname
    $mail->SMTPAuth = true;
    $mail->Username = 'my gmail email id'; // Change this to your email address
    $mail->Password = 'mypassword'; // Change this to your email password
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;

    // Sender and recipient
    $mail->setFrom($email, $name); // Use client's email and name as sender
    $mail->addAddress('receipient gmail id'); // Change this to the recipient email address

    // Content
    $mail->isHTML(true);
    $mail->Subject = "Message from $name";
    $mail->Body = "Name: $name<br>Phone: $phone<br>Email: $email<br>Message: $message";

    // Send email
    $mail->send();
    echo "Thank you! Your message has been sent.";
} catch (Exception $e) {
    echo "Oops! Something went wrong: {$mail->ErrorInfo}";
}
Enter fullscreen mode Exit fullscreen mode

} else {
// Redirect back to the contact form if accessed directly
header("Location: index.html");
}
?>

Top comments (5)

Collapse
 
johnsonepo profile image
Johnson Epo • Edited

The execution of the mailing function code will only run when there is a post request.
// Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") { ....
make sure you have a form like

''

    Name:<br>
    <br>

    Email:<br>
    <br>

    Phone:<br>
    <br>

    Message:<br>
    <br>
Enter fullscreen mode Exit fullscreen mode

``

Collapse
 
tech2125 profile image
php21

Thanks but could we please connect one to one to understand and resolve this issue faster either through google meeting or through anydesk ?

Collapse
 
johnsonepo profile image
Johnson Epo
Thread Thread
 
tech2125 profile image
php21

thanks, just sent google meeting invitation could you please join the same.

Thread Thread
 
tech2125 profile image
php21

Or please advise your suitable free time (with time zone) when we can meet in Google Meting to resolve this issue, please?

Thanks.