-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send mail via MailSenderInterface
- Loading branch information
Showing
7 changed files
with
200 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |