website/static/php/ajaxmail.php

93 lines
3.7 KiB
PHP
Raw Normal View History

<?php
2024-06-28 17:38:12 +02:00
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");
2024-06-28 17:38:12 +02:00
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
$lang = ( isset($_POST['sprache']) && $_POST['sprache'] == "FR" ? "FR" : "DE" );
$email = filter_var(trim(filter_input(INPUT_POST, 'email')), FILTER_SANITIZE_EMAIL);
// if order is from Organisation, use email on contact-person too
if (isset($_POST['ansprechpartner_email']) && $_POST['ansprechpartner_email'] != "" )
{
$emailContact = filter_var(trim(filter_input(INPUT_POST, 'ansprechpartner_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.');
}
// 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 = 'order@verua.swiss'; // SMTP username
$mail->Password = 'Ont2J0s1qQYvX9TQrsx0nQv4Spuhha'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable encryption
$mail->Port = 465; // TCP port for the connection
// Recipients
$mail->setFrom('order@verua.swiss', 'VeruA AG'); // Sender address
$mail->addAddress('support@verua.ch', 'VeruA AG'); // Add another recipient
2024-06-26 17:37:48 +02:00
// $mail->addBCC('ah@mediendesign-hnida.de'); // Add bounce email address as BCC
2024-06-28 17:38:12 +02:00
$mail->addBCC('rabe@verua.swiss'); // Add bounce email address as BCC
// Content Mail to support
$mail->isHTML(true); // Email in HTML format
$mail->CharSet = 'UTF-8';
$encodedSubject = mb_encode_mimeheader($subject, "UTF-8", "Q");
$mail->Subject = $encodedSubject;
2024-06-28 11:05:20 +02:00
// $mail->Body = nl2br(htmlspecialchars($message));
2024-06-28 17:38:12 +02:00
$mail->Body = $message;
$mail->AltBody = $message;
2024-06-28 17:38:12 +02:00
// Send email
if ($mail->send())
{
// Success response
echo json_encode(['success' => true, 'message' => 'Nachricht wurde gesendet.']);
http_response_code(200);
2024-06-28 17:38:12 +02:00
// Send a second email to the customer
$customerEmail = $email;
$customerSubject = ($lang == "DE" ? 'Danke für Ihre Bestellung bei der VeruA AG' : 'Merci pour votre commande chez VeruA AG' );
$encodedSubject = mb_encode_mimeheader($customerSubject, "UTF-8", "Q");
2024-06-28 17:38:12 +02:00
$customerMessage = constructCustomerMessage($message, $lang, $name);
2024-06-28 17:38:12 +02:00
$mail->clearAddresses();
$mail->ClearBCCs();
$mail->addAddress($customerEmail);
if ( isset($emailContact) )
{
$mail->addAddress($customerEmail);
}
$mail->Subject = $encodedSubject;
2024-06-28 11:05:20 +02:00
// $mail->Body = nl2br(htmlspecialchars($customerMessage));
2024-06-28 17:38:12 +02:00
$mail->Body = $customerMessage;
2024-06-28 11:05:20 +02:00
// $mail->AltBody = htmlspecialchars($customerMessage);
2024-06-28 17:38:12 +02:00
$mail->AltBody = $customerMessage;
2024-06-28 17:38:12 +02:00
$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}"]);
}
?>