Today I added a line to this logging function so that it would keep a running tab of everything being written to the log, so that I could also email that content to me. This is represented by $log4RobertEmail.
And here is what I use elsewhere in the same PHP code in various places:
$log_post = âReached line 083 (Start processing next email)â;
log_message($log_post, âSTART TIMEâ);
Everything is written to the actual log just fine, but when I email the contents of $log4RobertEmail, nothing is printed in the email. What am I doing wrong?
Php functions have local variable scope. $log4RobertEmail doesnât exist outside that function and is destroyed when each call to that function ends. You would be getting a php undefined variable error when trying to reference the variable in the email code.
You can make this âworkâ by making $log4RobertEmail a main program variable, then using the global keyword inside the function to break variable scope.
Please do not communicate things through global variables
What is the idea behind the email, will it send the entire log for each request to you via email?
Also, can you show us the code thatâs supposed to send the email?
The idea behind the email is to send me a log of events that the bounceChecker script runs through while processing incoming bounced emails, unsubscribe request emails, etc. So yes, I can monitor what is happening in the actual log file, but it would also be nice to have those details emailed to me for problematic emails the bounce processor was not able to successfully take care of.
Here is the code to send the email, very simple:
if ($notifyRobert) {
$log_post = "Reached line 670 (Robert will be notified by email)";
log_message($log_post, 'LINE LOG');
if ($emailSubject) {
// Use the subject created ealier in this code.
} else {
// Otherwise use this subject:
$emailSubject = "BounceCheck: Unable to determine action";
}
$justEmailID = preg_replace('~\D~', '', $subject);
$emailMessage .= "Subject:\n\n";
$emailMessage .= "$subject\n\n";
$emailMessage .= "EmailID if found in the subject line:\n\n";
$emailMessage .= "$justEmailID\n\n";
$emailMessage .= "BounceCheck log:\n\n";
$emailMessage .= "$log4RobertEmail\n\n";
$emailMessage .= "Body:\n\n";
$emailMessage .= "$body\n\n";
$emailMessage .= "Full email content processed by BoogieTools:\n\n";
$emailMessage .= "$messageContent\n";
$recipients = array('robert@domain.com');
$toAddresses = implode(',', $recipients);
$headers = 'From: support@domain.com' . "\r\n" .
'Reply-To: support@domain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($toAddresses,$emailSubject,$emailMessage,$headers);
}
I personally hate global variables. To me, the whole point of a function is that it is a piece of standalone code than can be called from anywhere, without relying on anything other than what is passed in. In the OPs case Iâd want to return the new log string from the function:
function log_message($message) {
// build up the message string as you already do, and write to the log
$msg = $date . " " . $time . " " . $message;
return $msg; // pass it back to the calling code
}
$LogToDave .= log_message("Add a log entry here");
// more code
// more code
$LogToDave .= log_message("Add another log entry here");