Merge pull request 'switch to phpmailer to send emails' (#27) from anthony.bourguignon/SiteWebAstrolabe:master into master

Reviewed-on: AstrolabeCAE/SiteWebAstrolabe#27
This commit is contained in:
Florent Le Saout 2023-01-06 18:20:44 +01:00
commit 5699e85fcb

View File

@ -1,6 +1,12 @@
<?php
require("/usr/share/php/libphp-phpmailer/autoload.php");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
$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'])) {
$errors .= "\n Error: all fields are required";
@ -21,14 +27,6 @@
}
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;
$emailSubject = "[Formulaire Astrolabe] Nouveau message";
@ -49,15 +47,35 @@
$purpose = "Autre demande";
}
$messageWrap = wordwrap($message, 70, "\r\n", false);
$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);
// send the email
mail($to, $emailSubject, $emailBodyHTML, implode("\r\n", $headers));
$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($myEmail);
$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)) {