Solved

Mail Filtering

Posted on 2003-12-03
6
304 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 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