From e75f2e27cabfa3a866fc9594d3ee4a4f104846e6 Mon Sep 17 00:00:00 2001 From: Sveneld Date: Mon, 26 Feb 2024 21:39:02 +0100 Subject: [PATCH] Add MailSenderInterface Send mail via MailSenderInterface --- actions-web.php | 4 +- common.php | 67 ++++++++++++--------------- src/Mail/DebugMailSender.php | 11 +++++ src/Mail/MailSenderInterface.php | 8 ++++ src/Mail/PHPMailerMailSender.php | 64 ++++++++++++++++++++++++++ tests/Mail/DebugMailSenderTest.php | 19 ++++++++ tests/Mail/PHPMailerSenderTest.php | 74 ++++++++++++++++++++++++++++++ 7 files changed, 207 insertions(+), 40 deletions(-) create mode 100644 src/Mail/DebugMailSender.php create mode 100644 src/Mail/MailSenderInterface.php create mode 100644 src/Mail/PHPMailerMailSender.php create mode 100644 tests/Mail/DebugMailSenderTest.php create mode 100644 tests/Mail/PHPMailerSenderTest.php diff --git a/actions-web.php b/actions-web.php index 03bd975..652a5a6 100644 --- a/actions-web.php +++ b/actions-web.php @@ -740,7 +740,7 @@ function changecity($userid, $city) function resetpassword($number) { - global $db, $systemname, $systemrules, $systemURL; + global $db, $mailer, $systemname, $systemrules, $systemURL; $number = $db->conn->real_escape_string(trim($number)); @@ -766,7 +766,7 @@ function resetpassword($number) _('Your password has been reset successfully.') . "\n\n" . _('Your new password is:') . "\n" . $password; - sendEmail($email, $subject, $message); + $mailer->send($email, $subject, $message); response(_('Your password has been reset successfully.') . ' ' . _('Check your email.')); } diff --git a/common.php b/common.php index 0c055c7..3031e7b 100644 --- a/common.php +++ b/common.php @@ -1,5 +1,8 @@ isSMTP(); // Set mailer to use SMTP - //$mail->SMTPDebug = 2; - $mail->Host = $email["smtp"]; // Specify main and backup SMTP servers - $mail->Username = $email["user"]; // SMTP username - $mail->Password = $email["pass"]; // SMTP password - $mail->SMTPAuth = true; // Enable SMTP authentication - $mail->SMTPSecure = "ssl"; // Enable SSL - $mail->Port = 465; // TCP port to connect to - $mail->CharSet = "UTF-8"; - $mail->From = $systememail; - $mail->FromName = $systemname; - $mail->addAddress($emailto); // Add a recipient - $mail->addBCC($systememail); // Add a recipient - $mail->Subject = $subject; - $mail->Body = $message; - if (DEBUG === FALSE) { - $mail->send(); - } else { - echo $email, ' | ', $subject, ' | ', $message; - } -} - function sendSMS($number,$text) { @@ -275,27 +267,26 @@ function checkstandname($stand) **/ function notifyAdmins($message, $notificationtype = 0) { - global $db, $systemname, $watches; + global $db, $systemname, $watches, $mailer; $result = $db->query('SELECT number,mail FROM users where privileges & 2 != 0'); while ($row = $result->fetch_assoc()) { if ($notificationtype == 0) { sendSMS($row['number'], $message); - sendEmail($watches['email'], $systemname . ' ' . _('notification'), $message); + $mailer->send($watches['email'], $systemname . ' ' . _('notification'), $message); } else { - sendEmail($row['mail'], $systemname . ' ' . _('notification'), $message); + $mailer->send($row['mail'], $systemname . ' ' . _('notification'), $message); } - } - -//copy to Trello board -- might be added as a person instead + }//copy to Trello board -- might be added as a person instead if ($notificationtype == 0) { - sendEmail('cyklokoalicia1+q31wfjphbgkuelf19hlb@boards.trello.com', $message, $message); + $mailer->send('cyklokoalicia1+q31wfjphbgkuelf19hlb@boards.trello.com', $message, $message); } } function sendConfirmationEmail($emailto) { - global $db, $dbpassword, $systemname, $systemrules, $systemURL; + + global $db, $dbpassword, $systemname, $systemrules, $systemURL, $mailer; $subject = _('Registration'); @@ -312,10 +303,10 @@ function sendConfirmationEmail($emailto) $names = preg_split("/[\s,]+/", $row['userName']); $firstname = $names[0]; $message = _('Hello') . ' ' . $firstname . ",\n\n" . - _('you have been registered into community bike share system') . ' ' . $systemname . ".\n\n" . - _('System rules are available here:') . "\n" . $systemrules . "\n\n" . - _('By clicking the following link you agree to the System rules:') . "\n" . $systemURL . 'agree.php?key=' . $userKey; - sendEmail($emailto, $subject, $message); + _('you have been registered into community bike share system') . ' ' . $systemname . ".\n\n" . + _('System rules are available here:') . "\n" . $systemrules . "\n\n" . + _('By clicking the following link you agree to the System rules:') . "\n" . $systemURL . 'agree.php?key=' . $userKey; + $mailer->send($emailto, $subject, $message); } function confirmUser($userKey) diff --git a/src/Mail/DebugMailSender.php b/src/Mail/DebugMailSender.php new file mode 100644 index 0000000..b3bb01c --- /dev/null +++ b/src/Mail/DebugMailSender.php @@ -0,0 +1,11 @@ +fromEmail = $fromEmail; + $this->fromName = $fromName; + $this->emailConfig = $emailConfig; + $this->mailer = $mailer; + } + + public function sendMail($recipient, $subject, $message) + { + $this->mailer->clearAllRecipients(); + + $this->mailer->isSMTP(); // Set mailer to use SMTP + //$this->mailer->SMTPDebug = 2; + $this->mailer->Host = $this->emailConfig["smtp"]; // Specify main and backup SMTP servers + $this->mailer->Username = $this->emailConfig["user"]; // SMTP username + $this->mailer->Password = $this->emailConfig["pass"]; // SMTP password + $this->mailer->SMTPAuth = true; // Enable SMTP authentication + $this->mailer->SMTPSecure = "ssl"; // Enable SSL + $this->mailer->Port = 465; // TCP port to connect to + $this->mailer->CharSet = "UTF-8"; + $this->mailer->From = $this->fromEmail; + $this->mailer->FromName = $this->fromName; + $this->mailer->addAddress($recipient); // Add a recipient + $this->mailer->addBCC($this->fromEmail); // Add a recipient + $this->mailer->Subject = $subject; + $this->mailer->Body = $message; + $this->mailer->send(); + } +} diff --git a/tests/Mail/DebugMailSenderTest.php b/tests/Mail/DebugMailSenderTest.php new file mode 100644 index 0000000..6bc8b5c --- /dev/null +++ b/tests/Mail/DebugMailSenderTest.php @@ -0,0 +1,19 @@ +sendMail($recipient, $subject, $message); + $this->expectOutputString($recipient . ' | ' . $subject . ' | ' . $message . PHP_EOL); + } +} diff --git a/tests/Mail/PHPMailerSenderTest.php b/tests/Mail/PHPMailerSenderTest.php new file mode 100644 index 0000000..f4ad2bd --- /dev/null +++ b/tests/Mail/PHPMailerSenderTest.php @@ -0,0 +1,74 @@ + 'smtp', + 'user' => 'user', + 'pass' => 'pass', + ]; + + $this->mailer = $this->createMock(\PHPMailer::class); + $this->mailSender = new PHPMailerMailSender( + 'fromEmail', + 'fromName', + $email, + $this->mailer + ); + } + + public function testSendMail() + { + $recipient = 'recipient'; + $subject = 'subject'; + $message = 'message'; + + $this->mailer + ->expects($this->once()) + ->method('clearAllRecipients'); + $this->mailer + ->expects($this->once()) + ->method('isSMTP'); + $this->mailer + ->expects($this->once()) + ->method('addAddress') + ->with($recipient); + $this->mailer + ->expects($this->once()) + ->method('addBCC') + ->with('fromEmail'); + $this->mailer + ->expects($this->once()) + ->method('send'); + + $this->mailSender->sendMail($recipient, $subject, $message); + + $this->assertEquals($this->mailer->Host, 'smtp'); + $this->assertEquals($this->mailer->Username, 'user'); + $this->assertEquals($this->mailer->Password, 'pass'); + $this->assertEquals($this->mailer->SMTPAuth, true); + $this->assertEquals($this->mailer->SMTPSecure, 'ssl'); + $this->assertEquals($this->mailer->Port, 465); + $this->assertEquals($this->mailer->CharSet, 'UTF-8'); + $this->assertEquals($this->mailer->From, 'fromEmail'); + $this->assertEquals($this->mailer->FromName, 'fromName'); + $this->assertEquals($this->mailer->Subject, $subject); + $this->assertEquals($this->mailer->Body, $message); + } +}