Skip to content

Commit

Permalink
🔧 Configure SMTPSecure using the encryption config (Fixes #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x authored Mar 26, 2024
2 parents 6bd55d7 + 9b78aa8 commit ba0aeb6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ SMTP credentials can be found in the published `mail.php` config file and defaul

## Usage

For most configurations, you can simply set the following environment variables:
For most configurations, you can simply set the following variables in your environment:

```env
MAIL_HOST=
Expand All @@ -42,6 +42,12 @@ MAIL_FROM_ADDRESS=
MAIL_FROM_NAME=
```

If you need to change the default encryption from `tls`, you can set the encryption variable:

```env
MAIL_ENCRYPTION=ssl
```

Once the credentials are properly configured, you can send a test email using Acorn's CLI:

```sh
Expand Down
1 change: 1 addition & 0 deletions src/AcornMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ protected function configureMail(): void
$mail->Username = $this->config->get('username');
$mail->Password = $this->config->get('password');
$mail->Timeout = $this->config->get('timeout', $mail->Timeout);
$mail->SMTPSecure = $this->config->get('encryption', $mail->SMTPSecure);

$mail->setFrom(
$this->fromAddress(),
Expand Down
33 changes: 23 additions & 10 deletions src/Console/Commands/MailTestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,24 @@ class MailTestCommand extends Command
protected $description = 'Test the WordPress SMTP mailer';

/**
* The error collection.
*
* @var \Illuminate\Support\Collection
* The mail errors.
*/
protected array $errors = [];

/**
* The mail options.
*/
protected $errors = [];
protected array $options = [
'From' => 'From',
'FromName' => 'From Name',
'Host' => 'Host',
'Password' => 'Password',
'Port' => 'Port',
'SMTPSecure' => 'Encryption',
'Subject' => 'Subject',
'Timeout' => 'Timeout',
'Username' => 'Username',
];

/**
* Execute the console command.
Expand All @@ -49,9 +62,9 @@ public function handle()
$phpmailer->Debugoutput = fn ($error) => $instance->errors[] = $error;

$config = collect($phpmailer)
->filter(fn ($value, $key) => in_array($key, ['Host', 'Port', 'Username', 'Password', 'Timeout', 'FromName', 'From', 'Subject']))
->filter(fn ($value, $key) => in_array($key, array_keys($this->options)))
->map(fn ($value, $key) => $key === 'Password' ? Str::mask($value, '*', 0) : $value)
->map(fn ($value, $key) => "{$key}: ".((is_null($value) || empty($value)) ? 'Not set' : "<fg=blue>{$value}</>"));
->map(fn ($value, $key) => Str::finish($this->options[$key], ': ').(is_null($value) || empty($value) ? 'Not set' : "<fg=blue>{$value}</>"));

$this->components->bulletList($config);
});
Expand All @@ -78,21 +91,21 @@ public function handle()
return;
}

$this->errors = collect($this->errors)
$errors = collect($this->errors)
->filter(fn ($error) => Str::startsWith($error, 'SMTP Error: '));

if ($this->errors->isEmpty()) {
if ($errors->isEmpty()) {
$this->components->error('The test email failed to send.');

return;
}

$this->errors = $this->errors
$errors = $errors
->map(fn ($error) => " {$error}")
->map(fn ($error) => str_replace("\n", "\n ", $error));

$this->components->error('The test email failed to send. The following errors were encountered:');
$this->line($this->errors->first());
$this->line($errors->first());
}

/**
Expand Down

0 comments on commit ba0aeb6

Please sign in to comment.