first commit
هذا الالتزام موجود في:
76
contact_handler.php
Normal file
76
contact_handler.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
// Import PHPMailer library
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
|
||||
// Include library files via Composer autoloader
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
// Check if form was submitted via POST method
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
|
||||
// Get data from form and sanitize it
|
||||
$name = strip_tags(trim($_POST["name"]));
|
||||
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
|
||||
$phone = trim($_POST["phone"]);
|
||||
$message = trim($_POST["message"]);
|
||||
|
||||
// Validate required fields
|
||||
if (empty($name) || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($message)) {
|
||||
// Return error response
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Please fill in all required fields and try again.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Create new PHPMailer object
|
||||
$mail = new PHPMailer(true);
|
||||
|
||||
try {
|
||||
// SMTP settings for sending via Gmail
|
||||
$mail->isSMTP();
|
||||
$mail->Host = 'smtp.gmail.com'; // Gmail server
|
||||
$mail->SMTPAuth = true;
|
||||
$mail->Username = 'scandrone308@gmail.com'; // Your Gmail address for sending
|
||||
$mail->Password = 'your_app_password'; // App password (explained below)
|
||||
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Encryption (SSL)
|
||||
$mail->Port = 465; // Gmail port
|
||||
|
||||
// Who will receive the email
|
||||
$mail->setFrom('from@yourwebsite.com', 'Website Contact Form'); // Dummy sender email
|
||||
$mail->addAddress('bayan10kh@gmail.com', 'Bayan'); // Your email to receive messages
|
||||
|
||||
// Email content
|
||||
$mail->isHTML(true); // Enable HTML in email
|
||||
$mail->Subject = 'New message from contact form';
|
||||
|
||||
// Build message body
|
||||
$email_content = "<h2>You have received a new message from your website:</h2>";
|
||||
$email_content .= "<p><strong>Name:</strong> {$name}</p>";
|
||||
$email_content .= "<p><strong>Email:</strong> {$email}</p>";
|
||||
$email_content .= "<p><strong>Phone:</strong> {$phone}</p>";
|
||||
$email_content .= "<p><strong>Message:</strong><br>{$message}</p>";
|
||||
|
||||
$mail->Body = $email_content;
|
||||
$mail->AltBody = "Name: {$name}\nEmail: {$email}\nPhone: {$phone}\nMessage:\n{$message}"; // Text version of email
|
||||
|
||||
$mail->send();
|
||||
|
||||
// Return success response
|
||||
echo json_encode(['success' => true, 'message' => 'Your message has been sent successfully!']);
|
||||
exit;
|
||||
|
||||
} catch (Exception $e) {
|
||||
// Handle error
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => "Sorry, your message could not be sent. Error: {$mail->ErrorInfo}"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
} else {
|
||||
// If not accessed via POST
|
||||
http_response_code(403);
|
||||
echo json_encode(['success' => false, 'message' => 'There was a problem with your request, please try again.']);
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
المرجع في مشكلة جديدة
حظر مستخدم