switch to phpmailer to send emails

This commit is contained in:
Anthony Bourguignon 2023-01-04 18:04:37 +01:00
parent 770fd5e874
commit b8315263ec

View File

@ -1,6 +1,12 @@
<?php <?php
require("/usr/share/php/libphp-phpmailer/autoload.php");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
$errors = ''; $errors = '';
$myEmail = 'contact-form@astrolabe.coop'; // email address to send form content $myEmail = getenv('ASTRO_SMTP_FROM');
if(empty($_POST['namezzz']) || empty($_POST['emailzzz']) || empty($_POST['message'])) { if(empty($_POST['namezzz']) || empty($_POST['emailzzz']) || empty($_POST['message'])) {
$errors .= "\n Error: all fields are required"; $errors .= "\n Error: all fields are required";
@ -21,14 +27,6 @@
} }
if( empty($errors)) { if( empty($errors)) {
$headers = array(
"From: " .$myEmail,
"Reply-To: " .$emailAddress,
"MIME-Version: 1.0",
"Content-Type: text/html;charset=UTF-8",
"Content-Transfer-Encoding:8bit"
);
$to = $myEmail; $to = $myEmail;
$emailSubject = "[Formulaire Astrolabe] Nouveau message"; $emailSubject = "[Formulaire Astrolabe] Nouveau message";
@ -49,15 +47,36 @@
$purpose = "Autre demande"; $purpose = "Autre demande";
} }
$messageWrap = wordwrap($message, 70, "\r\n", false);
$emailBody = "Vous avez reçu un nouveau message depuis le formulaire du site Astrolabe :". $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$messageWrap"; "\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); $emailBodyHTML = str_replace("\r\n", "<br>", $emailBody);
// send the email $mail->isSMTP();
mail($to, $emailSubject, $emailBodyHTML, implode("\r\n", $headers)); $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($myEmail);
$mail->addAddress('anthony@6clones.fr');
$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 subscribe add to mailing list
if(!empty($subscribe)) { if(!empty($subscribe)) {