Skip to content

Commit

Permalink
Merge pull request #22 from WebFiori/dev
Browse files Browse the repository at this point in the history
refactor: Added Class to Store SMTP Options
  • Loading branch information
usernane authored Apr 8, 2024
2 parents 0fafcf3 + 1659bcc commit 91f377b
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 60 deletions.
15 changes: 8 additions & 7 deletions example/sendMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

require '../vendor/autoload.php';

use webfiori\email\AccountOption;
use webfiori\email\Email;
use webfiori\email\SMTPAccount;

//First, create new SMTP account that holds SMTP connection information.
$smtp = new SMTPAccount([
'port' => 465,
AccountOption::PORT => 465,
//Replace server address with your mail server address
'server-address' => 'mail.example.com',
AccountOption::SENDER_ADDRESS => 'mail.example.com',
//Replace server username with your mail server username
'user' => '[email protected]',
'pass' => 'KnvcbxFYCz77',
'sender-name' => 'Ibrahim',
AccountOption::USERNAME => '[email protected]',
AccountOption::PASSWORD => 'KnvcbxFYCz77',
AccountOption::SENDER_NAME => 'Ibrahim',
//Replace sender address with your mail server sender address
'sender-address' => '[email protected]',
'account-name' => 'no-reply'
AccountOption::SERVER_ADDRESS => '[email protected]',
AccountOption::NAME => 'no-reply'
]);

//Second, create your actual email. using the account that was just created to
Expand Down
43 changes: 22 additions & 21 deletions tests/webfiori/tests/mail/EmailMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace webfiori\tests\mail;

use PHPUnit\Framework\TestCase;
use webfiori\email\AccountOption;
use webfiori\email\Email;
use webfiori\email\exceptions\SMTPException;
use webfiori\email\SendMode;
Expand All @@ -16,31 +17,31 @@
*/
class EmailMessageTest extends TestCase {
private $acc00 = [
'port' => 587,
'server-address' => 'outlook.office365.com',
'user' => '[email protected]',
'password' => '???',
'sender-name' => 'Ibrahim',
'sender-address' => '[email protected]',
'account-name' => 'no-reply'
AccountOption::PORT => 587,
AccountOption::SERVER_ADDRESS => 'outlook.office365.com',
AccountOption::USERNAME => '[email protected]',
AccountOption::PASSWORD => '???',
AccountOption::SENDER_NAME => 'Ibrahim',
AccountOption::SENDER_ADDRESS => '[email protected]',
AccountOption::NAME => 'no-reply'
];
private $acc01 = [
'port' => 465,
'server-address' => 'mail.programmingacademia.com',
'user' => '[email protected]',
'pass' => 'KnvcbxFYCz77',
'sender-name' => 'Ibrahim',
'sender-address' => '[email protected]',
'account-name' => 'no-reply2'
AccountOption::PORT => 465,
AccountOption::SERVER_ADDRESS => 'mail.programmingacademia.com',
AccountOption::USERNAME => '[email protected]',
AccountOption::PASSWORD => 'KnvcbxFYCz77',
AccountOption::SENDER_NAME => 'Ibrahim',
AccountOption::SENDER_ADDRESS => '[email protected]',
AccountOption::NAME => 'no-reply2'
];
private $acc02 = [
'port' => 465,
'server-address' => 'mail.programmingacademia.com',
'user' => '[email protected]',
'pass' => '2233',
'sender-name' => 'Ibrahim',
'sender-address' => '[email protected]',
'account-name' => 'no-reply2'
AccountOption::PORT => 465,
AccountOption::SERVER_ADDRESS => 'mail.programmingacademia.com',
AccountOption::USERNAME => '[email protected]',
AccountOption::PASSWORD => '2233',
AccountOption::SENDER_NAME => 'Ibrahim',
AccountOption::SENDER_ADDRESS => '[email protected]',
AccountOption::NAME => 'no-reply2'
];
/**
* @test
Expand Down
29 changes: 15 additions & 14 deletions tests/webfiori/tests/mail/SMTPAccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace webfiori\tests\mail;

use PHPUnit\Framework\TestCase;
use webfiori\email\AccountOption;
use webfiori\email\SMTPAccount;
/**
* A test class for testing the class 'webfiori\framework\mail\SMTPAccount'.
Expand All @@ -26,13 +27,13 @@ public function test00() {
*/
public function test01() {
$acc = new SMTPAccount([
'user' => '[email protected]',
'pass' => '123456',
'port' => 25,
'server-address' => 'mail.examplex.com',
'sender-name' => 'Example Sender',
'sender-address' => '[email protected]',
'account-name' => 'no-reply'
AccountOption::USERNAME => '[email protected]',
AccountOption::PASSWORD => '123456',
AccountOption::PORT => 25,
AccountOption::SERVER_ADDRESS => 'mail.examplex.com',
AccountOption::SENDER_NAME => 'Example Sender',
AccountOption::SENDER_ADDRESS => '[email protected]',
AccountOption::NAME => 'no-reply'
]);
$this->assertSame(25,$acc->getPort());
$this->assertEquals('[email protected]',$acc->getAddress());
Expand All @@ -47,13 +48,13 @@ public function test01() {
*/
public function test02() {
$acc = new SMTPAccount([
'username' => '[email protected]',
'password' => '123456',
'port' => 25,
'server-address' => 'mail.examplex.com',
'sender-name' => 'Example Sender',
'sender-address' => '[email protected]',
'account-name' => 'no-reply'
AccountOption::USERNAME => '[email protected]',
AccountOption::PASSWORD => '123456',
AccountOption::PORT => 25,
AccountOption::SERVER_ADDRESS => 'mail.examplex.com',
AccountOption::SENDER_NAME => 'Example Sender',
AccountOption::SENDER_ADDRESS => '[email protected]',
AccountOption::NAME => 'no-reply'
]);
$this->assertSame(25,$acc->getPort());
$this->assertEquals('[email protected]',$acc->getAddress());
Expand Down
49 changes: 49 additions & 0 deletions webfiori/email/AccountOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* This file is licensed under MIT License.
*
* Copyright (c) 2024 Ibrahim BinAlshikh
*
* For more information on the license, please visit:
* https://github.com/WebFiori/.github/blob/main/LICENSE
*
*/
namespace webfiori\email;

/**
* A class that holds constants that represents SMTP account options.
*
* @author Ibrahim
*/
class AccountOption {
/**
* An option which is used to set the address of SMTP server.
*/
const SERVER_ADDRESS = 'server-address';
/**
* An option which is used to set SMTP server port.
*/
const PORT = 'port';
/**
* An option which is used to set the username at which it is used to log in to SMTP server.
*/
const USERNAME = 'user';
/**
* An option which is used to set the password of the account.
*/
const PASSWORD = 'pass';
/**
* An option which is used to set the name of the sender that will appear when the
* message is sent.
*/
const SENDER_NAME = 'sender-name';
/**
* An option which is used to set the address that will appear when the
* message is sent. Usually, it is the same as the username.
*/
const SENDER_ADDRESS = 'sender-address';
/**
* An option which is used to set a unique name for the account.
*/
const NAME = 'account-name';
}
32 changes: 14 additions & 18 deletions webfiori/email/SMTPAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,48 +77,44 @@ class SMTPAccount {
*
*/
public function __construct(array $options = []) {
if (isset($options['port'])) {
$this->setPort($options['port']);
if (isset($options[AccountOption::PORT])) {
$this->setPort($options[AccountOption::PORT]);
} else {
$this->setPort(465);
}

if (isset($options['user'])) {
$this->setUsername($options['user']);
} else if (isset($options['username'])) {
$this->setUsername($options['username']);
if (isset($options[AccountOption::USERNAME])) {
$this->setUsername($options[AccountOption::USERNAME]);
} else {
$this->setUsername('');
}

if (isset($options['pass'])) {
$this->setPassword($options['pass']);
} else if (isset($options['password'])) {
$this->setPassword($options['password']);
if (isset($options[AccountOption::PASSWORD])) {
$this->setPassword($options[AccountOption::PASSWORD]);
} else {
$this->setPassword('');
}

if (isset($options['server-address'])) {
$this->setServerAddress($options['server-address']);
if (isset($options[AccountOption::SERVER_ADDRESS])) {
$this->setServerAddress($options[AccountOption::SERVER_ADDRESS]);
} else {
$this->setServerAddress('');
}

if (isset($options['sender-name'])) {
$this->setSenderName($options['sender-name']);
if (isset($options[AccountOption::SENDER_NAME])) {
$this->setSenderName($options[AccountOption::SENDER_NAME]);
} else {
$this->setSenderName('');
}

if (isset($options['sender-address'])) {
$this->setAddress($options['sender-address']);
if (isset($options[AccountOption::SENDER_ADDRESS])) {
$this->setAddress($options[AccountOption::SENDER_ADDRESS]);
} else {
$this->setAddress('');
}

if (isset($options['account-name'])) {
$this->setAccountName($options['account-name']);
if (isset($options[AccountOption::NAME])) {
$this->setAccountName($options[AccountOption::NAME]);
} else {
$this->setAccountName($this->getSenderName());
}
Expand Down
9 changes: 9 additions & 0 deletions webfiori/email/SendMode.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?php
/**
* This file is licensed under MIT License.
*
* Copyright (c) 2024 Ibrahim BinAlshikh
*
* For more information on the license, please visit:
* https://github.com/WebFiori/.github/blob/main/LICENSE
*
*/
namespace webfiori\email;

/**
Expand Down

0 comments on commit 91f377b

Please sign in to comment.