diff options
Diffstat (limited to 'src/EmailService.php')
-rw-r--r-- | src/EmailService.php | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/EmailService.php b/src/EmailService.php new file mode 100644 index 0000000..c1f74d1 --- /dev/null +++ b/src/EmailService.php | |||
@@ -0,0 +1,67 @@ | |||
1 | <?php | ||
2 | // src/EmailService.php | ||
3 | |||
4 | declare(strict_types=1); | ||
5 | |||
6 | use PHPMailer\PHPMailer\PHPMailer; | ||
7 | //use PHPMailer\PHPMailer\Exception; | ||
8 | use App\Entity\Email; | ||
9 | use Doctrine\ORM\EntityManager; | ||
10 | |||
11 | class EmailService | ||
12 | { | ||
13 | static public function send(EntityManager $entityManager, string $recipient, bool $true_email, string $name = '', string $email = '', string $message = ''): bool | ||
14 | { | ||
15 | $mail = new PHPMailer(true); // true => exceptions | ||
16 | $mail->CharSet = 'UTF-8'; | ||
17 | |||
18 | try{ | ||
19 | // Paramètres du serveur | ||
20 | $mail->isSMTP(); | ||
21 | $mail->Host = Config::$smtp_host; | ||
22 | $mail->SMTPAuth = true; | ||
23 | $mail->Port = 25; | ||
24 | |||
25 | if($mail->SMTPAuth){ | ||
26 | $mail->Username = Config::$smtp_username; // e-mail | ||
27 | $mail->Password = Config::$smtp_password; | ||
28 | $mail->SMTPSecure = Config::$smtp_secure; // tls (starttls) ou ssl (smtps) | ||
29 | if($mail->SMTPSecure === 'tls'){ | ||
30 | $mail->Port = 587; | ||
31 | } | ||
32 | elseif($mail->SMTPSecure === 'ssl'){ | ||
33 | $mail->Port = 465; | ||
34 | } | ||
35 | } | ||
36 | //var_dump($mail->smtpConnect());die; // test de connexion | ||
37 | |||
38 | // Expéditeur et destinataire | ||
39 | $mail->setFrom(strtolower(Config::$email_from), Config::$email_from_name); // expéditeur | ||
40 | $mail->addAddress(strtolower($recipient), Config::$email_dest_name); // destinataire | ||
41 | |||
42 | // Contenu | ||
43 | $mail->isHTML(true); | ||
44 | if($true_email){ | ||
45 | $mail->Subject = 'Message envoyé par: ' . $name . ' (' . $email . ') depuis le site web'; | ||
46 | } | ||
47 | else{ | ||
48 | $mail->Subject = "TEST d'un envoi d'e-mail depuis le site web"; | ||
49 | } | ||
50 | $mail->Body = $message; | ||
51 | $mail->AltBody = $message; | ||
52 | |||
53 | $mail->send(); | ||
54 | |||
55 | // copie en BDD | ||
56 | $db_email = new Email($email, Config::$email_dest, $message); | ||
57 | $entityManager->persist($db_email); | ||
58 | $entityManager->flush(); | ||
59 | |||
60 | return true; | ||
61 | } | ||
62 | catch(Exception $e){ | ||
63 | return false; | ||
64 | //echo "Le message n'a pas pu être envoyé. Erreur : {$mail->ErrorInfo}"; | ||
65 | } | ||
66 | } | ||
67 | } \ No newline at end of file | ||