Skip to content

Commit 455a2b8

Browse files
committed
[IMPROVES] Add Mail return statement
1 parent bbd2d35 commit 455a2b8

2 files changed

Lines changed: 26 additions & 11 deletions

File tree

‎App/Manager/Mail/MailManager.php‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class MailManager extends AbstractManager
2626
* @Param string $subject -> subject of mail
2727
* @Param string $body -> html content with data
2828
*/
29-
public function sendMailSMTP(string $receiver, string $subject, string $body, ?string $senderMail, ?string $senderName): void
29+
public function sendMailSMTP(string $receiver, string $subject, string $body, ?string $senderMail, ?string $senderName): bool
3030
{
3131
$config = MailModel::getInstance()->getConfig();
3232

@@ -66,39 +66,46 @@ public function sendMailSMTP(string $receiver, string $subject, string $body, ?s
6666
$mail->Body = $body . '<br>' . $config?->getFooter();
6767

6868
// Send mail
69-
$mail->send();
69+
$status = $mail->send();
70+
71+
if (!$status) {
72+
error_log("Message could not be sent. Mailer Error: $mail->ErrorInfo");
73+
}
74+
75+
return $status;
7076
} catch (Exception $e) {
7177
echo "Message could not be sent. Mailer Error: $e";
7278
error_log("Message could not be sent. Mailer Error: $e");
79+
return false;
7380
}
7481
}
7582

7683
/**
7784
* @param string $receiver
7885
* @param string $object
7986
* @param string $body
80-
* @return void
87+
* @return bool
8188
* @desc Send mail with the Default php function
8289
*/
83-
private function sendMailPHP(string $receiver, string $object, string $body): void
90+
private function sendMailPHP(string $receiver, string $object, string $body): bool
8491
{
85-
mail($receiver, $object, $body);
92+
return mail($receiver, $object, $body);
8693
}
8794

8895
/**
8996
* @param string $receiver
9097
* @param string $object
9198
* @param string $body
92-
* @return void
99+
* @return bool
93100
* @desc Send mail (SMTP OR PHP MAIL function)
94101
*/
95-
public function sendMail(string $receiver, string $object, string $body): void
102+
public function sendMail(string $receiver, string $object, string $body): bool
96103
{
97104
if (MailModel::getInstance()->getConfig() !== null && MailModel::getInstance()->getConfig()->isEnable()) {
98-
$this->sendMailSMTP($receiver, $object, $body, '', '');
99-
} else {
100-
$this->sendMailPHP($receiver, $object, $body);
105+
return $this->sendMailSMTP($receiver, $object, $body, '', '');
101106
}
107+
108+
return $this->sendMailPHP($receiver, $object, $body);
102109
}
103110

104111
/**

‎App/Package/Core/Controllers/MailController.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use CMW\Utils\Redirect;
1616
use CMW\Utils\Utils;
1717
use JetBrains\PhpStorm\NoReturn;
18+
use function http_response_code;
19+
use function is_null;
1820

1921
/**
2022
* Class: @MailController
@@ -80,6 +82,12 @@ private function testMailConfigurationPost(): void
8082
}
8183

8284
$receiver = FilterManager::filterInputStringPost('receiver');
83-
MailManager::getInstance()->sendMail($receiver, 'Test CraftMyWebsite - MAILS', '<p>Hello World !</p>');
85+
$status = MailManager::getInstance()->sendMail($receiver, 'Test CraftMyWebsite - MAILS', '<p>Hello World !</p>');
86+
87+
if ($status) {
88+
http_response_code(204);
89+
} else {
90+
http_response_code(500);
91+
}
8492
}
8593
}

0 commit comments

Comments
 (0)