只有第一封未读电子邮件才能将新邮件通知发送到外部地址

我正在运行一个带有联系表单的networking服务器,该表单触发一个php脚本,将邮件发送到服务器上的本地帐户,并且当我收到新邮件时,我想在我的常规电子邮件帐户上收到通知。

我可以通过电子邮件发送通知,或直接发送电子邮件到我的帐户,但这太多了:我只是想每当收件箱从0个未读邮件变为1时,发送到我的电子邮件“你有新的邮件”通知。

Google正在将我埋在sendmail文档中,但仍然无法find任何相关信息。

有任何想法吗?

我使用.forward文件来实现这一点。

PROS:死简单。

缺点:1收到每封电子邮件“ping”电子邮件。 所以如果我在收到5封电子邮件之前收到5封电子邮件, 这可以通过使用诸如send_notification_if_no_new_mails()之类的脚本来解决。

.forward文件:

\username |"echo 'New email just arrived.' | mailx -s 'new message on the server' [email protected]" 

第一行是您的本地帐户,前面加一个反斜杠以防止循环。 这确保了本地交付。 第二行执行一个脚本。 在这种情况下,它直接调用mailx来发送ping电子邮件。 您可以改为运行一个类似于您的send_notification_if_no_new_mails()的脚本来限制发送的ping。

你可能想写一些类似的cron任务。

例如。 您可以用Python或PHP编写每分钟运行一次的内容,使用IMAPlogin到邮箱,​​检查等待的邮件大小是否已更改,如果是,则向您发送通知电子邮件。

让Sendmail在本地执行此操作将会遇到更多的麻烦。

顺便说一句,为什么你不把刚刚从联系表单发送的电子邮件转到本地帐户和真实帐户呢?

你需要写一个像biff一样的程序,比如xbiff或者xbiff2。 您应该logging以下状态:全读,未读邮件未发送,未读邮件发送。

所以现在你必须写一个脚本运行的脚本每隔30分钟,检查你的邮箱(通过POP3,IMAP,甚至直接),并要求新的邮件。 如果存在新邮件,则必须知道您是否发送了通知邮件。 如果你有新的邮件,但没有发送通知,则发送通知并将其logging在“标志”文件中。 如果您有新邮件并且存在该文件,请勿发送电子邮件。 如果没有电子邮件是新的,并且文件存在,请将其删除。

如果有人有兴趣,这里有一个基本的例子,我已经包含在我的.php联系表格脚本中的代码,通过调用mail -f /var/mail/www-data -e ,做我想要的。 不是我正在寻找的那种解决scheme,而是相同的结果:

基本联系表格和邮件脚本:

 <?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> 

必要时检查和发送通知的function:

 <?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="[email protected]"; $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 <[email protected]>'.PHP_EOL; $headers .= 'Reply-To: noreply <[email protected]>'.PHP_EOL; $headers .= 'Return-Path: noreply <[email protected]>'.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; } ?>