Goldfish, A PHP Autoresponder Script

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Thu, 2009-06-18 10:47. :: Email | PHP | Postfix

Goldfish, A PHP Autoresponder Script For The HowtoForge "Virtual Users And Domains With Postfix, Courier, MySQL And SquirrelMail " Setups

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Last edited 06/08/2009

Goldfish is a quite simple autoresponder script (written in PHP) for Postfix. It consists of only one PHP file which can be started through a cronjob. It works with our "Virtual Users And Domains With Postfix, Courier, MySQL And SquirrelMail" tutorials (it cannot be used for other setups, especially Postfix setups that don't use a MySQL database).

I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

I'm assuming that you already have a working "Virtual Users And Domains With Postfix, Courier, MySQL And SquirrelMail" setup, e.g.:

 

2 Installing And Using Goldfish

First we connect to our MySQL mail database and create an additional table called autoresponder:

mysql -u root -p

USE mail;

CREATE TABLE `autoresponder` (
`email` varchar(255) NOT NULL default '',
`descname` varchar(255) default NULL,
`from` date NOT NULL default '0000-00-00',
`to` date NOT NULL default '0000-00-00',
`message` text NOT NULL,
`enabled` tinyint(4) NOT NULL default '0',
`subject` varchar(255) NOT NULL default '',
PRIMARY KEY (`email`),
FULLTEXT KEY `message` (`message`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

quit;

Afterwards we download the latest goldfish release to /tmp and rename the goldfish PHP script to autoresponder.php:

cd /tmp
wget http://remofritzsche.ch/goldfish/download/current.tar.gz
tar xvfz current.tar.gz
cd goldfish-002p1/
mv goldfish-002-p1.php autoresponder.php

Then we open autoresponder.php and adjust the Configuration section. Make sure you fill in the correct database details (you can use the existing mail_admin MySQL user); in the $conf['q_mailbox_path'] line, make sure that you replace the table name view_users with users:

vi autoresponder.php

[...]
    ######################################
    # Configuration                      #
    ######################################
    /* General */
    $conf['cycle'] = 5 * 60;

    /* Logging */
    $conf['log_file_path'] = "/var/log/goldfish";
    $conf['write_log'] = true;

    /* Database information */
    $conf['mysql_host'] = "localhost";
    $conf['mysql_user'] = "mail_admin";
    $conf['mysql_password'] = "mail_admin_password";
    $conf['mysql_database'] = "mail";

    /* Database Queries */

    # This query has to return the path (`path`) of the corresponding
    # maildir-Mailbox with email-address %m
    $conf['q_mailbox_path'] = "SELECT CONCAT('/home/vmail/', SUBSTRING_INDEX(email,'@',-1), '/', SUBSTRING_INDEX(email,'@',1), '/') as `path` FROM users WHERE `email` = '%m'";

    # This query has to return the following fields from the autoresponder table: `from`, `to`, `email`, `message` where `enabled` = 2
    $conf['q_forwardings'] = "SELECT * FROM `autoresponder` WHERE `enabled` = 1";

    # This query has to disable every autoresponder entry which ended in the past
    $conf['q_disable_forwarding'] = "UPDATE `autoresponder` SET `enabled` = 0 WHERE `to` < CURDATE();";

    # This query has to activate every autoresponder entry which starts today
    $conf['q_enable_forwarding'] = "UPDATE `autoresponder` SET `enabled` = 1 WHERE `from` = CURDATE();";

    # This query has to return the message of an autoresponder entry identified by email %m
    $conf['q_messages'] = "SELECT `message` FROM `autoresponder` WHERE `email` = '%m'";

    # This query has to return the subject of the autoresponder entry identified by email %m
    $conf['q_subject'] = "SELECT `subject` FROM `autoresponder` WHERE `email` = '%m'";
[...]

Afterwards we move autoresponder.php to /usr/local/bin, make it owned by the user and group vmail, and make it executable:

mv autoresponder.php /usr/local/bin

chown vmail:vmail /usr/local/bin/autoresponder.php
chmod 755 /usr/local/bin/autoresponder.php

Now we create the log file /var/log/goldfish and make it owned by the user and group vmail:

touch /var/log/goldfish
chown vmail:vmail /var/log/goldfish

I want autoresponder.php to be executed every five minutes, therefore I create a cron job for it which will be run by the user vmail:

crontab -u vmail -e

*/5 * * * * /usr/local/bin/autoresponder.php

That's it! Now you can create autoresponders in the autoresponder table, e.g. with phpMyAdmin or on the command line, e.g. as follows:

mysql -u root -p

USE mail;

INSERT INTO `autoresponder` (`email`, `descname`, `from`, `to`, `message`, `enabled`, `subject`) VALUES('sales@example.com', 'sales@example.com Autoresponder', '2009-06-08', '2009-06-12', 'I will be out the week of June 8 with very limited access to email.\r\nI will respond as soon as possible.\r\nThanks!\r\nFalko', 1, 'Out of Office');

quit;

This creates an autoresponder for the email address sales@example.com which is active between June 8, 2009, and June 12, 20009. Because the autoresponder cron job is executed only every five minutes, the autoresponder message will not be sent immediately, but within five minutes after an email is sent to the sales@example.com address.

You should see something like this in the /var/log/goldfish log file whenever an autoresponder is sent from the sales@example.com address:

2009-06-08 07:00:01 Connection to database established successfully
2009-06-08 07:00:01 Database selected successfully
2009-06-08 07:00:01 Successfully updated database (disabled entries)
2009-06-08 07:00:01 Successfully updated database (enabled entries)
2009-06-08 07:00:01 Successfully fetched maildir directories
2009-06-08 07:00:01 Successfully fetched subject of sales@example.com
2009-06-08 07:00:01 Successfully fetched message of sales@example.com
2009-06-08 07:00:02 --------- End execution ------------

 

3 Links


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Tue, 2012-09-11 01:31.

Your tutorial is flawless.. but Goldfish is mediocre at best.. being a php developer.. this code is all sorts of wrong and also major problem.. it will send "out of office" every 5minutes to same person over and over again .. so goldfish .. BAD

 I will be writing a replacement for goldfish and i will post it here .. if it's ok

Submitted by SvenS (not registered) on Thu, 2011-02-03 15:48.

Autoreplay for Roundcube at http://eugenesklyar.com/roundcube-autoresponder-goldfish-autoreply-plugin

Submitted by Will (not registered) on Thu, 2009-09-17 22:57.

I recently installed this script and found that there is an error with values used when creating the "autoresponder" table. 

The field "descname" should NOT contain the email address. When the email address is included in this filed responses will not be sent. 

Submitted by Marc (not registered) on Sun, 2009-06-21 05:28.

Thanks for this tutorial, exactly what I was searching for.

There's a nice squirrelmail plug-in for goldfish at their website. May be add this to the tutorial?

http://www.remofritzsche.ch/goldfish/index.php?page=extensions-frontends