Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 82 additions & 6 deletions app/Web/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace App\Web;

use InvalidArgumentException;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception as PHPMailerException;

class Mail
{
Expand Down Expand Up @@ -60,7 +62,7 @@ public function to(string $mail)
if (!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('Mail not valid.');
}
$this->to = "<$mail>";
$this->to = $mail;
return $this;
}

Expand Down Expand Up @@ -138,15 +140,89 @@ public function send()
throw new InvalidArgumentException('Message cannot be null.');
}

$this->setHeaders();
if (self::$testing) {
return 1;
}

$this->headers .= $this->additionalHeaders;
$message = html_entity_decode($this->message);
$config = resolve('config');
$mailConfig = $config['mail'] ?? [];
$driver = $mailConfig['driver'] ?? 'mail';

if (self::$testing) {
if ($driver === 'smtp') {
return $this->sendViaSMTP($mailConfig);
}

return $this->sendViaMail();
}

/**
* Send email using SMTP via PHPMailer
*
* @param array $config
* @return int
*/
protected function sendViaSMTP(array $config)
{
$mail = new PHPMailer(true);

try {
// Server settings
$mail->isSMTP();
$mail->Host = $config['host'] ?? '';
$mail->Port = $config['port'] ?? 587;

// Authentication
if (!empty($config['username'])) {
$mail->SMTPAuth = true;
$mail->Username = $config['username'];
$mail->Password = $config['password'] ?? '';
}

// Encryption
$encryption = $config['encryption'] ?? 'tls';
if ($encryption === 'tls') {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
} elseif ($encryption === 'ssl') {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
} else {
$mail->SMTPSecure = '';
$mail->SMTPAutoTLS = false;
}

// Sender
$fromMail = !empty($config['from']) ? $config['from'] : $this->fromMail;
$fromName = !empty($config['from_name']) ? $config['from_name'] : ($this->fromName ?? '');
$mail->setFrom($fromMail, $fromName);

// Recipient
$mail->addAddress($this->to);

// Content
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Subject = $this->subject;
$mail->Body = '<html><body>'.html_entity_decode($this->message).'</body></html>';
$mail->AltBody = strip_tags(html_entity_decode($this->message));

$mail->send();
return 1;
} catch (PHPMailerException $e) {
error_log('Mail sending failed: '.$mail->ErrorInfo);
return 0;
}
}

/**
* Send email using PHP's mail() function (legacy method)
*
* @return int
*/
protected function sendViaMail()
{
$this->setHeaders();
$this->headers .= $this->additionalHeaders;
$message = html_entity_decode($this->message);

return (int) mail($this->to, $this->subject, "<html><body>$message</body></html>", $this->headers);
return (int) mail("<{$this->to}>", $this->subject, "<html><body>$message</body></html>", $this->headers);
}
}
10 changes: 10 additions & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@
'base_domain' => null,
'user_domain' => null,
],
'mail' => [
'driver' => 'mail',
'host' => '',
'port' => 587,
'encryption' => 'tls',
'username' => '',
'password' => '',
'from' => '',
'from_name' => '',
],
], require CONFIG_FILE);

$builder = new ContainerBuilder();
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"league/flysystem-cached-adapter": "^1.1",
"maennchen/zipstream-php": "^2.0",
"monolog/monolog": "^1.23",
"phpmailer/phpmailer": "^6.8",
"php-di/slim-bridge": "^3.0",
"sapphirecat/slim4-http-interop-adapter": "^1.0",
"slim/psr7": "^1.5",
Expand Down
83 changes: 82 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@
'driver' => 'local',
'path' => realpath(__DIR__).'/storage',
],
// SMTP configuration (optional - if not configured, PHP's mail() function will be used)
// 'mail' => [
// 'driver' => 'smtp', // 'smtp' or 'mail' (default: 'mail')
// 'host' => 'smtp.example.com',
// 'port' => 587,
// 'encryption' => 'tls', // 'tls', 'ssl', or '' for none
// 'username' => 'your-username',
// 'password' => 'your-password',
// 'from' => 'noreply@example.com',
// 'from_name' => 'XBackBone',
// ],
];