Page 1 of 1

Why am I getting exception 'Swift_Connection_Exception'?

Posted: Wed Jul 11, 2007 6:54 pm
by Luke
I am using Swift-3.0.3-php5

I am trying to send an email--a pretty simple one... and for some reason, no matter what I do, I get this exception thrown when I send()

Code: Select all

exception 'Swift_Connection_Exception' with message 'Expected response code(s) [250] but got response []' in /vservers/chicorotaryc/phplib/Swift-3.0.3-php5/lib/Swift.php:252 Stack trace: #0 /vservers/chicorotaryc/phplib/Swift-3.0.3-php5/lib/Swift.php(305): Swift->assertCorrectResponse(Object(Swift_Events_ResponseEvent), 250) #1 /vservers/chicorotaryc/phplib/Swift-3.0.3-php5/lib/Swift.php(314): Swift->command('RSET', 250) #2 /vservers/chicorotaryc/phplib/Swift-3.0.3-php5/lib/Swift.php(395): Swift->reset() #3 /vservers/chicorotaryc/htdocs/members/library/Rotary/Controller/Action.php(288): Swift->send(Object(Swift_Message), Object(Swift_RecipientList), Object(Swift_Address)) #4 /vservers/chicorotaryc/htdocs/members/application/controllers/PasswordController.php(65): Rotary_Controller_Action->_sendMail(Array, 'Password Reset', Array, NULL) #5 /vservers/chicorotaryc/htdocs/members/application/controllers/PasswordController.php(44): PasswordController->sendEmail(Object(Table_User)) #6 /vservers/chicorotaryc/htdocs/members/application/controllers/PasswordController.php(7): PasswordController->processPasswordReset() #7 /vservers/chicorotaryc/phplib/ZendFramework-0.6.0/library/Zend/Controller/Dispatcher.php(436): PasswordController->indexAction() #8 /vservers/chicorotaryc/phplib/ZendFramework-0.6.0/library/Zend/Controller/Front.php(725): Zend_Controller_Dispatcher->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #9 /vservers/chicorotaryc/htdocs/members/index.php(149): Zend_Controller_Front->dispatch() #10 {main}
My code:

Code: Select all

require_once 'Swift.php';
        require_once 'Swift/Connection/SMTP.php';
        require_once 'Swift/Authenticator/LOGIN.php';

        if (!$config = new Zend_Config_Ini('./config/config.ini', 'mail'))
        {
            throw new Rotary_Exception('Cannot read from ./config/config.ini - please ensure it is there and has the proper [mail] section');
        }
        if (!is_a($this->_mailserver, 'Swift_Connection'))
        {
            $this->_mailserver = new Swift_Connection_SMTP($config->server);
        }
        
        $swift = new Swift($this->_mailserver);
        
        $this->_mailserver->setUsername($config->user);
        $this->_mailserver->setPassword($config->pass);
        $this->_mailserver->attachAuthenticator(new Swift_Authenticator_LOGIN());

        $recipients = new Swift_RecipientList();
        foreach ($recips as $name => $email)
        {
            $recipients->addTo($email, $name);
        }
        
        if (empty($from))
        {
            $from = new Swift_Address($this->_config->email_from_address, $this->_config->email_from_name);
        }
        else
        {
            $from = new Swift_Address($from['email'], $from['name']);
        }
        
        $message = new Swift_Message($subject . " - " . $this->_config->site_name);
        $message->attach(new Swift_Message_Part($emailBody['plain']));
        if (array_key_exists('html', $emailBody))
        {
            $message->attach(new Swift_Message_Part($emailBody['html'], "text/html"));
        }
        
        try
        {
	        if ($batch)
	        {
		        $numSent = $swift->batchSend(
		            $message,
		            $recipients,
		            $from
		        );
	        }
	        else
	        {
		        $numSent = $swift->send(
		            $message,
		            $recipients,
		            $from
		        );
        
	        }
        } catch (Swift_Connection_Exception $e) {
            
            echo $e;
            
        }

Posted: Thu Jul 12, 2007 1:29 am
by Chris Corbyn
Try the latest version of Swift first of all. Looking through all the commands on that stack trace it appears that Swift is trying to send the MAIL FROM command and being told "No, not now", then trying to issue a RSET command and being told "No, can't reset". May be authentication failed, but Swift should have picked up on that and thrown a different error explaining so.... again, try the latest version (3.2.6).

I'll get you to dump a log of upgrading doesn't help (but also double check the credentials).

Posted: Thu Jul 12, 2007 2:16 am
by anjanesh
Using Swift-3.2.6-php5 on a linux server mydomain.com having PHP 5.0.2, I got this error

Code: Select all

<br />
<b>Fatal error</b>:  Uncaught exception 'Swift_Connection_Exception' with message 'The SMTP connection failed to start [mail.my-other-domain.com:26]: fsockopen returned Error Number 111 and Error String 'Connection refused'' in /home/mywebsite/myfolder/lib/Swift/Connection/SMTP.php:293
Stack trace:
#0 /home/mywebsite/myfolder/lib/Swift/Connection/SMTP.php(293): Swift_Connection_SMTP::start()
#1 /home/mywebsite/myfolder/lib/Swift.php(225): Swift_Connection_SMTP->start()
#2 /home/mywebsite/myfolder/lib/Swift.php(102): Swift->connect()
#3 /home/mywebsite/myfolder/myscript.php(112): Swift->__construct(Object(Swift_Connection_SMTP))
#4 /home/mywebsite/myfolder/myscript.php(72): myfoo2()
#5 /home/mywebsite/myfolder/myscript.php(38): myfoo1()
#6 {main}
  thrown in <b>/home/mywebsite/myfolder/lib/Swift/Connection/SMTP.php</b> on line <b>293</b><br />
Btw, its port 26 for my-other-domain.com not 25.
Does this mean that mydomain.com's host wont allow an SMTP connection to my-other-domain.com ?

Posted: Thu Jul 12, 2007 3:25 am
by Chris Corbyn
~anjanesh, your host is blocking the connection. This could either be down to permissions or down to firewall rules. You'll need to ask your host why you can't use fsockopen() because that's where the problem is stemming from.

Posted: Thu Jul 12, 2007 3:55 am
by anjanesh
Thanks d11wtq - I guess the host webstrikesolutions is blocking it - they're very cautious on email-spam.
But get_file_contents and cURL work - dont they rely on fsockopen ?

Posted: Thu Jul 12, 2007 5:36 am
by Chris Corbyn
It's probably the port number that's blocked. Not 100%, I just know the connection is being refused ;)

Posted: Thu Jul 12, 2007 10:18 am
by Luke
Didn't help. What now?

Posted: Thu Jul 12, 2007 2:52 pm
by Chris Corbyn
The Ninja Space Goat wrote:Didn't help. What now?
:( Now I need a clearer picture of the problem...

Code: Select all

require_once 'Swift.php'; 
        require_once 'Swift/Connection/SMTP.php'; 
        require_once 'Swift/Authenticator/LOGIN.php'; 

        if (!$config = new Zend_Config_Ini('./config/config.ini', 'mail')) 
        { 
            throw new Rotary_Exception('Cannot read from ./config/config.ini - please ensure it is there and has the proper [mail] section'); 
        } 
        if (!is_a($this->_mailserver, 'Swift_Connection')) 
        { 
            $this->_mailserver = new Swift_Connection_SMTP($config->server); 
        } 
        
        $swift = new Swift($this->_mailserver, null, Swift::ENABLE_LOGGING); 
        
        $this->_mailserver->setUsername($config->user); 
        $this->_mailserver->setPassword($config->pass); 
        $this->_mailserver->attachAuthenticator(new Swift_Authenticator_LOGIN()); 

        $recipients = new Swift_RecipientList(); 
        foreach ($recips as $name => $email) 
        { 
            $recipients->addTo($email, $name); 
        } 
        
        if (empty($from)) 
        { 
            $from = new Swift_Address($this->_config->email_from_address, $this->_config->email_from_name); 
        } 
        else 
        { 
            $from = new Swift_Address($from['email'], $from['name']); 
        } 
        
        $message = new Swift_Message($subject . " - " . $this->_config->site_name); 
        $message->attach(new Swift_Message_Part($emailBody['plain'])); 
        if (array_key_exists('html', $emailBody)) 
        { 
            $message->attach(new Swift_Message_Part($emailBody['html'], "text/html")); 
        } 
        
        try 
        { 
                if ($batch) 
                { 
                        $numSent = $swift->batchSend( 
                            $message, 
                            $recipients, 
                            $from 
                        ); 
                } 
                else 
                { 
                        $numSent = $swift->send( 
                            $message, 
                            $recipients, 
                            $from 
                        ); 
        
                } 
        } catch (Swift_Connection_Exception $e) { 
            
            echo $e; 
            echo "Log shows: <pre>";
            $swift->log->dump();
            echo "</pre>";
            
        }
Just post the output here. Some of it contains < and > tags so will appear invisible when viewed in the browser. I'm ching that in 3.3.0 so $swift->log->dump(true) can be used to return the log dump as a string (which can be passed through htmlentities()) instead.

Posted: Thu Jul 12, 2007 3:46 pm
by Luke
hmm... it's giving me a different exception now... I don't know why or how I made it change, but here's what I'm getting now (with the added log dump)

Code: Select all

exception 'Swift_BadResponseException' with message 'Expected response code(s) [250] but got response []' in /vservers/chicorotaryc/phplib/Swift-3.2.6-php5/lib/Swift.php:258
Stack trace:
#0 /vservers/chicorotaryc/phplib/Swift-3.2.6-php5/lib/Swift.php(317): Swift->assertCorrectResponse(Object(Swift_Events_ResponseEvent), 250)
#1 /vservers/chicorotaryc/phplib/Swift-3.2.6-php5/lib/Swift.php(326): Swift->command('RSET', 250)
#2 /vservers/chicorotaryc/phplib/Swift-3.2.6-php5/lib/Swift.php(419): Swift->reset()
#3 /vservers/chicorotaryc/htdocs/members/library/Rotary/Controller/Action.php(287): Swift->send(Object(Swift_Message), Object(Swift_RecipientList), Object(Swift_Address))
#4 /vservers/chicorotaryc/htdocs/members/application/controllers/PasswordController.php(65): Rotary_Controller_Action->_sendMail(Array, 'Password Reset', Array, NULL)
#5 /vservers/chicorotaryc/htdocs/members/application/controllers/PasswordController.php(44): PasswordController->sendEmail(Object(Table_User))
#6 /vservers/chicorotaryc/htdocs/members/application/controllers/PasswordController.php(7): PasswordController->processPasswordReset()
#7 /vservers/chicorotaryc/phplib/ZendFramework-0.6.0/library/Zend/Controller/Dispatcher.php(436): PasswordController->indexAction()
#8 /vservers/chicorotaryc/phplib/ZendFramework-0.6.0/library/Zend/Controller/Front.php(725): Zend_Controller_Dispatcher->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#9 /vservers/chicorotaryc/htdocs/members/index.php(149): Zend_Controller_Front->dispatch()
#10 {main}Log shows: <pre></pre><br />
Doesn't look like it's outputting anything at all (from the log dump). This is cut & pasted from the source too, so it isn't due to < and > characters :(

Posted: Wed Sep 26, 2007 4:53 pm
by ReverendDexter
Okay, I got thrown this app, and I figured this out (for anyone else who gets this error). After a lot of beating my head against the wall, it ended up being something so frustratingly simple... well... take a look for yourself.

This bit of code here:

Code: Select all

$swift = new Swift($this->_mailserver);
        $this->_mailserver->setUsername($config->user);
        $this->_mailserver->setPassword($config->pass);
        $this->_mailserver->attachAuthenticator(new Swift_Authenticator_LOGIN());
Notice the order in which things happen... swift gets created, then the mailserver gets it's username and password.

A quick change to this:

Code: Select all

$this->_mailserver->setUsername($config->user);
        $this->_mailserver->setPassword($config->pass);
        $this->_mailserver->attachAuthenticator(new Swift_Authenticator_LOGIN());
        $swift = new Swift($this->_mailserver);
And it works.

Posted: Wed Sep 26, 2007 5:14 pm
by Luke
Thanks dex... :lol: so simple.