env('admin_email', 'noreply@'.$_SERVER['SERVER_NAME']), 'log_path' => '../logs/email.log', 'method' => 'smtp', // 'smtp' or 'log' 'smtp_host' => env('smtp_host', 'localhost'), 'smtp_port' => env('smtp_port', 25), 'smtp_username' => env('smtp_username', null), 'smtp_password' => env('smtp_password', null), 'smtp_encryption' => env('smtp_encryption', null) ], $options); // Always send to log during debug if (env('debug', false)) $config['method'] = 'log'; // Validate input if (empty($to) || empty($subject) || empty($message)) { error_log('Email sending failed: Missing required parameters'); return false; } // Prepare email headers $headers = [ 'From: ' . $config['from'], 'X-Mailer: PHP/' . phpversion() ]; // Choose sending method switch ($config['method']) { case 'log': // Log email details to file $logMessage = sprintf( "[%s] To: %s, Subject: %s, Message:\n\n %s\n\n\n\n", date('Y-m-d H:i:s'), $to, $subject, $message ); // Attempt to log to file if (file_put_contents($config['log_path'], $logMessage, FILE_APPEND) === false) { error_log('Failed to write to log file: ' . $config['log_path']); return false; } return true; case 'smtp': default: // Attempt to send via SMTP try { // Prepare SMTP connection $smtpConfig = [ 'host' => $config['smtp_host'], 'port' => $config['smtp_port'], 'username' => $config['smtp_username'], 'password' => $config['smtp_password'], 'encryption' => $config['smtp_encryption'] ]; // Send email using PHP's mail function (basic SMTP) $result = mail( $to, $subject, $message, implode("\r\n", $headers) ); if (!$result) { error_log('SMTP email sending failed'); return false; } return true; } catch (Exception $e) { error_log('Email sending error: ' . $e->getMessage()); return false; } } } // Example usage: // Send via SMTP // send_email('recipient@example.com', 'Test Subject', 'Email body text'); // Send via log // send_email('recipient@example.com', 'Test Subject', 'Email body text', ['method' => 'log']); // Customize SMTP settings // send_email('recipient@example.com', 'Test Subject', 'Email body text', [ // 'method' => 'smtp', // 'smtp_host' => 'smtp.yourserver.com', // 'smtp_port' => 587, // 'smtp_username' => 'your_username', // 'smtp_password' => 'your_password', // 'smtp_encryption' => 'tls' // ]);