diff options
Diffstat (limited to 'src/Service/EmailService.php')
| -rw-r--r-- | src/Service/EmailService.php | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/src/Service/EmailService.php b/src/Service/EmailService.php new file mode 100644 index 0000000..38b4cf8 --- /dev/null +++ b/src/Service/EmailService.php | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | <?php | ||
| 2 | // src/Service/EmailService.php | ||
| 3 | |||
| 4 | declare(strict_types=1); | ||
| 5 | |||
| 6 | namespace App\Service; | ||
| 7 | |||
| 8 | use PHPMailer\PHPMailer\PHPMailer; | ||
| 9 | //use PHPMailer\PHPMailer\Exception; | ||
| 10 | use Doctrine\ORM\EntityManager; | ||
| 11 | use App\Entity\Email; | ||
| 12 | use App\Entity\EmailForm; | ||
| 13 | |||
| 14 | class EmailService | ||
| 15 | { | ||
| 16 | const KEEP_EMAILS_DEFAULT = false; | ||
| 17 | |||
| 18 | static public function send(EntityManager $entityManager, EmailForm $form_data, bool $test_email, string $name = '', string $email = '', string $message = ''): bool | ||
| 19 | { | ||
| 20 | $mail = new PHPMailer(true); // true => exceptions | ||
| 21 | $mail->CharSet = 'UTF-8'; | ||
| 22 | |||
| 23 | $smtp_host = $form_data->getData()['smtp_host'] ?? Config::$smtp_host; | ||
| 24 | $smtp_secure = $form_data->getData()['smtp_secure'] ?? Config::$smtp_secure; | ||
| 25 | $smtp_username = $form_data->getData()['smtp_username'] ?? Config::$smtp_username; | ||
| 26 | $smtp_password = $form_data->getData()['smtp_password'] ?? Config::$smtp_password; | ||
| 27 | |||
| 28 | $email_from = $form_data->getData()['email_from'] ?? Config::$email_from; // adresse de l'expéditeur affichée | ||
| 29 | $email_from = empty($email_from) ? $smtp_username : $email_from; // si vide | ||
| 30 | $email_from_name = $form_data->getData()['email_from_name'] ?? Config::$email_from_name; // = nom de l'expéditeur affiché | ||
| 31 | $email_dest = $form_data->getData()['email_dest'] ?? Config::$email_dest; | ||
| 32 | $email_dest_name = $form_data->getData()['email_dest_name'] ?? Config::$email_dest_name; // = destinataire formulaire | ||
| 33 | |||
| 34 | try{ | ||
| 35 | // Paramètres du serveur | ||
| 36 | $mail->isSMTP(); | ||
| 37 | $mail->Host = $smtp_host; | ||
| 38 | $mail->SMTPAuth = true; | ||
| 39 | $mail->Port = 25; | ||
| 40 | |||
| 41 | if($mail->SMTPAuth){ | ||
| 42 | $mail->Username = $smtp_username; // e-mail | ||
| 43 | $mail->Password = $smtp_password; | ||
| 44 | $mail->SMTPSecure = $smtp_secure; // tls (starttls) ou ssl (smtps) | ||
| 45 | if($mail->SMTPSecure === 'tls'){ | ||
| 46 | $mail->Port = 587; | ||
| 47 | } | ||
| 48 | elseif($mail->SMTPSecure === 'ssl'){ | ||
| 49 | $mail->Port = 465; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | //var_dump($mail->smtpConnect());die; // test de connexion | ||
| 53 | |||
| 54 | // Expéditeur et destinataire | ||
| 55 | // $email_from, $email_from_name et $email_dest_name sont modifiables uniquement dans le config.ini pour l'instant | ||
| 56 | $mail->setFrom(strtolower($email_from), $email_from_name); | ||
| 57 | $mail->addAddress(strtolower($email_dest), $email_dest_name); | ||
| 58 | |||
| 59 | // Contenu | ||
| 60 | $mail->isHTML(true); | ||
| 61 | if($test_email){ | ||
| 62 | $mail->Subject = "TEST d'un envoi d'e-mail depuis le site web"; | ||
| 63 | } | ||
| 64 | else{ | ||
| 65 | $mail->Subject = 'Message envoyé par: ' . $name . ' (' . $email . ') depuis le site web'; | ||
| 66 | } | ||
| 67 | $mail->Body = $message; | ||
| 68 | $mail->AltBody = $message; | ||
| 69 | |||
| 70 | $mail->send(); | ||
| 71 | |||
| 72 | // copie en BDD | ||
| 73 | if(!$test_email && ($form_data->getData()['keep_emails'] ?? self::KEEP_EMAILS_DEFAULT)){ | ||
| 74 | $db_email = new Email($name, $email, $email_dest, $message, $form_data); | ||
| 75 | $entityManager->persist($db_email); | ||
| 76 | self::updateLastContactDate($entityManager, $email); | ||
| 77 | $entityManager->flush(); | ||
| 78 | } | ||
| 79 | |||
| 80 | return true; | ||
| 81 | } | ||
| 82 | catch(\Exception $e){ | ||
| 83 | echo "Le message n'a pas pu être envoyé. Erreur : {$e} <br> {$mail->ErrorInfo}"; | ||
| 84 | return false; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | static public function updateLastContactDate(EntityManager $entityManager, string $sender): void | ||
| 89 | { | ||
| 90 | foreach($entityManager->getRepository('App\Entity\Email')->findAll() as $email){ | ||
| 91 | $email->getSenderAddress() === $sender ? $email->updateLastContactDate() : null; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | // peut être appelée par bin/clean_emails_cron.php | ||
| 96 | static public function cleanEmails(EntityManager $entityManager): void | ||
| 97 | { | ||
| 98 | $emails = $entityManager->getRepository('App\Entity\Email')->findAll(); | ||
| 99 | foreach($emails as $email){ | ||
| 100 | if($email->getDeletionDate() < new \DateTime()){ | ||
| 101 | $entityManager->remove($email); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | $entityManager->flush(); | ||
| 105 | } | ||
| 106 | } \ No newline at end of file | ||
