The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Email filtering wisdom needed!

Discussion in 'E-mail Discussions' started by erick_paper, Jun 1, 2007.

  1. erick_paper

    erick_paper Well-Known Member

    Joined:
    Apr 19, 2005
    Messages:
    245
    Likes Received:
    0
    Trophy Points:
    16
    Hi, two question:

    1. How do I bounce email from a specific sender? Not "fail" (which bounces email from an entire email address) and not "discard" (which doesn't bounce at all)...I remember long ago there used to be a command "::bounce::". It doesn't work anymore? Do I need to muck around with the EXIM filter file for this?

    2. Secondly, if I set up a filter to send all incoming email to a PHP program, how does that work? How does PHP get the values from email, with "$_GET" variables?

    Many thanks!!
     
  2. Stefaans

    Stefaans Well-Known Member

    Joined:
    Mar 5, 2002
    Messages:
    451
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    Vancouver, Canada
    I stand to be corrected, but do not think the Exim Filter specification mentions a command to bounce emails. You could possibly achieve the effect with an autoresponse as follows:

    Edit your filter file at /etc/valiase/yourdomain.com
    Code:
    if $sender_address matches person@otherdomain.com then
    mail to $sender_address
    subject "Undeliverable email"
    expand file /home/username/bounce.msg
    file $home/vacation/messageendif
    
    In the above /home/username/bounce.msg is a file that contains the body of the bounce message. Be aware of the fact that the filter file will be overwritten when you use the cPanel Mail Filters command. I have not tested the above, so please use with caution ;)


    Code:
    2. Secondly, if I set up a filter to send all incoming email to a PHP program, how does that work? How does PHP get the values from email, with "$_GET" variables? 
    No, $_GET will not work. You have to decode the message and look at the MIME parts. Using the PEAR MAIL package (http://pear.php.net/packages.php?catpid=14&catname=Mail) is an easy way of doing this ;)
     
  3. erick_paper

    erick_paper Well-Known Member

    Joined:
    Apr 19, 2005
    Messages:
    245
    Likes Received:
    0
    Trophy Points:
    16
    Thanks Stefaans.

    About bouncing. That file doesn't exist. Is the path correct "/etc/valiase/mydomain.com"? Or should I create a new one to put that code in it?

    Thanks also for info about grabbing text from incoming mail. Is there any specific part of that MAIL pear package that I need to look at? I find the PEAR library in general quite a bloated one (which serves the purpose it is meant for, I guess) so I wouldn't want to install the whole thing.

    Thanks!
     
  4. Stefaans

    Stefaans Well-Known Member

    Joined:
    Mar 5, 2002
    Messages:
    451
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    Vancouver, Canada
    Typo alart! :eek: The file resides in /etc/valiases/ (I missed an "s"). If you do not have such a file, simply add a filter in cPanel. You will then have the file in /etc/valiases that you can overwrite as neeeded.

    Maybe it's bloated, maybe it's just very comprehensive. It's a matter of opinion ;) The PEAR packages are generally small smiles; it will not really hurt of you install a few packages that you do not really need.

    Mail_mimeDecode is the package you want to use. Below is an example; it is an abstract from an actual script we use. I do not suggest that it is the best way of doing things or that the script is complete. Please test and use at your own risk ;)

    PHP:
    // Mail_mimeDecode example by Stephen @ ANNO
    // Read the piped message
    $pipe fopen('php://stdin''r');
    while (!
    feof($pipe)) {
        
    $buffer fgets($pipe4096);
        
    $input.= $buffer;
    }
    fclose($pipe);

    // DeMIME with Pear packages
    $params['include_bodies'] = true;
    $params['decode_bodies'] = true;
    $params['decode_headers'] = true;
    $params['input'] = $input;
    $structure Mail_mimeDecode::decode($params);

    // Message headers
    $mail_from strip_address($structure->headers['from']);
    $mail_return strip_address($structure->headers['return-path']);
    $mail_to strip_address($structure->headers['to']);
    $mail_date $structure->headers['date'];
    $mail_delivered $structure->headers['delivery-date'];
    $mail_subject $structure->headers['subject'];

    // Check message format
    if ( ($structure->ctype_primary=='text') and ($structure->ctype_secondary=='plain') )
        
    $mail_body $structure->body;
    elseif (isset(
    $structure->parts)) { // HTML email; get plain text part
        
    foreach ($structure->parts as $part) {
            if ( (isset(
    $part->body)) and ($part->ctype_primary=="text") and ($part->ctype_secondary=="plain") )
            
    $mail_body $part->body;
        }  
    // end foreach
    }  // end elseif

    To pipe the email into this script, set up forwarder for the relevant email address to
    Code:
    |/usr/bin/php -q /home/username/pipescript.php >/dev/null 2>&1
    
     
  5. erick_paper

    erick_paper Well-Known Member

    Joined:
    Apr 19, 2005
    Messages:
    245
    Likes Received:
    0
    Trophy Points:
    16
    Thanks so much for this. But it isn't working. The code I have is this:

    Code:
    if $sender_address matches NAME.SURNAME@gmail.com then
    mail to $sender_address
    subject "Undeliverable email"
    expand file /home/MYDOMAIN/bounce.msg
    file [COLOR="Red"]$home/vacation/message[/COLOR]
    endif
    
    abuse@MYDOMAIN.com: NAME.LASTNAME@gmail.com
    access@MYDOMAIN.com: NAME.LASTNAME@gmail.com
    ....long list of these....
    
    I've masked the names and such for this public forum, but is this the basic way to enter the information into that valiases file? Or should I place the code elsewhere? :confused:

    Thanks so much also for the mimedecode class, will look for it and pester you with questions when I do :)
     
  6. Stefaans

    Stefaans Well-Known Member

    Joined:
    Mar 5, 2002
    Messages:
    451
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    Vancouver, Canada
    I have dabbled with the Exim filters, but do not think I am an expert. Be cautious taking my advice ;)

    It seems like I am guilty of yet another typo! The line "file $home/vacation/message" should not be there. Try:
    Code:
    if $sender_address matches [email]NAME.SURNAME@gmail.com[/email] then
      mail to $sender_address
      subject "Undeliverable email"
      expand file /home/MYDOMAIN/bounce.msg
    endif
    
    I notice now (did not see it before) that the Exim Filter Spec mentions a fail command. The following may possible be a much simpler solution:
    Code:
    if $sender_address matches [email]NAME.SURNAME@gmail.com[/email] then
      fail "Undeliverable emai"
    endif
    
    Something I should have mentioned before, is that errors in your filter file could mess up delivery of all email to your domain. So, perhaps a good idea to clear/delete /etc/vfilters/MYDOMAIN, and edit and test an "offline" filter file first.

    You can test your filter as follows:
    Code:
    exim -bf filterfile <email_message
    
    where filterfile is the Exim filter file you are editing, and email_message is a test email message stored somewhere on your server, e.g. /home/username/mail/MYDOMAIN/test/new/1233blah_blah

    And then, once you are happy with the results, copy the filter file to /etc/vfilters/MYDOMAIN.
     
Loading...

Share This Page