SiteWebAstrolabe/src/form/contact-form-handler.php
2024-03-21 12:49:00 +01:00

117 lines
3.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require("/usr/share/php/libphp-phpmailer/autoload.php");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
$errors = '';
$myEmail = getenv('ASTRO_SMTP_FROM');
$myEmailSplitted = explode('@', $myEmail);
$domainFromMyEmail = (
empty($myEmailSplitted[1])
|| count($myEmailSplitted) != 2
) ? ''
: $myEmailSplitted[1];
$wantedContact = filter_input(INPUT_POST, 'contactTo', FILTER_SANITIZE_SPECIAL_CHARS);
$wantedContact = (
empty($wantedContact)
|| strpos($wantedContact, '@') !== false
|| strpos($wantedContact, '&') !== false
|| empty($domainFromMyEmail)
) ? $myEmail : "$wantedContact@$domainFromMyEmail" ;
if(empty($_POST['namezzz']) || empty($_POST['emailzzz']) || empty($_POST['message'])) {
$errors .= "\n Error: all fields are required";
}
if(!empty($_POST['name']) && !empty($_POST['email'])) {
$errors .= "\n Error: spam";
}
$name = $_POST['namezzz'];
$emailAddress = $_POST['emailzzz'];
$select = $_POST['select'];
$message = $_POST['message'];
$subscribe = $_POST['subscribe'];
if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
$errors .= "\n Error: Invalid email address $emailAddress";
}
if(empty($errors)) {
$emailSubject = "[Formulaire Astrolabe] Nouveau message";
switch ($select) {
case "option 1":
$purpose = "Demande de rendez-vous";
break;
case "option 2":
$purpose = "Demande de précisions sur le statut dentrepreneur salarié";
break;
case "option 3":
$purpose = "Proposition de misson";
break;
case "option 4":
$purpose = "Proposition de partenariat";
break;
default:
$purpose = "Autre demande";
}
$emailSubject .= " : $purpose";
$emailBody = "Vous avez reçu un nouveau message depuis le formulaire du site Astrolabe :".
"\r\n\r\nNom: $name \r\nEmail: $emailAddress \r\nRaison: $purpose\r\nSubscribe: $subscribe\r\n\r\n$message";
$emailBodyHTML = str_replace("\r\n", "<br>", $emailBody);
$mail->isSMTP();
$mail->Host = getenv('ASTRO_SMTP_HOSTNAME');
$mail->SMTPAuth = true;
$mail->Username = getenv('ASTRO_SMTP_USERNAME');
$mail->Password = getenv('ASTRO_SMTP_PASSWORD');
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
//Options
$mail->CharSet = 'UTF-8';
$mail->WordWrap = 70;
//Recipients
$mail->setFrom($myEmail);
$mail->addAddress($wantedContact);
$mail->addReplyTo($emailAddress, $name);
// Content
$mail->isHTML(true);
$mail->Subject = $emailSubject;
$mail->Body = $emailBodyHTML;
$mail->AltBody = $emailBody;
$mail->send();
// if subscribe add to mailing list
if(!empty($subscribe)) {
// process
// enovoi mail add to mailing list
}
// redirect to the 'thank you' page
header("Location: /thank-you/index.html");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>