In case anybody is interested, here's a basic example with the code I've included on my .php contact form script which, by calling mail -f /var/mail/www-data -e, does what I want. Not exactly the kind of solution I was looking for, but the same results:
Basic contact form and mail script:
<?php
require_once 'send_notification_if_no_new_mails.php';
if (isset($_POST['subject'])&&isset($_POST['message'])){
send_notification_if_no_new_mails();
mail("www-data",$_POST['subject'],$_POST['message']);
}
?>
<!doctype html><html>
<head><title>contact form</title></head>
<body><form method='post'>Subject:<input name='subject' type='text' /><br />
<textarea name='message'>Type here your message.</textarea>
<input type='submit' value='send'/></form></body>
</html>
The function to check and send the notification if necessary:
<?php
function send_notification_if_no_new_mails(){
exec('mail -f /var/mail/www-data -e',$output,$return_var);
if ($return_var=='0') {
/* There's already new mail. Do not send notification. */
return 0;
}
/* There is no new mail but there is going to be now -> Send notification */
$email="myemail@gmail.com";
$subject="New message from your webapp";
$msg = "You have a new message from your webapp's contact form";
$msg .= PHP_EOL.PHP_EOL;
/* Common Headers */
$time = time();
$now = (int)(date('Y',$time).date('m',$time).date('j',$time));
$headers = 'From: SYNAPP mailer <noreply@mywebapp.com>'.PHP_EOL;
$headers .= 'Reply-To: noreply <noreply@mywebapp.com>'.PHP_EOL;
$headers .= 'Return-Path: noreply <noreply@mywebapp.com>'.PHP_EOL;
$headers .= "Message-ID:<".$now." admin@".$_SERVER['SERVER_NAME'].">".PHP_EOL;
$headers .= "X-Mailer: PHP v".phpversion().PHP_EOL;
$headers .= 'MIME-Version: 1.0'.PHP_EOL;
$headers .= "Content-Type: text/plain; charset=\"utf-8\"".PHP_EOL;
mail($email, $subject, $msg, $headers);
return 1;
}
?>