Skip to content

Commit

Permalink
Add MailSenderInterface
Browse files Browse the repository at this point in the history
Send mail via MailSenderInterface
  • Loading branch information
sveneld committed Feb 26, 2024
1 parent 09089eb commit e4b7a34
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 13 deletions.
4 changes: 2 additions & 2 deletions actions-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ function changecity($userid, $city)

function resetpassword($number)
{
global $db, $systemname, $systemrules, $systemURL;
global $db, $mailer, $systemrules, $systemURL;

$number = $db->conn->real_escape_string(trim($number));

Expand All @@ -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.'));
}

Expand Down
39 changes: 28 additions & 11 deletions common.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use BikeShare\Mail\DebugMailSender;
use BikeShare\Mail\MailSenderInterface;
use BikeShare\Mail\PHPMailerMailSender;
use BikeShare\SmsConnector\SmsConnectorFactory;

require_once 'vendor/autoload.php';
Expand All @@ -16,6 +19,21 @@
DEBUG
);

/**
* @var MailSenderInterface $mailer
*/
if (DEBUG===TRUE) {
$mailer = new DebugMailSender();
} else {
$mailer = new PHPMailerMailSender(
$systemname,
$systememail,
$email,
new PHPMailer(false)
);
}


function error($message)
{
global $db;
Expand Down Expand Up @@ -275,27 +293,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);
}
}

function sendConfirmationEmail($emailto)
{
global $db, $dbpassword, $systemname, $systemrules, $systemURL;

global $db, $dbpassword, $systemname, $systemrules, $systemURL, $mailer;

$subject = _('Registration');

Expand All @@ -312,10 +329,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)
Expand Down
11 changes: 11 additions & 0 deletions src/Mail/DebugMailSender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace BikeShare\Mail;

class DebugMailSender implements MailSenderInterface
{
public function sendMail($recipient, $subject, $message)
{
echo $recipient, ' | ', $subject, ' | ', $message . PHP_EOL;
}
}
8 changes: 8 additions & 0 deletions src/Mail/MailSenderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace BikeShare\Mail;

interface MailSenderInterface
{
public function sendMail($recipient, $subject, $message);
}
58 changes: 58 additions & 0 deletions src/Mail/PHPMailerMailSender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace BikeShare\Mail;

class PHPMailerMailSender implements MailSenderInterface
{
/**
* @var string
*/
private $fromEmail;
/**
* @var string
*/
private $fromName;
/**
* @var array
*/
private $emailConfig;
/**
* @var \PHPMailer
*/
private $mailer;

public function __construct(
$fromEmail = null,
$fromName = null,
array $emailConfig = null,
\PHPMailer $mailer = null
) {

$this->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();
}
}
19 changes: 19 additions & 0 deletions tests/Mail/DebugMailSenderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Test\BikeShare\Mail;

use BikeShare\Mail\DebugMailSender;
use PHPUnit\Framework\TestCase;

class DebugMailSenderTest extends TestCase
{
public function testSendMail()
{
$recipient = 'recipient';
$subject = 'subject';
$message = 'message';
$mailer = new DebugMailSender();
$mailer->sendMail($recipient, $subject, $message);
$this->expectOutputString($recipient . ' | ' . $subject . ' | ' . $message . PHP_EOL);
}
}
74 changes: 74 additions & 0 deletions tests/Mail/PHPMailerSenderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Test\BikeShare\Mail;

use BikeShare\Mail\PHPMailerMailSender;
use PHPUnit\Framework\TestCase;

class PHPMailerSenderTest extends TestCase
{
/**
* @var \PHPMailer|\PHPUnit_Framework_MockObject_MockObject
*/
private $mailer;
/**
* @var PHPMailerMailSender
*/
private $mailSender;

protected function setUp()
{
$email = [
'smtp' => '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);
}
}

0 comments on commit e4b7a34

Please sign in to comment.