Skip to content

Commit

Permalink
Fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
dietercoopman authored and github-actions[bot] committed Aug 5, 2022
1 parent 0980b25 commit 34188d2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
55 changes: 27 additions & 28 deletions src/Mailspfchecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
use SPFLib\Decoder;
use SPFLib\Term\Mechanism;


class Mailspfchecker
{

private array $spfRecords = [];
private string $server = "";

private string $server = '';

private string $sendingMailserver;

public function __construct()
Expand All @@ -21,13 +21,14 @@ public function __construct()

public function canISendAs(string $emailOrDomain): bool
{
return ($this->check($emailOrDomain, true) === "pass");
return $this->check($emailOrDomain, true) === 'pass';
}

public function using(string $server): self
{
$this->server = $server;
$this->setSendingMailServer();

return $this;
}

Expand All @@ -38,100 +39,98 @@ private function getDomain(string $emailOrDomain): string
} else {
$domain = $emailOrDomain;
}

return $domain;
}

public function check(string $emailOrDomain, $returnCode = false): mixed
{
$domain = $this->getDomain($emailOrDomain);
$domain = $this->getDomain($emailOrDomain);
$decoder = new \SPFLib\Decoder();

$code = "error";
$code = 'error';
$codes = [];


foreach ($this->spfRecords as $spfValueToCheck) {
$record = $decoder->getRecordFromDomain($domain);
if ($record) {
foreach ($record->getTerms() as $term) {
$codes[$spfValueToCheck] = "error";
$codes[$spfValueToCheck] = 'error';
if (strstr(strtolower($term), strtolower($spfValueToCheck))) {
$codes[$spfValueToCheck] = "pass";
$codes[$spfValueToCheck] = 'pass';
break;
}
}
} else {
$codes[$spfValueToCheck] = "error";
$codes[$spfValueToCheck] = 'error';
}
}

if (in_array("pass", $codes)) {
$code = "pass";
if (in_array('pass', $codes)) {
$code = 'pass';
}

return ($returnCode) ? $code : $codes;

}

private function setSendingMailServer(): void
{
if (!blank($this->server)) {
if (! blank($this->server)) {
$sendingMailserver = $this->server;
} else {
$sendingMailserver = config('mail.mailers.smtp.host');
}

//if the address is localhost, then check wan address via icanhazip
if ($sendingMailserver == "127.0.0.1" || $sendingMailserver == "localhost") {
$sendingMailserver = trim(file_get_contents("https://icanhazip.com/"));
if ($sendingMailserver == '127.0.0.1' || $sendingMailserver == 'localhost') {
$sendingMailserver = trim(file_get_contents('https://icanhazip.com/'));
}

$this->sendingMailserver = $sendingMailserver;
$this->spfRecords = $this->retreiveSpfRecordsFromSendingServer();


$this->spfRecords = $this->retreiveSpfRecordsFromSendingServer();
}

private function retreiveSpfRecordsFromSendingServer(): array
{
$spfRecords = [];
$server = $this->sendingMailserver;
$server = $this->sendingMailserver;

if (!filter_var($server, FILTER_VALIDATE_IP)) {

$checker = new Decoder();
if (! filter_var($server, FILTER_VALIDATE_IP)) {
$checker = new Decoder();
$explodedServerUrl = explode('.', $server);
array_shift($explodedServerUrl);
$domain = implode(".", $explodedServerUrl);
$domain = implode('.', $explodedServerUrl);
if ($domain) {
$record = $checker->getRecordFromDomain($domain);
foreach ($record->getTerms() as $term) {
if ($term instanceof Mechanism\IncludeMechanism || $term instanceof Mechanism\AMechanism) {
$domainSpec = (string)$term->getDomainSpec();
$domainSpec = (string) $term->getDomainSpec();
if (strstr($domainSpec, $domain)) {
$spfRecords[] = $domainSpec;
}
}
}
}
}

return array_unique($spfRecords);
}

public function howCanISendAs(string $emailOrDomain, string $overRuleMessage = null): string
{
list($name, $value) = array_values($this->buildDnsString($emailOrDomain));
[$name, $value] = array_values($this->buildDnsString($emailOrDomain));
if ($overRuleMessage) {
return str_replace([':name', ':value'], [$name, $value], $overRuleMessage);
}

return "Generate a txt-record with a name of <strong>{$name}</strong> and the value <strong>{$value}</strong>";
}

public function buildDnsString(string $emailOrDomain): array
{
$domain = $this->getDomain($emailOrDomain);
$record = new \SPFLib\Record();
if (!empty($this->spfRecords)) {
if (! empty($this->spfRecords)) {
foreach ($this->spfRecords as $server) {
if (filter_var($server, FILTER_VALIDATE_IP)) {
$record->addTerm(new Mechanism\Ip4Mechanism(Mechanism::QUALIFIER_PASS, IPv4::parseString($server)));
Expand All @@ -145,6 +144,6 @@ public function buildDnsString(string $emailOrDomain): array

$record->addTerm(new Mechanism\AllMechanism(Mechanism::QUALIFIER_FAIL));

return ["name" => $domain, "value" => (string)$record];
return ['name' => $domain, 'value' => (string) $record];
}
}
2 changes: 1 addition & 1 deletion src/MailspfcheckerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Dietercoopman\Mailspfchecker;

use Dietercoopman\Mailspfchecker\Commands\MailspfcheckerCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Dietercoopman\Mailspfchecker\Commands\MailspfcheckerCommand;

class MailspfcheckerServiceProvider extends PackageServiceProvider
{
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Dietercoopman\Mailspfchecker\Tests;

use Dietercoopman\Mailspfchecker\MailspfcheckerServiceProvider;
use Illuminate\Database\Eloquent\Factories\Factory;
use Orchestra\Testbench\TestCase as Orchestra;
use Dietercoopman\Mailspfchecker\MailspfcheckerServiceProvider;

class TestCase extends Orchestra
{
Expand Down

0 comments on commit 34188d2

Please sign in to comment.