Goldfish Autoresponder not worked

Discussion in 'HOWTO-Related Questions' started by budim2010, Feb 7, 2011.

  1. budim2010

    budim2010 New Member

    Dear all,

    I'm having problem setup Goldfish autoresponder for Postfix.
    I'm following "Virtual users and domains ..." from Falko.
    then I want to add Goldfish1.1 autoresponder.

    But the autoresponder won't sendback any email to the sender.
    I'm using Fedora 10.

    Anybody can help.......?? :confused:

    Here's my maillog
    Here's my Goldfish log
     
  2. budim2010

    budim2010 New Member

    In case needed to see my autoresponder.php

    Code:
    #!/usr/bin/php -q
    <?
        /*
        	goldfish - the PHP auto responder for postfix
    	    Copyright (C) 2007-2008 by Remo Fritzsche
    	
    	    This program is free software: you can redistribute it and/or modify
    	    it under the terms of the GNU General Public License as published by
    	    the Free Software Foundation, either version 3 of the License, or
    	    any later version.
    	
    	    This program is distributed in the hope that it will be useful,
    	    but WITHOUT ANY WARRANTY; without even the implied warranty of
    	    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    	    GNU General Public License for more details.
    	
    	    You should have received a copy of the GNU General Public License
    	    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    	    
    	    (c) 2007-2009 Remo Fritzsche	(Main application programmer)
    		(c)      2009 Karl Herrick		(Bugfix)
    	    (c) 2007-2008 Manuel Aller		(Additional programming)
    	    
    	    Version 1.0-STABLE
    	*/
    	
        ini_set('display_errors', true);
        error_reporting( E_ALL );
        
        ######################################
        # Check PHP version                  #
        ######################################
        
    	if ( version_compare( PHP_VERSION, "5.0.0" ) == - 1 )
    	{
    		echo "Error, you are currently not running PHP 5 or later. Exiting.\n";
    		exit;
    	}
        
        ######################################
        # 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'] = "mypassword";
        $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'";
        
        ######################################
        # Logger class                       #
        ######################################
        
        class Logger
        {
    		var $logfile;
    		var $str;
    
    		function addLine($str)
    		{
    		    $str = date("Y-m-d h:i:s")." ".$str;
    		    $this->str .= "\n$str";
    		    echo $str."\n";
    		}
    		
    		function writeLog(&$conf)
    		{
    		    if (! $conf['write_log'] ) return;
    		    
    		    if (is_writable($conf['log_file_path']))
    		    {
    		    	$this->addLine("--------- End execution ------------");
    	   	    	if (!$handle = fopen($conf['log_file_path'], 'a'))
    	   	    	{
    	                echo "Cannot open file ({$conf['log_file_path']})";
    	                exit;
    	            }
    	
    	            if (fwrite($handle, $this->str) === FALSE)
    	            {
    	                echo "Cannot write to file)";
    	                exit;
    	            }
    	            else
    	            {
    					echo "Wrote log successfully.";
    		    	}
    	
    	            fclose($handle);
    	
    		  }
    		  else
    		  {
    			echo "Error: The log file is not writeable.\n";
    			echo "The log has not been written.\n";
    		  }
    		}
        }
        
        ######################################
        # Create log object                  #
        ######################################
        $log = new Logger();
        
        ######################################
        # function endup()                   #
        ######################################
        function endup(&$log, &$conf)
        {
    		$log->writeLog($conf);
    		exit;
        }
        
        ######################################
        # Database connection                #
        ######################################
        $link = @mysql_connect($conf['mysql_host'], $conf['mysql_user'], $conf['mysql_password']);
        if (!$link)
        {
    		$log->addLine("Could not connect to database. Abborting.");
    		endup($log, $conf);
        }
        else
        {
    		$log->addLine("Connection to database established successfully");
    		
    		if (!mysql_select_db($conf['mysql_database']))
    		{
    	    	$log->addLine("Could not select database ".$conf['mysql_database']);
    	    	endup($log, $conf);
    		}
    		else
    		{
    	    	$log->addLine("Database selected successfully");
    		}
        }
        
        ######################################
        # Update database entries            #
        ######################################
        $result = mysql_query($conf['q_disable_forwarding']);
        
        if (!$result)
        {
    		$log->addLine("Error in query ".$conf['q_disable_forwarding']."\n".mysql_error());
        }
        else
        {
    		$log->addLine("Successfully updated database (disabled entries)");
        }
        
        mysql_query($conf['q_enable_forwarding']);
        
        if (!$result)
        {
    		$log->addLine("Error in query ".$conf['q_enable_forwarding']."\n".mysql_error());
        }
        else
        {
    		$log->addLine("Successfully updated database (enabled entries)");
        }
        
        ######################################
        # Catching dirs of autoresponders mailboxes #
        ######################################
        
        // Corresponding email addresses
        $result = mysql_query($conf['q_forwardings']);
        
        if (!$result)
        {
        	$log->addLine("Error in query ".$conf['q_forwardings']."\n".mysql_error());
        	exit;
        }
       
        $num = mysql_num_rows($result);
        
        for ($i = 0; $i < $num; $i++)
        {
    		$emails[] = mysql_result($result, $i, "email");
    		$name[] = mysql_result($result, $i, "descname");
        }
        
        // Fetching directories
        for ($i = 0; $i < $num; $i++)
        {
    		$result = mysql_query(str_replace("%m", $emails[$i], $conf['q_mailbox_path']));
    		
    		if (!$result)
    		{
    	    	$log->addLine("Error in query ".$conf['q_mailbox_path']."\n".mysql_error()); exit; 
    		}
    		else
    		{
    	    	$log->addLine("Successfully fetched maildir directories"); 
    		}
    	
    		$paths[] = mysql_result($result, 0, 'path') . 'new/';
        }
        
        ######################################
        # Reading new mails                  #
        ######################################
        if ($num > 0)
        {
    	    $i = 0;
    	    
    	    foreach ($paths as $path)
    	    {
    	    	foreach(scandir($path) as $entry)
    	    	{
    		    	if ($entry != '.' && $entry != '..')
    		    	{
    					if (time() - filemtime($path . $entry) - $conf['cycle'] <= 0)
    					{
    			    		$mails[] = $path . $entry;
    			    		
    					    ###################################
    					    # Send response                   #
    					    ###################################
    			    
    			    		// Reading mail address
    			   			$mail = file($path.$entry);
    			    		
    					foreach ($mail as $line) 
    					{
            					$line = trim($line); 
            					
    						if (substr($line, 0, 12) == 'Return-Path:') 
            					{ 
                    					$returnpath = substr($line, strpos($line, '<') + 1, strpos($line, '>') - strpos($line, '<')-1)."\n"; 
            					} 
    					
    						if (substr($line, 0, 5) == 'From:' && strstr($line,"@")) 
    					        { 
    						        $address = substr($line, strpos($line, '<') + 1, strpos($line, '>') - strpos($line, '<')-1)."\n"; 
    						        break; 
    					        } 
    					        elseif(substr($line,0,5) == 'From:' && !strstr($line,"@") && !empty ($returnpath)) 
    					        { 
    					                $address = $returnpath; 
    					                break; 
    					        } 
    					} 
    		    
    			    		// Check: Is this mail allready answered
    			    
    			    		if (empty($address))
    			    		{
    							$log->addLine("Error, could not parse mail $path");
    			    		}
    			    		else
    			    		{
    							// Get data of current mail
    				   			$email = $emails[$i];
    				    
    				    		// Get subject
    				    		$result = mysql_query(str_replace("%m", $emails[$i], $conf['q_subject']));
    				    
    				    		if (!$result)
    				    		{
    								$log->addLine("Error in query ".$conf['q_subject']."\n".mysql_error()); exit;
    				    		}
    				    		else
    				    		{
    								$log->addLine("Successfully fetched subject of {$emails[$i]}");
    				    		}
    		
    				    		$subject = mysql_result($result, 0, 'subject');
    	
    				    		// Get Message
    				    		$result = mysql_query(str_replace("%m", $emails[$i], $conf['q_messages']));
    				    		
    				    		if (!$result)
    				    		{
    								$log->addLine("Error in query ".$conf['q_messages']."\n".mysql_error()); exit;
    				    		}
    				    		else
    				    		{
    								$log->addLine("Successfully fetched message of {$emails[$i]}");
    				    		}
    				    
    				    		$message = mysql_result($result, 0, 'message');
    	
    				    		$headers = "From: ".$name[$i]."<".$emails[$i].">";
    	
    				    		// Check if mail is allready an answer:
    				    		if (strstr($mail, $message))
    				    		{
    								$log->addLine("Mail from {$emails[$i]} allready answered");
    								break;
    				    		}
    				
    							// strip the line break from $address for checks
    							// fix by Karl Herrick, thank's a lot
    							if ( substr($address,0,strlen($address)-1) == $email )
    							{
    							        $log->addLine("Email address from autoresponder table is the same as the intended recipient! Not sending the mail!");
    							        break;
    							}
    				    
    							mail($address, $subject, $message, $headers);
    			   			}
    					}
    		    	}
    			}
    			
    			$i++;
    		}
    	}
    
    	$log->writeLog($conf);
    	echo "End execution.";
    ?>
    
    
     
  3. budim2010

    budim2010 New Member

    I've made a progress. I tried using older version of Goldfish 002-p1.
    And the autoresponder seems to start working, but still got error.

    When I'm sending from "[email protected]", the Autoresponder sends back to
    "[email protected]" ( missing one character 'm' ).

    I'm not good in php , is there anything I should change in autoresponder.php?? :confused: :confused:

    maillog
    Code:
    Feb  8 17:05:01 mail sendmail[32747]: p18A51nP032747: from=vmail, size=118, class=0, nrcpts=1, msgid=<[email protected]>, [email protected]
    Feb  8 17:05:01 mail postfix/smtpd[32726]: connect from localhost.localdomain[127.0.0.1]
    Feb  8 17:05:01 mail sendmail[32747]: STARTTLS=client, relay=[127.0.0.1], version=TLSv1/SSLv3, verify=FAIL, cipher=DHE-RSA-AES256-SHA, bits=256/256
    Feb  8 17:05:01 mail postfix/smtpd[32726]: EC27714504: client=localhost.localdomain[127.0.0.1], [email protected]
    Feb  8 17:05:02 mail postfix/cleanup[32733]: EC27714504: message-id=<[email protected]>
    Feb  8 17:05:02 mail sendmail[32747]: p18A51nP032747: to=rom: [email protected], ctladdr=vmail (5000/5000), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=30118, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (Ok: queued as EC27714504)
    Feb  8 17:05:02 mail postfix/qmgr[5666]: EC27714504: from=<[email protected]>, size=556, nrcpt=1 (queue active)
    Feb  8 17:05:02 mail postfix/smtpd[32726]: disconnect from localhost.localdomain[127.0.0.1]
    Feb  8 17:05:02 mail sendmail[32746]: p18A51jV032746: from=vmail, size=1040, class=0, nrcpts=1, msgid=<[email protected]>, [email protected]
    Feb  8 17:05:02 mail postfix/smtpd[32726]: connect from localhost.localdomain[127.0.0.1]
    Feb  8 17:05:02 mail postfix/smtp[32754]: EC27714504: to=<[email protected]>, relay=none, delay=0.16, delays=0.07/0.08/0/0, dsn=5.4.4, status=bounced (Host or domain name not found. Name service error for name=simasrb.co type=AAAA: Host not found)
    Feb  8 17:05:02 mail postfix/cleanup[32733]: 31E9E14507: message-id=<[email protected]>
    Feb  8 17:05:02 mail postfix/bounce[32755]: EC27714504: sender non-delivery notification: 31E9E14507
    Feb  8 17:05:02 mail postfix/qmgr[5666]: 31E9E14507: from=<>, size=2478, nrcpt=1 (queue active)
    Feb  8 17:05:02 mail postfix/qmgr[5666]: EC27714504: removed
    Feb  8 17:05:02 mail postfix/local[32757]: 31E9E14507: to=<[email protected]>, relay=local, delay=0.06, delays=0.01/0.03/0/0.02, dsn=2.0.0, status=sent (delivered to mailbox)
    Feb  8 17:05:02 mail postfix/qmgr[5666]: 31E9E14507: removed
    Feb  8 17:05:02 mail sendmail[32746]: STARTTLS=client, relay=[127.0.0.1], version=TLSv1/SSLv3, verify=FAIL, cipher=DHE-RSA-AES256-SHA, bits=256/256
    Feb  8 17:05:02 mail postfix/smtpd[32726]: 598AB14504: client=localhost.localdomain[127.0.0.1], [email protected]
    Feb  8 17:05:02 mail postfix/cleanup[32733]: 598AB14504: message-id=<[email protected]>
    Feb  8 17:05:02 mail sendmail[32746]: p18A51jV032746: to=vmail, ctladdr=vmail (5000/5000), delay=00:00:01, xdelay=00:00:00, mailer=relay, pri=31040, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (Ok: queued as 598AB14504)
    Feb  8 17:05:02 mail postfix/qmgr[5666]: 598AB14504: from=<[email protected]>, size=1545, nrcpt=1 (queue active)
    Feb  8 17:05:02 mail postfix/local[32757]: 598AB14504: to=<[email protected]>, relay=local, delay=0.07, delays=0.06/0/0/0.01, dsn=2.0.0, status=sent (delivered to mailbox)
    
    
    autoresponder.php
    Code:
    #!/usr/bin/php -q
    <?
        /*
        	goldfish - the PHP auto responder for postfix
    	    Copyright (C) 2007-2008 by Remo Fritzsche
    	
    	    This program is free software: you can redistribute it and/or modify
    	    it under the terms of the GNU General Public License as published by
    	    the Free Software Foundation, either version 3 of the License, or
    	    any later version.
    	
    	    This program is distributed in the hope that it will be useful,
    	    but WITHOUT ANY WARRANTY; without even the implied warranty of
    	    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    	    GNU General Public License for more details.
    	
    	    You should have received a copy of the GNU General Public License
    	    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    	    
    	    (c) 2007-2008 Remo Fritzsche	(Main application programmer)
    	    (c) 2007-2008 Manuel Aller		(Additional programming)
    	    
    	    Version 002, patch level 1
    	*/
    
        ini_set('display_errors', true);
        error_reporting( E_ALL );
        
        ######################################
        # Check PHP version                  #
        ######################################
        
    	if ( version_compare( PHP_VERSION, "5.0.0" ) == - 1 )
    	{
    		echo "Error, you are currently not running PHP 5 or later. Exiting.\n";
    		exit;
    	}
        
        ######################################
        # 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'] = "mypassword";
        $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'";
        
        ######################################
        # Logger class                       #
        ######################################
        
        class Logger
        {
    		var $logfile;
    		var $str;
    
    		function addLine($str)
    		{
    		    $str = date("Y-m-d h:i:s")." ".$str;
    		    $this->str .= "\n$str";
    		    echo $str."\n";
    		}
    		
    		function writeLog(&$conf)
    		{
    		    if (! $conf['write_log'] ) return;
    		    
    		    if (is_writable($conf['log_file_path']))
    		    {
    		    	$this->addLine("--------- End execution ------------");
    	   	    	if (!$handle = fopen($conf['log_file_path'], 'a'))
    	   	    	{
    	                echo "Cannot open file ({$conf['log_file_path']})";
    	                exit;
    	            }
    	
    	            if (fwrite($handle, $this->str) === FALSE)
    	            {
    	                echo "Cannot write to file)";
    	                exit;
    	            }
    	            else
    	            {
    					echo "Wrote log successfully.";
    		    	}
    	
    	            fclose($handle);
    	
    		  }
    		  else
    		  {
    			echo "Error: The log file is not writeable.\n";
    			echo "The log has not been written.\n";
    		  }
    		}
        }
        
        ######################################
        # Create log object                  #
        ######################################
        $log = new Logger();
        
        ######################################
        # function endup()                   #
        ######################################
        function endup(&$log, &$conf)
        {
    		$log->writeLog($conf);
    		exit;
        }
        
        ######################################
        # Database connection                #
        ######################################
        $link = @mysql_connect($conf['mysql_host'], $conf['mysql_user'], $conf['mysql_password']);
        if (!$link)
        {
    		$log->addLine("Could not connect to database. Abborting.");
    		endup($log, $conf);
        }
        else
        {
    		$log->addLine("Connection to database established successfully");
    		
    		if (!mysql_select_db($conf['mysql_database']))
    		{
    	    	$log->addLine("Could not select database ".$conf['mysql_database']);
    	    	endup($log, $conf);
    		}
    		else
    		{
    	    	$log->addLine("Database selected successfully");
    		}
        }
        
        ######################################
        # Update database entries            #
        ######################################
        $result = mysql_query($conf['q_disable_forwarding']);
        
        if (!$result)
        {
    		$log->addLine("Error in query ".$conf['q_disable_forwarding']."\n".mysql_error());
        }
        else
        {
    		$log->addLine("Successfully updated database (disabled entries)");
        }
        
        mysql_query($conf['q_enable_forwarding']);
        
        if (!$result)
        {
    		$log->addLine("Error in query ".$conf['q_enable_forwarding']."\n".mysql_error());
        }
        else
        {
    		$log->addLine("Successfully updated database (enabled entries)");
        }
        
        ######################################
        # Catching dirs of autoresponders mailboxes #
        ######################################
        
        // Corresponding email addresses
        $result = mysql_query($conf['q_forwardings']);
        
        if (!$result)
        {
        	$log->addLine("Error in query ".$conf['q_forwardings']."\n".mysql_error());
        	exit;
        }
       
        $num = mysql_num_rows($result);
        
        for ($i = 0; $i < $num; $i++)
        {
    		$emails[] = mysql_result($result, $i, "email");
    		$name[] = mysql_result($result, $i, "descname");
        }
        
        // Fetching directories
        for ($i = 0; $i < $num; $i++)
        {
    		$result = mysql_query(str_replace("%m", $emails[$i], $conf['q_mailbox_path']));
    		
    		if (!$result)
    		{
    	    	$log->addLine("Error in query ".$conf['q_mailbox_path']."\n".mysql_error()); exit; 
    		}
    		else
    		{
    	    	$log->addLine("Successfully fetched maildir directories"); 
    		}
    	
    		$paths[] = mysql_result($result, 0, 'path') . 'new/';
        }
        
        ######################################
        # Reading new mails                  #
        ######################################
        if ($num > 0)
        {
    	    $i = 0;
    	    
    	    foreach ($paths as $path)
    	    {
    	    	foreach(scandir($path) as $entry)
    	    	{
    		    	if ($entry != '.' && $entry != '..')
    		    	{
    					if (time() - filemtime($path . $entry) - $conf['cycle'] <= 0)
    					{
    			    		$mails[] = $path . $entry;
    			    		
    					    ###################################
    					    # Send response                   #
    					    ###################################
    			    
    			    		// Reading mail address
    			   			$mail = file($path.$entry);
    			    		
    			    		foreach ($mail as $line)
    			    		{
    							$line = trim($line);
    							
    							if (substr($line, 0, 5) == 'From:')
    							{
    				   				$address = substr($line, strpos($line, '<') + 1, strpos($line, '>') - strpos($line, '<')-1)."\n";			     
    				    			break;
    							}
    			    		}
    			    
    			    		// Check: Is this mail allready answered
    			    
    			    		if (empty($address))
    			    		{
    							$log->addLine("Error, could not parse mail $path");
    			    		}
    			    		else
    			    		{
    							// Get data of current mail
    				   			$email = $emails[$i];
    				    
    				    		// Get subject
    				    		$result = mysql_query(str_replace("%m", $emails[$i], $conf['q_subject']));
    				    
    				    		if (!$result)
    				    		{
    								$log->addLine("Error in query ".$conf['q_subject']."\n".mysql_error()); exit;
    				    		}
    				    		else
    				    		{
    								$log->addLine("Successfully fetched subject of {$emails[$i]}");
    				    		}
    		
    				    		$subject = mysql_result($result, 0, 'subject');
    	
    				    		// Get Message
    				    		$result = mysql_query(str_replace("%m", $emails[$i], $conf['q_messages']));
    				    		
    				    		if (!$result)
    				    		{
    								$log->addLine("Error in query ".$conf['q_messages']."\n".mysql_error()); exit;
    				    		}
    				    		else
    				    		{
    								$log->addLine("Successfully fetched message of {$emails[$i]}");
    				    		}
    				    
    				    		$message = mysql_result($result, 0, 'message');
    	
    				    		$headers = "From: ".$name[$i]."<".$emails[$i].">";
    	
    				    		// Check if mail is allready an answer:
    				    		if (strstr($mail, $message))
    				    		{
    								$log->addLine("Mail from {$emails[$i]} allready answered");
    								break;
    				    		}
    				    
    							mail($address, $subject, $message, $headers);
    			   			}
    					}
    		    	}
    			}
    			
    			$i++;
    		}
    	}
    
    	$log->writeLog($conf);
    	echo "End execution.";
    ?>
    
    
     

Share This Page