Solved

Mail Filtering

Posted on 2003-12-03
6
300 Views
Last Modified: 2012-06-27
Im setting up a very simple spam filter. When a mail comes to a user i will use a .forward file to send this to a php script.

My problem is how does my php script read this file and give me varibles for the to and from address.

Dont know if this is needed but just in case.
Linux, PHP 4.3.2, pop users accounts with imap enabled, sendmail 8.11

Thanks in advance.

0
Comment
Question by:tsPHP
  • 4
  • 2
6 Comments
 
LVL 6

Expert Comment

by:aolXFT
Comment Utility
Personally I'd recommend using procmail instead of a PHP Script, but if you do want to filter it with a PHP Script, you simply have to use regex( http://www.php.net/manual/en/ref.pcre.php ) to filter out unsocilicited mail.

How are you planning on doing this? How do you plan on knowing what is Spam, and what isn't?
0
 
LVL 6

Expert Comment

by:aolXFT
Comment Utility
Personally I'd recommend using procmail instead of a PHP Script, but if you do want to filter it with a PHP Script, you simply have to use regex( http://www.php.net/manual/en/ref.pcre.php ) to filter out unsocilicited mail.

How are you planning on doing this? How do you plan on knowing what is Spam, and what isn't?
0
 

Author Comment

by:tsPHP
Comment Utility
Ill will have a db of address i want to be blocked, i will check this against th to field to see if it is a spammer.

if it is a spammer i would like to delete the message be fore it reaches my mailbox.

Any ideas on how i go about this.

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:aolXFT
Comment Utility
I don't know anything about your enviornment, but this would work for mbox, with exim.
#############################
<?php

$stdin = fopen("php://stdin", "r");
$bad_email = "spammer@spam\.net";
$mbox = "/home/tsPHP/mbox";

$mail = "";
while(!feof($stdin)){
  $mail .= fread($stdin, 1024);
}

if( preg_match("#^Envelope-to: $bad_email#m", $mail) ){
   die("Bad Mail");
}else{
   $mbox_handle = fopen($mbox, "a");
   fwrite($mbox_handle, $mail, strlen($mail));
}


This is a very simplistic approach. It only offers one email address, where as in reality you have loads. It also assumes the Envelope-to header, which is specific to exim, and it assumes that you have no line in your message body begining with 'From'(problem with mbox format) but it should get you on the right track.
0
 

Author Comment

by:tsPHP
Comment Utility
ok, this seems to make sense but my problem is i dont want to filter the a whole mailbox file but i want to filter each email as it comes in. I have read to do this i can use a .forward file and point it to my script but i havn't a clue on how to then read the input.

I hope ive made more sense this time.

Thanks alot for your time
0
 
LVL 6

Accepted Solution

by:
aolXFT earned 175 total points
Comment Utility
I don't think you understand my suggestion.

lets say you have the above php file I suggested, and are using mbox format, and called that PHP file /home/tsPHP/mail_filter.php, and made it executable, etc.

The reason I made reference to mbox, was that you need to store the message after you're finished filtering it. mbox stores messages by appending them to a mbox file. You can read more about mbox if this is what you are using.
You'd have this in your .forward file.

.forward:
==========================
|/home/tsPHP/mail_filter.php
-----------------------------------------------

I also made a mistake, in that it is email From Certain individuals that you want to filter out, and not email to certain individuals.

Change the regex in the above file to #^From.*$bad_email#i

or the command to

preg_match("#^From.*$bad_email#i", $mail);

Having all that said, I still suggest you use procmail instead.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now