Skip to content

Commit

Permalink
refactor: Added Class to Store SMTP Options
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Apr 8, 2024
1 parent 427d117 commit c5cfe5c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 25 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
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::SERVER_ADDRESS]);
} else {
$this->setSenderName('');
}

if (isset($options['sender-address'])) {
$this->setAddress($options['sender-address']);
if (isset($options[AccountOption::SERVER_ADDRESS])) {
$this->setAddress($options[AccountOption::SERVER_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 c5cfe5c

Please sign in to comment.