Difference between revisions of "Procmail"

From HE FAQ
Jump to: navigation, search
(Corrected for v2's, uses MAILDIR for compatibility)
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{1 prefix}}
 
{{1 prefix}}
This document demonstrates how to use procmail to automate mail handling.
+
This document demonstrates how to use procmail to automate mail handling, including forwarding, filtering into folders based on the subject line, handling multiple mailboxes, and running scripts.
General mail handling based on the subject line is discussed first to get you familiar with Procmail. Handling multiple mailboxes is discussed at the end of this document.
+
 
= Getting Started with Procmail =
 
= Getting Started with Procmail =
You need to create a file named ".procmailrc" (note the ".") in the home directory of your account.  You can do this either by connecting to the server via SSH and using a text editor such as nano or vi, or you can create a file named, for instance, "procmail.txt" on your computer, upload the file to your account with an SFTP client the same way you would upload a web page, and then rename the file.
+
You need to create a file named ".procmailrc" (note the ".") in the home directory of your account.  Create a file named "procmail.txt" on your computer, upload the file to your account with an SFTP client the same way you would [[Uploading & Downloading Files | upload a web page]], and then rename the file.  (If you are using a Windows computer, the filename ''must'' end in .txt before uploading.)  Alternatively, you can do this by connecting to the server via SSH and using a text editor such as nano or vi.
  
At the top of the file, include the following lines, making sure to change it to match your account's domain name:
+
At the top of the new file, include the following lines, making sure to change it to match your account's domain name:
  PATH=/usr/bin:/usr/local/bin               # Path Variables for the procmail session
+
  PATH=/usr/bin:/usr/local/bin:/bin          # Path Variables for the procmail session
  MAILDIR=/var/spool/mail/yourdomain.com     # Do not put 'www.' in front of your domain here
+
  MAILDIR=/var/mail/yourdomain.com           # Do not put 'www.' in front of your domain here
 
  LOGFILE=$HOME/procmail.log                  # Keeps a log of email routing
 
  LOGFILE=$HOME/procmail.log                  # Keeps a log of email routing
 
  VERBOSE=no                                  # Verbose Error Reporting --values (no|off yes|on)
 
  VERBOSE=no                                  # Verbose Error Reporting --values (no|off yes|on)
Line 13: Line 12:
 
  # SENDMAIL=/usr/bin/sendmail                # sendmail location (comment in procmail only)
 
  # SENDMAIL=/usr/bin/sendmail                # sendmail location (comment in procmail only)
  
The Procmail rules, sometimes called "recipes," will come after that.
+
The Procmail rules, sometimes called "recipes," should be added after that section.
 +
 
 +
On one of our older servers (version 1-2), use "MAILDIR=/var/spool/mail/yourdomain.com" instead.
  
 
= Email Forwarding =
 
= Email Forwarding =
  
Here is a simple email forwarding rule:
+
Here is a simple email forwarding rule which would forward email addressed to my_name@myHurricaneElectricHostedDomain.com to my_personal_address@someOtherDomain.net:
  
 
  :0
 
  :0
  * ^TO_somethinglocal@myHurricaneElectricHostedDomain.com
+
  * $DEFAULT ?? ^$MAILDIR/my_name(/|$)
 
  ! my_personal_address@someOtherDomain.net
 
  ! my_personal_address@someOtherDomain.net
  
'''Note:''' this forwards all mail, including spam. Please install [[SpamAssassin]] at the top of your .procmailrc file to limit the amount of spam your destination mailbox receives.
+
'''Note:''' this forwards all mail, including spam. Please install [[SpamAssassin]] before any forwarding rules to limit the amount of spam your destination mailbox receives.
 +
 
 +
To forward email to a different mailbox at the same domain, use the following recipe:
 +
 
 +
:0
 +
* $DEFAULT ?? ^$MAILDIR/my_name(/|$)
 +
$MAILDIR/my_other_name/
 +
 
  
 
= Sorting into Folders =
 
= Sorting into Folders =
Line 39: Line 47:
 
This is a little more difficult to understand. Make sure that 'SUPPORT' is in the Subject line and that it does not contain data from 'FROM_DAEMON' (listserv's and auto-mailers are suppose to put this in their headers.)
 
This is a little more difficult to understand. Make sure that 'SUPPORT' is in the Subject line and that it does not contain data from 'FROM_DAEMON' (listserv's and auto-mailers are suppose to put this in their headers.)
  
We also don't want to keep replying to other auto responders. This is remedied by putting an 'X' header on your response. (X headers are not seen by standard mail clients) formail reformats the headers of the outgoing message to include 'X-Loop: john_doe@bigbusiness.com' The text that is being displayed is in the file '/home/your_account/acknowledgement.txt' It's mailed to support@bigbusiness.com and saved to the 'Support' folder.
+
We also don't want to keep replying to other auto responders. This is remedied by putting an 'X' header on your response. (X headers are not seen by standard mail clients.) Formail reformats the headers of the outgoing message to include 'X-Loop: john_doe@bigbusiness.com'The response message that will be sent back is in the file '/home/your_account/acknowledgement.txt' (make sure you create this file first).
  
 
  :0
 
  :0
Line 47: Line 55:
 
   {
 
   {
 
     :0 h c
 
     :0 h c
       | (formail -t -r -A"X-Loop: john_doe@bigbusines.com " ; \
+
       | (formail -t -r -A"X-Loop: john_doe@bigbusiness.com " ; \
       cat /home/your_account/acknowlegement.txt ) | $SENDMAIL -t
+
       cat /home/your_account/acknowledgement.txt ) | $SENDMAIL -t
 
   }
 
   }
  
Line 76: Line 84:
 
  Registrations
 
  Registrations
  
Here's a modification of the autoresponder used above: mail sent support@yourvirtualhost.com triggers an autoresponse, is saved to a folder ("Support"), and a copy is forwarded (to support@bigbusiness.com).
+
Here's a modification of the autoresponder used above: mail sent to support@yourvirtualhost.com triggers an autoresponse, is saved to a folder ("Support"), and a copy is forwarded (to support@bigbusiness.com).
  
 
  :0
 
  :0
Line 84: Line 92:
 
   {
 
   {
 
     :0 h c
 
     :0 h c
       | (formail -t -r -A"X-Loop: john_doe@bigbusines.com " ; \
+
       | (formail -t -r -A"X-Loop: john_doe@bigbusiness.com " ; \
       cat /home/your_account/acknowlegement.txt ) | $SENDMAIL -t
+
       cat /home/your_account/acknowledgement.txt ) | $SENDMAIL -t
 
     :0 c
 
     :0 c
 
       !support@bigbusiness.com
 
       !support@bigbusiness.com

Latest revision as of 11:44, 18 April 2019

This information only pertains to Hurricane Electric's Shared Web Hosting package. There may be different information in our other categories.

This document demonstrates how to use procmail to automate mail handling, including forwarding, filtering into folders based on the subject line, handling multiple mailboxes, and running scripts.

Getting Started with Procmail

You need to create a file named ".procmailrc" (note the ".") in the home directory of your account. Create a file named "procmail.txt" on your computer, upload the file to your account with an SFTP client the same way you would upload a web page, and then rename the file. (If you are using a Windows computer, the filename must end in .txt before uploading.) Alternatively, you can do this by connecting to the server via SSH and using a text editor such as nano or vi.

At the top of the new file, include the following lines, making sure to change it to match your account's domain name:

PATH=/usr/bin:/usr/local/bin:/bin           # Path Variables for the procmail session
MAILDIR=/var/mail/yourdomain.com            # Do not put 'www.' in front of your domain here
LOGFILE=$HOME/procmail.log                  # Keeps a log of email routing
VERBOSE=no                                  # Verbose Error Reporting --values (no|off yes|on)
COMSAT=no                                   # set to no|off
# SENDMAIL=/usr/bin/sendmail                # sendmail location (comment in procmail only)

The Procmail rules, sometimes called "recipes," should be added after that section.

On one of our older servers (version 1-2), use "MAILDIR=/var/spool/mail/yourdomain.com" instead.

Email Forwarding

Here is a simple email forwarding rule which would forward email addressed to my_name@myHurricaneElectricHostedDomain.com to my_personal_address@someOtherDomain.net:

:0
* $DEFAULT ?? ^$MAILDIR/my_name(/|$)
! my_personal_address@someOtherDomain.net

Note: this forwards all mail, including spam. Please install SpamAssassin before any forwarding rules to limit the amount of spam your destination mailbox receives.

To forward email to a different mailbox at the same domain, use the following recipe:

:0
* $DEFAULT ?? ^$MAILDIR/my_name(/|$)
$MAILDIR/my_other_name/


Sorting into Folders

Adding the following text will match everything with 'INFO REQUEST' in the Subject line and save it to the folder 'Marketing'.

:0
* ^Subject:.*INFO REQUEST
Marketing

Custom Autoresponders

Adding the text below will match everything with 'SUPPORT' in the Subject line and return an acknowledgement to the user who sent the support request.

This is a little more difficult to understand. Make sure that 'SUPPORT' is in the Subject line and that it does not contain data from 'FROM_DAEMON' (listserv's and auto-mailers are suppose to put this in their headers.)

We also don't want to keep replying to other auto responders. This is remedied by putting an 'X' header on your response. (X headers are not seen by standard mail clients.) Formail reformats the headers of the outgoing message to include 'X-Loop: john_doe@bigbusiness.com'. The response message that will be sent back is in the file '/home/your_account/acknowledgement.txt' (make sure you create this file first).

:0
* ^Subject:.*SUPPORT
* !^FROM_DAEMON
* !^X-Loop: john_doe@bigbusiness.com
  {
   :0 h c
     | (formail -t -r -A"X-Loop: john_doe@bigbusiness.com " ; \
     cat /home/your_account/acknowledgement.txt ) | $SENDMAIL -t
  }

Examples of Combining Rules

Adding the following text will match everything with 'ORDER' in the subject line and save it to the folder Sales' and then send it to joe@bigbusiness.com.

:0
* ^Subject:.*ORDER
{  
  :0 c
  ! joe@bigbusiness.com
  :0
  Sales
}

To separate mail by sender (From line) use the following text, which will cause any mail from joe@bigbusiness.com to be stored in the folder 'BigBoss'.

:0
* ^From:.*joe@bigbusiness.com
BigBoss

The following text will cause any mail sent to register@yourvirtualhost.com to be stored in the folder 'Registrations'. This can be handy if the mail being sent to register@yourvirtualhost.com is actually being forwarded to another address, because the email's entire history is preserved, and Procmail will know who the original recipient was supposed to be.

:0
* ^TO_register@yourvirtualhost.com
Registrations

Here's a modification of the autoresponder used above: mail sent to support@yourvirtualhost.com triggers an autoresponse, is saved to a folder ("Support"), and a copy is forwarded (to support@bigbusiness.com).

:0
* ^TO_support@yourvirtualhost.com
* !^FROM_DAEMON
* !^X-Loop: john_doe@bigbusiness.com
  {
   :0 h c
     | (formail -t -r -A"X-Loop: john_doe@bigbusiness.com " ; \
     cat /home/your_account/acknowledgement.txt ) | $SENDMAIL -t
   :0 c
     !support@bigbusiness.com
   :0
     Support
  }