Add comprehensive email sending logging

Added detailed logging for SMTP email process:
- Log recipient, subject, service type
- Log SMTP connection details (host, port, user)
- Log connection status
- Log each SMTP command step
- Log success/failure with clear markers
- Log full exception stack traces

Now easy to troubleshoot email issues by checking logs for:
'===== EMAIL SEND START ====='
and
'===== EMAIL SEND END ====='

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
DJP 2025-11-18 11:51:39 -05:00
parent 5158db0534
commit c88cee98e6

View file

@ -122,12 +122,21 @@ class EmailService {
*/
private function sendViaSMTP($to, $subject, $text, $html = null) {
try {
error_log('===== EMAIL SEND START =====');
error_log('To: ' . $to);
error_log('Subject: ' . $subject);
error_log('Service: SMTP');
$from = $this->config['from'];
$host = $this->config['smtp_host'];
$port = $this->config['smtp_port'];
$username = $this->config['smtp_username'];
$password = $this->config['smtp_password'];
error_log('SMTP Host: ' . $host . ':' . $port);
error_log('SMTP User: ' . $username);
error_log('From: ' . $from);
// Build email message with multipart if HTML provided
$boundary = md5(time());
@ -143,10 +152,11 @@ class EmailService {
}
// Create SMTP connection
error_log('Connecting to SMTP server...');
$socket = fsockopen($host, $port, $errno, $errstr, 10);
if (!$socket) {
error_log('SMTP connection failed: ' . $errstr);
error_log('SMTP connection failed: ' . $errstr . ' (errno: ' . $errno . ')');
return [
'success' => false,
'error' => 'SMTP connection failed',
@ -154,6 +164,8 @@ class EmailService {
];
}
error_log('SMTP connection established');
// SMTP conversation
$this->smtpCommand($socket, null, 220); // Wait for greeting
$this->smtpCommand($socket, "EHLO " . $host, 250);
@ -185,12 +197,16 @@ class EmailService {
$message .= ".\r\n";
error_log('Sending email data...');
$this->smtpCommand($socket, $message, 250);
error_log('Sending QUIT command...');
$this->smtpCommand($socket, "QUIT", 221);
fclose($socket);
error_log('Email sent via SMTP to ' . $to);
error_log('✅ Email sent successfully via SMTP to ' . $to);
error_log('===== EMAIL SEND END (SUCCESS) =====');
return [
'success' => true,
@ -198,7 +214,9 @@ class EmailService {
];
} catch (Exception $e) {
error_log('SMTP exception: ' . $e->getMessage());
error_log('❌ SMTP exception: ' . $e->getMessage());
error_log('Stack trace: ' . $e->getTraceAsString());
error_log('===== EMAIL SEND END (FAILED) =====');
return [
'success' => false,