Page 1 of 1

Target Machine actively refusing

Posted: Mon May 05, 2008 12:58 pm
by seodevhead
Hey gang,

I have a script that I want to use SwiftMailer for sending out confirmation emails on various database events that happen.

For simplicity sake, I made a function called useSwiftMailer() that accepts various parameters and invokes the SwiftMailer class to send an email.

Code: Select all

function useSwiftMailer ($subject, $txtBody, $recipient, $mailingOn)
{
    $fullTxtBody = outputThis($txtBody);
    
    $smtp = new Swift_Connection_SMTP("mail.mydomain.com");
    $smtp->setUsername("event+mydomain.com");
    $smtp->setPassword("corbyncakes");
    $swift = new Swift($smtp);
    
    // Create a message
    $message = new Swift_Message($subject, $fullTxtBody);
    
    if ($mailingOn)
    {
        // Send Email:
        if ($swift->send($message, new Swift_Address($recipient, "Seodevhead"), new Swift_Address("event@mydomain.com", "Database Event Happened")))
        {
            // Do nothing.  Email sent successfully.
        } else
        {
            // SwiftMailer failed.  No email sent.
        }
    }       
}
The function seems to be working fine, and the emails are apparently being sent, however this page when accessed is causing many errors:

An error occurred in script '/home/user10/public_html/swiftlib/Swift/Connection/SMTP.php' on line 301:
fsockopen() [function.fsockopen]: unable to connect to mail.mydomain.com:25 (No connection could be made because the target machine actively refused it. )
Date/Time: 5-5-2008 13:42:38

Fatal error: Uncaught exception 'Swift_ConnectionException' with message 'The SMTP connection failed to start [mail.mydomain.com:25]: fsockopen returned Error Number 10061 and Error String 'No connection could be made because the target machine actively refused it. '' in /home/user10/public_html/swiftlib/Swift/Connection/SMTP.php:309 Stack trace: #0 /home/user10/public_html/swiftlib/Swift.php(216): Swift_Connection_SMTP->start() #1 /home/user10/public_html/swiftlib/Swift.php(101): Swift->connect() #2 /home/user10/public_html/event-handling/process-event.php(74): Swift->__construct(Object(Swift_Connection_SMTP)) #3 /home/user10/public_html/event-handling/process-event.php(355): useSwiftMailer('Database Event H...', 'An event has taken pl...', 'event@mydomai...', false, 1) #4 {main} thrown in /home/user10/public_html/swiftlib/Swift/Connection/SMTP.php on line 309

---------

I have replaced the actual domain name with mydomain.com for privacy sake. I use SwiftMailer on many other scripts on my website without any issues. I have a feeling these errors are being caused because this function I created is being called numerous times during this particular script's execution.

I don't think I have any firewalls that are blocking ports like 25, etc... so I'm not sure where these errors are coming from. But with my php skills... it's probably something I did wrong. :oops:

Any help would greatly be appreciated. Thanks!

Re: Target Machine actively refusing

Posted: Mon May 05, 2008 6:06 pm
by seodevhead
I figured out what it was... it was CSF/LFD firewall. Since the script is on a seperate server to the mailserver it specifies to use, CSF/LFD is blocking it.

And that's okay. But leaves me with a related question then...

Since I made this function to use SwiftMailer to send emails... I'm going to get this error (the error in the first post), everytime the script is run, regardless of whether or not the function is called, it's going to want to connect to that mail server specified in that SwiftMailer function I created.

How can I configure this so my script wont initiate a connection with the mail server unless that function is specifically called upon? Is there anyway to do this?

Thanks!

Re: Target Machine actively refusing

Posted: Mon May 05, 2008 6:12 pm
by Chris Corbyn
It won't connect until this line occurs:

Code: Select all

   $swift = new Swift($smtp);
So just including the function will do nothing.

Also, to return false instead of throwing an Exception:

Code: Select all

function useSwiftMailer ($subject, $txtBody, $recipient, $mailingOn)
{
  try
  {
    //All your code here
  }
  catch (Swift_ConnectionException $e)
  {
    return FALSE;
  }
}
:)