Solved

Mail Filtering

Posted on 2003-12-03
6
307 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

766 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