website/static/php/ajaxmail.php
Andreas Hnida 632963da3a
All checks were successful
Build and Deploy Hugo Site / buildAndDeploy (push) Successful in 1m10s
removed ah@... from open recipients list
2024-05-14 20:43:54 +02:00

76 lines
3.2 KiB
PHP

<?php
header('Access-Control-Allow-Origin: *'); // Allow access from any origin
header('Content-Type: application/json'); // Set the response content type to JSON
header('Access-Control-Allow-Methods: POST'); // Allow only POST requests
require '../vendor/autoload.php'; // Adjust the path to Composer autoload.php
require 'message.php'; // Include the message.php file
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
mb_internal_encoding("UTF-8");
try {
// Receive and validate input data using filter_input()
$name = strip_tags(trim(filter_input(INPUT_POST, 'name') . ' ' . filter_input(INPUT_POST, 'vorname'))); // Add filter_input(INPUT_POST, 'nachname') to $name
$email = filter_var(trim(filter_input(INPUT_POST, 'email')), FILTER_SANITIZE_EMAIL);
$subject = strip_tags(trim(filter_input(INPUT_POST, 'formularart')));
// Construct the message using the function from message.php
$message = constructMessage();
if (empty($name) || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($message)) {
// Invalid input
throw new Exception('Ungültige Eingabedaten.');
}
// TODO SMTP von Verua verwenden
// Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable this for detailed debugging
$mail->isSMTP(); // Use SMTP
$mail->Host = 'ophelia.kreativmedia.ch'; // Set SMTP server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'support@verua.ch'; // SMTP username
$mail->Password = 'zki@U/*{A]}3#kS/Lg|)l]t:,XHuV=8I_'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable encryption
$mail->Port = 465; // TCP port for the connection
// Recipients
$mail->setFrom('support@verua.ch', 'Verua Support'); // Sender address
$mail->addAddress('support@verua.ch', 'Verua Support'); // Add another recipient
$mail->addBCC('ah@mediendesign-hnida.de'); // Add bounce email address as BCC
// Content
$mail->isHTML(true); // Email in HTML format
$mail->CharSet = 'UTF-8';
$encodedSubject = mb_encode_mimeheader($subject, "UTF-8", "Q");
$mail->Subject = $encodedSubject;
$mail->Body = nl2br(htmlspecialchars($message));
$mail->AltBody = htmlspecialchars($message);
// Send email
if ($mail->send()) {
// Success response
echo json_encode(['success' => true, 'message' => 'Nachricht wurde gesendet.']);
http_response_code(200);
// Send a second email to the customer
$customerEmail = $email;
$customerSubject = 'Danke für Ihre Bestellung - Verua RaBe Websolutions';
$encodedSubject = mb_encode_mimeheader($customerSubject, "UTF-8", "Q");
$customerMessage = constructCustomerMessageDE($message);
$mail->clearAddresses();
$mail->addAddress($customerEmail);
$mail->Subject = $encodedSubject;
$mail->Body = nl2br(htmlspecialchars($customerMessage));
$mail->AltBody = htmlspecialchars($customerMessage);
$mail->send();
}
} catch (Exception $e) {
// Error response if an exception occurs
echo json_encode(['success' => false, 'message' => "Nachricht konnte nicht gesendet werden. Mailer Error: {$mail->ErrorInfo}"]);
}
?>