// Send approval email to participant
function sendApprovalEmail($registration) {
global $pdo;
// Get email settings from database (you'll need to create this table)
$stmt = $pdo->prepare("SELECT * FROM email_settings WHERE id = 1");
$stmt->execute();
$emailSettings = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$emailSettings) {
// Default email settings if not configured
$emailSettings = [
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => 587,
'smtp_username' => '',
'smtp_password' => '',
'smtp_encryption' => 'tls',
'from_email' => 'noreply@balabilalampung.com',
'from_name' => 'Bali Balap Lampung',
'reply_to' => 'admin@balabilalampung.com'
];
}
// Create the email content
$subject = "Registration Approved - " . $registration['race_name'];
// Email template
$message = "
Dear {$registration['participant_name']},
Congratulations! Your registration for {$registration['race_name']} has been approved.
Registration Details:
Registration Code: {$registration['registration_code']}
Race: {$registration['race_name']}
Category: {$registration['category_name']}
Event Date: " . formatDate($registration['event_date']) . "
Location: {$registration['location']}
Registration Fee: " . formatCurrency($registration['fee_paid']) . "
Your registration is now confirmed. Please keep this email for your records.
" . (!empty($registration['whatsapp_group_link']) ? "
Join our WhatsApp Group:
Click the button below to join our WhatsApp group for important updates and information about the race.
Join WhatsApp Group
" : "") . "
If you have any questions, please don't hesitate to contact us.
Best regards,
Bali Balap Lampung Team
";
// Create a temporary file for the email template (for debugging)
$tempFile = tempnam(sys_get_temp_dir(), 'approval_email_') . '.html';
file_put_contents($tempFile, $message);
// Initialize PHPMailer
require_once __DIR__ . '/../vendor/autoload.php';
$mail = new PHPMailer\PHPMailer(true);
try {
// Server settings
$mail->isSMTP();
$mail->Host = $emailSettings['smtp_host'];
$mail->SMTPAuth = true;
$mail->Username = $emailSettings['smtp_username'];
$mail->Password = $emailSettings['smtp_password'];
$mail->SMTPSecure = $emailSettings['smtp_encryption'];
$mail->Port = $emailSettings['smtp_port'];
// Recipients
$mail->setFrom($emailSettings['from_email'], $emailSettings['from_name']);
$mail->addAddress($registration['email'], $registration['participant_name']);
$mail->addReplyTo($emailSettings['reply_to'], $emailSettings['from_name']);
// Content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
// Send the email
$result = $mail->send();
// Clean up temporary file
unlink($tempFile);
// Log email sending attempt
if ($result) {
error_log("Approval email sent successfully to: " . $registration['email']);
return true;
} else {
error_log("Failed to send approval email to: " . $registration['email'] . " Error: " . $mail->ErrorInfo);
return false;
}
} catch (Exception $e) {
error_log("Email sending error: " . $e->getMessage());
// Clean up temporary file
if (file_exists($tempFile)) {
unlink($tempFile);
}
return false;
}
}