How can I set up an auto-responder?

The source for this article is http://www.knowplace.org/pages/howtos/autoresponder_with_procmail_howto.php





An
autoresponder/infobot is great for automatically replying to
information requests (akin to a fax-back system). If you have
information you want to make available to others via a request email,
but don’t want the hassle of having to manually reply, read on. While
there are plenty of autoresponder packages available on the web,
this is probably the simplest implemenation (and it only takes a couple
of minutes). Chances are, procmail is already installed on your system.



Prerequisites:

  • procmail (http://www.procmail.org/procmail-3.15.1.tar.gz)
  • A working Mail Transport Agent such as postfix (faster, smaller, more secure) or sendmail (slow and exploit-ridden). Other MTA’s such as qmail or exim
    will probably work as well, but I’m not familiar with them. If you know
    how to use procmail with your MTA of choice, please send me an mail and
    I’ll include the directions below.

First, a
quick overview. You need to set up an account on your system to receive
incoming emails. Once the account is properly set up, you’ll add some
filtering rules (recipes) to procmail so it knows how to direct the
incoming email. You’ll also need to create the autoreply texts so that
the infobot you’re creating will have something sensible to say. The
last step is to hook procmail into your favorite MTA so that procmail
is invoked when the info request email is received. Since this is a
quick howto, you will have to look to other sources if you need help setting up procmail or postfix. Likely, your favorite distribution already has precompiled RPM’s for procmail and your favorite MTA.

  • Account setup: 
    • Execute useradd -d /home/infobot -s /bin/false infobot. This will create a user called infobot with a home directory of /home/infobot and a login shell of /bin/false.
    • Execute mkdir /home/infobot to create the home directory (we didn’t use a template in the step above because this user will never login).
    • Edit /etc/shells and add a line containing /bin/false if it doesn’t already exist (otherwise, you’ll get errors complaining that /bin/false isn’t a valid shell).
    • Don’t worry about the proper permissions yet. We’ll do it at the end.

  • Setting up your procmail recipe:
    • Execute touch /home/infobot/.procmailrc. This will create a file that will hold your procmail recipes.
    • Edit /home/infobot/.procmailrc and add:
      #logging - in case you want to troubleshoot (uncomment by deleting #)
      #LOGFILE=$HOME/procmail.log
      #VERBOSE=yes

      #shell - otherwise, nothing will happen when we call formail
      SHELL=/bin/bash

      #begin example_info section
      :0 h
      * !^FROM_DAEMON
      * !^FROM_MAILER
      * !^X-Loop: infobot_reply
      * ^Subject:.*example_info.*
      | (formail -rt -A"Precedence: junk (autoreply)"
      -A"X-Loop: infobot_reply" ;
      cat $HOME/example_info.txt) | $SENDMAIL -t
      #end example_info section

      #begin error handling section - NOTE: this section always goes at the end
      :0 h
      * !^FROM_DAEMON
      * !^FROM_MAILER
      * !^X-Loop: infobot_reply
      * ^Subject:.*
      | (formail -rt -A"Precedence: junk (autoreply)"
      -A"X-Loop: infobot_reply" ;
      cat $HOME/error_notice.txt) | $SENDMAIL -t
      #end error handling section

      #begin catch_bucket
      :0
      /dev/null
      #end catch_bucket

    • procmail comments:
      • Change all incidences of example_info to the desired info-request keyword. It will be the requrest keyword that the sender will have to include in the subject line (non-case-sensitive).
        If you want your request keyword(s) to contain characters such as
        periods, you will have to escape them with a backslash in your procmail
        recipe (e.g. use example.info if you want people to use the subject keywords example.info). Space characters do not need to be escaped.
      • A note about the * !^ entries:
        • * !^FROM_DAEMON and * !^FROM_MAILER prevents answering to daemon and mailer generated emails.
        • * !^X-Loop: infobot_reply
          line attempts to prevent mail looping. Some people suggest using unique
          strings such as an email address. However, my personal opinion is that
          we should all use infobot_reply. Do we really want infobots talking to other infobots?
      • /home/infobot/example_info.txt
        is the text file that will hold the actual info (i.e. the body of your
        autoreply). Change this to reflect whatever you actually name your
        auto-reply text file.
      • The reason why we’re matching on the subject line (* ^Subject) instead of the recipient line (* ^TO) is to enable us to have different replies based on the subject line.
      • Duplicate the entire example_info
        section if you need to add additional replies and modify as necessary.
        You will need one per info-request keyword. Just make sure it always
        goes above the error handling section.
      • The
        error handling section isn’t strictly necessary, but it’s good
        practice. It’s invoked when the subject line doesn’t match any of your
        predefined request keywords (whether it’s an unintentional typo or
        spam). Of course, you should say something in your auto-reply. You can
        see my example at
        http://www.knowplace.org/dl/infobot_error.txt or send a blank email to infobot@knowplace.org (subject optional).
         
      • The reason why the auto-reply email is set to Precedence: junk is to suppress bounce messages to (most sensible) MTA’s that implement this feature.
      • The
        above example does not retain a copy of the incoming email. If you want
        to keep a copy of all of the incoming requests, change :0 h to :0 hc and remove the catch_bucket section. Be careful of what you wish for. =)
      • There is a good short primer on procmail syntax at http://www.linuxgazette.com/issue14/procmail.html if wish to learn more about procmail recipes.


  • Getting your MTA to use procmail:.
    • If you’re using postfix, edit your /etc/postfix/main.cf file and uncomment the line (or add if missing) that says mailbox_command = /usr/bin/procmail and restart postfix (/usr/sbin/postfix reload).
    • If you’re using sendmail, execute echo “”|/usr/bin/procmail -f-“” > /home/infobot/.forward; chown infobot.users /home/infobot/.forward.

  • Setting file ownership and permissions:
    • Set the correct file ownership and permissions by executing chown -R infobot.users /home/infobot/;chmod 600 /home/infobot/*.
    • Make sure that the directory of /home/infobot has execution permission by executing chmod 700 /home/infobot/. Otherwise, your procmail will die a silent death.

  • Some last comments:
    • Go
      ahead and send the infobot some test emails to make sure it’s working
      properly. Make sure you also test the error handling by feeding it
      garbage in the subject line.
    • Since the infobot
      account that we’ve created is a local account, you’ll need to configure
      /etc/postfix/virtual properly if you’re using virtual domains. Consult
      the man pages by executing man 5 virtual or see
      http://www.postfix.org/virtual.5.html if you don’t already have this set up.
    • If
      you add additional autoreply texts later, remember to set the correct
      ownership and file permissions (and insert the appropriate procmail
      recipes into /home/infobot/.procmailrc).
    • If
      you’re emailing the infobot silly and you’re not getting any replies
      (and nothing is showing up in any of your logs), check the mail spool
      file for your infobot user (typically in /var/spool/mail).
      If the spool file is full of your test messages, either procmail isn’t
      being invoked or doesn’t have permission to read its filtering recipes.
    • As always, comments or suggestions are always welcome.




Your rating: None