Solved

Mail Filtering

Posted on 2003-12-03
6
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Send email using HTML and PHP in separate file 5 40
Length of for loop to be dynamic 2 33
CURL sending XML without spaces PHP 5 33
PHP $_POST vs asp request 4 25
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

749 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