Benutzer-Werkzeuge

Action disabled: source

autoresponder for postfix

With dovecot-sieve there is a plugin called vacation which creates autoreplies (out of office messages). „Unfortunately“ vacation is written in a manner that allows at most one message a day for one sender address. If you want to use vacation for an info address which responds to every request, then you would have to dive into sourcecode of vacation.

But there is another, much easier way to implement this than hacking the source. Postfix (SMTP Server of Mailstation) offers a quite easy way to achieve this. Just some changes in two config files. That's it ;-)

Configuration

Location of postfix config files

The default location for postfix config files on a diskstation is /usr/syno/mailstation/etc/ If you put your config files in this location then you should perform regular backups of these files. They could be overwritten when upgrading the firmware!!
You cannot just put the files into another location as postfix only accepts config files from its own default directory.
So if you want to put your files elsewhere, you would have to amend the config of postfix to let it know the other location.
This can be done by setting alternate_config_directories in main.cf

#main.cf
alternate_config_directories = /opt/etc/postfix

virtual

In the file virtual you can add a line like the following to activate an autoreply for a certain address. It assumes that the address postmaster@domain.tld will be forwarded to user@domain.tld and to the autoreply address

postmaster@domain.tld user@domain.tld,user@domain.tld@autoreply.domain.tld

autoreply.domain.tld MUST NOT be set in eighter $virtual_mailbox_domains or $mydestination in main.cf!!

After that „convert“ the file with postmap command for postfix' satisfaction

postmap /path/to/virtual

transport

In the file transport the next hop for an email based on the receipient's domain will e defined. So we create a line for the domain and „connect“ it to a postfix handler. This file as well needs to be converted with postmap

autoreply.domain.tld autoreply:

So if a mail arrives for autoreply.domain.tld it will be handed over to a postfix process called autoreply. You can choose any name you like (just be ceareful with white-spaces and special chars). Postfix will look for this handler in the following file

master.cf

In this file the services of postfix will be defined. Here we need to create a handler which could look as follows

autoreply	unix	-	n	n	-	-	pipe
 flags= user=nobody argv=/opt/etc/postfix/autoreply ${sender} ${mailbox}

Important: In the first line TABS must be used for whitespace. Whereas the second line (parameter line) MUST start with a single space and only use spaces for seperate arguments

So if autoreply is called postfix will „pipe“ the content of the mail via stdin to the given script. Furthermore the sender address and the mailbox name will be given to the script.

main.cf

Now you „just“ need to activate virtual and transport in the main.cf file

transport_maps = hash:/path/to/transport
virtual_alias_maps = hash:/path/to/virtual

After that a

postfix stop
postfix start

autoresponder script

Here follows the script which will be called as autoreply handler. It's just a small shellscript which expects the entiere content of the mail (all headers included) on stdin. Furthermore the script want to know the sender (which will be the receiver of the autoreply) and the original receiver (which will be the sender of autoreply)

The script performs some basic checks on the mails. For example if the mail comes from a mailing list or from a bulk mailer (like newsletter). In such cases no autoreply will be generated (would be a mess otherwise). With the conditions of those checkings I based on the conditions used in dovecot-sieve vacation:

  • Auto-Submitted: header exists with any value except „no“
  • Precedence: header exists with value „junk“, „bulk“ or „list“
  • The envelope sender
    • begins with „MAILER-DAEMON“ (case-insensitive)
    • begins with „LISTSERV“ (case-insensitive)
    • begins with „majordomo“ (case-insensitive)
    • begins with „owner-“ (case-sensitive)
    • contains the string „-request“ anywhere within it (case-sensitive)
  • The envelope sender and envelope recipient are the same
  • The envelope recipient is not found in the message To:, Cc: or Bcc: fields.

Code

The script contains of the shellscript autoreply and two php files. The purpose of the php files is to trim the mailstring (trim.php) and to split mails into header and body sections (split.php). Furthermore two files are expected which contain the content for the autoreply mails sent. One file (autoreply.txt) contains the text content of the mail and the other (autoreply.html) expects the html content. These two files should be placed in the same directory as the php files and the autoreply script.

These two template files are „parsed“ by eval within autoreply, so variable substitution is performed. For example is possible to set variables in autoreply script itself and use those in the template files. So one could set

name='Me Myself'
company='Yourself Corp Ltd'
address='Some Road 11'
town='1234 Capetown'

in autoreply (important outside the function). In the template files the vars can be accessed as usual

Content-Type: text/html
Content-Transfer-Encoding: 7bit

<p>
Thanks a lot for your appreciated request<br />
We will get back to you asap
</p>
<p>
Best Regards<br /><br />
<strong>$name</strong> - $company<br />
$address<br />
<u>$town</u>
</p>

autoreply

#!/bin/bash
 
######################################################################
#                                                                    #
# autoreply script for integration into postfix server process       #
#                                                                    #
# Author:     tobi <tobster@brain-force.ch>                          #
# Version:    0.2 (bug fix)                                          #
# Date:       05/09/11                                               #
# License:    GPL Version 3 (contact author prior to commercial use) #
# Depends:    bash (get it via ipkg)                                 #
#             egrep (should be in ipkg coreutils or similar)         #
#                                                                    #
# Important:  You probably need to adjust chmod on the log file.     #
#             This script SHOULD NEVER run as root and therefore the #
#             logfile needs to be created manually by root. The user #
#             running autoreply need write access on that logfile.   #
#                                                                    #
#             Check all paths in the script as the default paths are #
#             for a debian. Change them to your enviroment!          #
#                                                                    #
#             create autoreply.txt and autoreply.html manually.      #
#             these files expect the content as valid (ex correct    #
#             mime headers and transfer-encodings) mime parts        #
#                                                                    #
# History:    * 04/08/10 initial version 0.1 as beta                 #
#             * 06/08/10 minor bug fixes                             #
#             * 10/08/10 support for template files added            #
#             * 11/08/10 version 0.2 as stable                       #
#             * 05/09/11 minor changes. trim.php no longer needed    #
#             * 05/09/11 bug in rfc attachment fixed                 #
#                                                                    #
# ToDo:       * support for txt and html templates (added in 0.2)    #
#             * original message as rfc822 compatible attachment     #
#                                                                    #
# Summary:    A small autoreply script that sends messages on every  #
#             mail received ex if you use a info address and want to #
#             confirm every message that is sent there.              #
#             Some tests are performed before sending the mail:      #
#                                                                    #
#              * is the mail coming from a mailing list?             #
#              * are there typical headers for bulk messages?        #
#              * can the rcpt address been found in msg headers?     #
#                                                                    #
# Usage:      echo msg | autreply sender reciever                    #
# * sender:   sender of the original msg. The one who will receive   #
#             the autoreply                                          #
# * receiver: the receiver of the original msg. Will be used as      #
#             sender of the autoreply message                        #
# * stdin:    on stdin the content of the original msg is expected   #
#                                                                    #
######################################################################
 
 
if test "$1" = "-h"; then
 echo "*************************************************************"
 echo "*                                                           *"
 echo "* Usage:    autoreply sender reciever                       *"
 echo "* sender:   the original sender of the mail                 *"
 echo "* reciever: the original reciever of the mail               *"
 echo "* stdin:    on stdin the content of the mail is expected    *"
 echo "*                                                           *"
 echo "*************************************************************"
 exit
fi
 
msg=""
log=/var/log/autoreply.log
err=''
touch $log
 
while read x1
do
 msg="$msg\n$x1"
done
 
sendReply ()
{
 mail='/usr/sbin/sendmail'
 str="From: $2 <$2>\n"
 str=$str"To: <"$1">\n"
 str=$str"Content-Type: multipart/mixed; boundary=\"grenze\"\n"
 str=$str"Subject: Ihre Anfrage an / Your request to "$2"\n\n"
 str=$str"--grenze\n"
 str=$str"Content-Type: multipart/alternative; boundary=\"alternative\"\n"
 eval "t=\"$(cat /etc/postfix/autoreply/autoreply.txt)\""
 str=$str"\n--alternative\n"
 str=$str$t
 str=$str"\n\n--alternative\n"
 eval "t=\"$(cat /etc/postfix/autoreply/autoreply.html)\""
 str=$str$t"\n\n--alternative--\n\n--grenze\n"
 str=$str"Content-Type: message/rfc822\nContent-Disposition: attachment;filename=message.eml;\nContent-Transfer-Encoding: 7bit\n\n$msg\n\n--grenze--"
 echo -e "$str" | $mail -f "$2" -t "$1"
 exit
}
 
err=$(echo -e $msg | grep -i ^Auto-Submitted: | grep -iv "no" )
t=$?
if test $t -eq 0 ; then
 echo -e $(date +"%b %_d %T")"\tMessage has Auto-Submitted Headers with a value other than \"no\"\n$1\t$2" >> $log
 exit
fi
 
err=$(echo -e $msg | grep -i ^Precedence: | grep -i "junk")
t=$?
if test $t -eq 0 ; then
 echo -e $(date +"%b %_d %T")"\tMessages has Precedence \"junk\" header\n$1\t$2" >> $log
 exit
fi
 
err=$(echo -e $msg | grep -i ^Precedence: | grep -i "bulk")
t=$?
if test $t -eq 0 ; then
 echo -e $(date +"%b %_d %T")"\tMessages has Precedence header \"bulk\"\n$1\t$2" >> $log
 exit
fi
 
err=$(echo -e $msg | grep -i ^Precedence: | grep -i "list")
t=$?
if test $t -eq 0 ; then
 echo -e $(date +"%b %_d %T")"\tMessage has Precedence header with \"list\"\n$1\t$2" >> $log
 exit
fi
 
err=$(echo $1 | egrep -i mailer-daemon\|listserv\|majordomo\|owner-\|\-request)
t=$?
if test $t -eq 0 ; then
 echo -e $(date +"%b %_d %T")"\tMessage seems to be from a mailing list\n$1\t$2" >> $log
 exit
fi
 
if test "$1" = "$2" ; then
 echo -e $(date +"%b %_d %T")"\tFehler: Sender und Empfaenger sind gleich\n$1\t$2" >> $log
 exit
fi
 
err=$(echo -e $msg | grep -i ^To: | grep "$2")
t=$?
if test $t -eq 0; then
 sendReply $1 $2 "$msg"
fi
 
err=$(echo -e $msg | grep -i ^Cc: | grep "$2")
t=$?
if test $t -eq 0; then
 sendReply $1 $2 "$msg"
fi
 
err=$(echo -e $msg | grep -i ^Bcc: | grep "$2")
t=$?
if test $t -eq 0; then
 sendReply $1 $2 "$msg"
fi
echo -e $(date +"%b %_d %T")"\trcpt not found in headers (To, Cc or Bcc)\n$1\t$2" >> $log

autoreply.txt

Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Besten Dank fuer Ihre Anfrage
 Ihre Anfrage wird so schnell wie moeglich beantwortet

Thanks for your request,
 it will be answered asap

Gruss / Regards

$2

Anbei der Inhalt ihrer Anfrage / Following the content of your request

autoreply.html

Content-Type: text/html
Content-Transfer-Encoding: 7bit
 
<p>Besten Dank f&uuml;r Ihre Anfrage, <br />
&nbsp;Ihre Anfrage ist angekommen und wird schnellstm&ouml;glich beantwortet</p>
 
<p>Thanks for your request,<br />\
&nbsp;Your request has been received and will be answered asap</p>
 
<p>Gruss / Regards<br /><br />
<a href=\"mailto:"$2"\">"$2"</a><br /><br />
Anbei der Inhalt ihrer Anfrage / Following the content of your request
</p>
Melden Sie sich an, um einen Kommentar zu erstellen.

Seiten-Werkzeuge