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