How can PHP be used to automate the process of accessing and sending log files from a web server to a specified email address on a regular basis?

To automate the process of accessing and sending log files from a web server to a specified email address on a regular basis, you can use PHP to create a script that reads the log files, composes an email with the log file contents as an attachment, and sends it to the specified email address using the mail function. This script can be scheduled to run periodically using a cron job.

<?php
$logFile = '/path/to/logfile.log';
$to = 'recipient@example.com';
$subject = 'Log File Report';
$message = 'Please find attached the log file report.';
$from = 'sender@example.com';
$replyTo = 'replyto@example.com';

// Read the log file contents
$logContents = file_get_contents($logFile);

// Compose the email
$boundary = md5(time());
$headers = "From: $from\r\n";
$headers .= "Reply-To: $replyTo\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $message . "\r\n\r\n";
$body .= "--$boundary\r\n";
$body .= "Content-Type: text/plain; name=\"" . basename($logFile) . "\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"" . basename($logFile) . "\"\r\n\r\n";
$body .= chunk_split(base64_encode($logContents)) . "\r\n";
$body .= "--$boundary--";

// Send the email
if (mail($to, $subject, $body, $headers)) {
    echo 'Log file report sent successfully.';
} else {
    echo 'Failed to send log file report.';
}
?>