Solved

Mail Filtering

Posted on 2003-12-03
6
301 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
ID: 9869059
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
ID: 9869214
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
ID: 9873133
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:aolXFT
ID: 9877271
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
ID: 9881076
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
ID: 9883271
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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 dynamically set the form action using jQuery.

896 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

15 Experts available now in Live!

Get 1:1 Help Now