-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature to read mail configs from database
- Loading branch information
Adam Nielsen
committed
Apr 10, 2021
1 parent
0677ef0
commit af1c690
Showing
8 changed files
with
327 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
<?php | ||
|
||
|
||
namespace IWasHereFirst2\LaravelMultiMail; | ||
|
||
|
||
use \IWasHereFirst2\LaravelMultiMail\MailSettings; | ||
use IWasHereFirst2\LaravelMultiMail\Models\EmailAccount; | ||
|
||
class DatabaseConfigMailSettings implements MailSettings | ||
{ | ||
private $account; | ||
|
||
private $provider; | ||
|
||
/** | ||
* Name from mail sender. | ||
* | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* Email from mail sender. | ||
* Has to be set in `config/multimail.php` | ||
* | ||
* @var string | ||
*/ | ||
private $email; | ||
|
||
|
||
/** | ||
* Email settings. | ||
* This may include credentials, name, provider. | ||
* | ||
* @var array | ||
*/ | ||
private $settings; | ||
|
||
public function initialize($key): DatabaseConfigMailSettings | ||
{ | ||
$this->parseEmail($key); | ||
|
||
try { | ||
$this->account = EmailAccount::where('email', '=', $this->email)->firstOrFail(); | ||
} catch (\Exception $e) { | ||
throw new Exceptions\EmailNotInConfigException($this->email); | ||
} | ||
|
||
if (empty($this->name)) { | ||
$this->name = $this->account->from_mail; | ||
} | ||
|
||
$this->loadProvider(); | ||
|
||
// If credentials are empty, load default values. | ||
// This makes local testing for many emails | ||
// very convenient. | ||
if ($this->isEmpty()) { | ||
$this->loadDefault(); | ||
} | ||
|
||
return $this; | ||
} | ||
/** | ||
* Check if log driver is used. | ||
* | ||
* @return boolean | ||
*/ | ||
public function isLogDriver() | ||
{ | ||
return (isset($this->provider['driver']) && $this->provider['driver'] == 'log'); | ||
} | ||
|
||
/** | ||
* Get provider. | ||
* | ||
* @return array | ||
*/ | ||
public function getProvider() | ||
{ | ||
return $this->provider; | ||
} | ||
|
||
/** | ||
* Get setting. | ||
* | ||
* @return array | ||
*/ | ||
public function getSetting() | ||
{ | ||
return $this->account->toArray(); | ||
} | ||
|
||
/** | ||
* Return email of sender. | ||
* | ||
* @return string | ||
*/ | ||
public function getFromEmail() | ||
{ | ||
return $this->account->from_mail ?? $this->email; | ||
} | ||
|
||
/** | ||
* Return name of sender. | ||
* | ||
* @return string | ||
*/ | ||
public function getFromName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* Return email of sender. | ||
* | ||
* @return string | ||
*/ | ||
public function getReplyEmail() | ||
{ | ||
return $this->account->reply_to_mail; | ||
} | ||
|
||
/** | ||
* Return name of sender. | ||
* | ||
* @return string | ||
*/ | ||
public function getReplyName() | ||
{ | ||
return $this->account->reply_to_name; | ||
} | ||
|
||
public function getEmail() | ||
{ | ||
return $this->email; | ||
} | ||
|
||
/** | ||
* Check if email, pass and username are not empty | ||
* | ||
* @return boolean | ||
*/ | ||
private function isEmpty() | ||
{ | ||
return (empty($this->email) || empty($this->account) || empty($this->account->pass) || empty($this->account->username)); | ||
} | ||
|
||
/** | ||
* Load default setting. If default setting is | ||
* invalid throw exception | ||
* | ||
* @return void | ||
*/ | ||
private function loadDefault() | ||
{ | ||
$this->account = (object) config('multimail.emails.default'); | ||
|
||
$this->loadProvider(); | ||
|
||
if ((!isset($this->provider['driver']) || $this->provider['driver'] != 'log') && (empty($this->acount->pass) || empty($this->account->username))) { | ||
throw new Exceptions\NoDefaultException($this->email); | ||
} | ||
} | ||
|
||
/** | ||
* Parse $key into email and possible from name | ||
* | ||
* @param mixed string/array | ||
* @return void | ||
*/ | ||
private function parseEmail($key) | ||
{ | ||
if (!is_array($key)) { | ||
$this->email = $key; | ||
return; | ||
} | ||
|
||
$this->name = $key['name'] ?? null; | ||
|
||
if (empty($key['email'])) { | ||
throw new Exceptions\InvalidConfigKeyException; | ||
} | ||
|
||
$this->email = $key['email']; | ||
} | ||
|
||
|
||
private function loadProvider() | ||
{ | ||
|
||
if (!empty($this->account->provider)) { | ||
$this->provider = $this->account->provider->toArray(); | ||
} | ||
|
||
if (empty($this->provider)) { | ||
$this->provider = config('multimail.provider.default'); | ||
} | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -197,4 +197,4 @@ private function loadProvider() | |
$this->provider = config('multimail.provider.default'); | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Migrations/2021_04_10_141537_create_email_providers_table.php
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,34 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateEmailProvidersTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('email_providers', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string('host'); | ||
$table->string('port'); | ||
$table->string('encryption'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('email_providers'); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Migrations/2021_04_10_141626_create_email_accounts_table.php
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,40 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateEmailAccountsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('email_accounts', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string('email'); | ||
$table->string('pass'); | ||
$table->string('username')->nullable(); | ||
$table->string('from_name')->nullable(); | ||
$table->string('reply_to_mail')->nullable(); | ||
$table->string('reply_to_name')->nullable(); | ||
$table->bigInteger('provider_id')->unsigned()->nullable(); | ||
$table->timestamps(); | ||
|
||
$table->foreign('provider_id')->references('id')->on('email_providers'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('email_accounts'); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace IWasHereFirst2\LaravelMultiMail\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class EmailAccount extends Model | ||
{ | ||
public function provider() | ||
{ | ||
return $this->belongsTo(EmailProvider::class); | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace IWasHereFirst2\LaravelMultiMail\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class EmailProvider extends Model | ||
{ | ||
// | ||
} |
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