option('company');
if ($companyId < 1) {
$this->output->writeln('Invalid --company value. Must be a positive integer.');
return 1;
}
// Resolve password: --password-stdin takes precedence over --password
$password = null;
if ($this->option('password-stdin')) {
$stdin = fgets(STDIN);
if ($stdin === false) {
$this->output->writeln('Could not read password from STDIN.');
return 1;
}
// Trim only the trailing newline, preserve all other characters
$password = rtrim($stdin, "\n\r");
} elseif ($this->option('password') !== null && $this->option('password') !== '') {
$password = $this->option('password');
}
// Map of option name => CompanySettings key
$fieldMap = [
'host' => 's_email_notify_host',
'port' => 's_email_notify_port',
'username' => 's_email_notify_username',
'encryption' => 's_email_notify_encryption',
'from-address' => 's_email_notify_mail_from_address',
'from-name' => 's_email_notify_mail_from_name',
];
$updated = [];
$skipped = [];
// Persist plain-text fields (only if the option was actually supplied)
foreach ($fieldMap as $option => $settingKey) {
$value = $this->option($option);
if ($value !== null && $value !== '') {
CompanySettings::set($settingKey, $value, $companyId);
$updated[$settingKey] = $value;
} else {
$skipped[] = $settingKey;
}
}
// Persist password separately (stored encrypted)
if ($password !== null) {
CompanySettings::set('s_email_notify_password', $password, $companyId);
$updated['s_email_notify_password'] = '********';
} else {
$skipped[] = 's_email_notify_password';
}
// Nothing was provided at all
if (empty($updated)) {
$this->output->writeln('No options supplied — nothing was changed.');
$this->output->writeln('Usage example:');
$this->output->writeln(" php artisan smtp:set --company=1 --host=smtp.example.com --port=587 --username=user@example.com --password='P@\$\$w#rd!' --encryption=tls --from-address=no-reply@example.com --from-name='My App'");
return 0;
}
// Output results
$rows = [];
foreach ($updated as $key => $value) {
$rows[] = [$key, $value, 'updated'];
}
foreach ($skipped as $key) {
$rows[] = [$key, '(unchanged)', 'skipped'];
}
$this->output->writeln('');
$this->output->writeln("SMTP settings saved for company ID {$companyId}:");
$this->table(['Setting', 'Value', 'Status'], $rows);
return 0;
}
}