views:

392

answers:

3
+1  Q: 

Swift Mailer Error

Hi, Im using the following code to send a message:

try
{   
    require_once "lib/Swift.php";
    require_once "lib/Swift/Connection/SMTP.php";
    $smtp =& new Swift_Connection_SMTP("mail.somedomain.net", 587);
    $smtp->setUsername("username");
    $smtp->setpassword("password");
    $swift =& new Swift($smtp);

    //Create the sender from the details we've been given
    $sender =& new Swift_Address($email, $name);
    $message =& new Swift_Message("message title");

    $message->attach(new Swift_Message_Part("Hello"));

    //Try sending the email
    $sent = $swift->send($message, "$myEmail", $sender);
    //Disconnect from SMTP, we're done
    $swift->disconnect();

    if($sent)
    {
        print 'sent';

    }
    else 
    {
        print 'not sent';
    }

}

catch (Exception $e) 
{
    echo"$e";
}

The issue is that it is working fine on my local server (which my xampp server) but not working when the file is uploaded on the real server.

It throwing this error:

'The SMTP connection failed to start [mail.somedomain.net:587]: fsockopen returned Error Number 110 and Error String 'Connection timed out''

Please what should I do to correct this error. Thanks for reading

+1  A: 

Make sure that the smtp server domain is valid. Trying pinging it to confirm a response. You may also try trace route to see if any of the switches are returning slow responses.

Brant
Thanks for answering, the domain is valid and it works when the script file is still on my local apache server. I also tried to ping the server domain using ping mail.somedomain.com, il works perfectly. Please i need some ideas. thanks for reading and answering.
Selom
@Selom - Who is your mail provider?
Brant
+1  A: 

Check your hostname.

"Connection timed out" could mean quite a few things, but it's most likely to be the wrong port or a firewall is blocking the port.

Update

Since your host has told you not to use SMTP then I guess you have no choice to be honest.

It should be straightforward changing your php class to use sendmail. If you get the latest version of swift mailer library and follow this example then you should be done in no time.

Are you only using this library to send simple emails like the above? Maybe the simpler alternative of PHP's mail function will suffice? See the example on that page.

Abs
Thank you for answering, the hostname is correct. Ive been using this script to send email for long time. What is weird is that when the script is on my local apache host and my pc connected to the internet, it works perfectly. but when the file is uploaded, it throws the error im talking about. Please give me some ideas. thanks
Selom
Hmm strange, Is it possible to contact your hosting support guys about Firewalls or ports blocked etc?
Abs
thanks, I just received a mail from the support guy and he is saying that i should not use SMTP if I running the script from the server. He suggest I change my script into sendmail. Swift mailer has been the one Ive been using for long time and it always works. Any suggestion please?
Selom
If the connection is blocked by your provider there's not much you can do about it (except maybe nagging). Take a look at http://swiftmailer.org/docs/sendmail-transport
VolkerK
Thanks for answering, I think the problem might be from the server configuration as the same script works fine on my local apache server when I use the same smtp server domain detail. I runned the phpinfo and the only thing i could notice was that the max_execution_time is set to 30 on the server but set to 60 on my local apache server. Any idea please
Selom
@Selom - I don't think increasing the max exec time will make a difference as your script would of reported a fatal error of max exec time reached but you have another problem. I have updated my question.
Abs
+1  A: 
$smtp =& new Swift_Connection_SMTP("mail.somedomain.net", 587);

is that '587' the port number to connect to? Any reason you're trying that instead of the normal port 25? Port 587 (submission) is normally used for local users to send mail. Once you're running this script on your remote web server, it's no longer "local", and is most likely firewalled off (or the mail server's not listening to that port on external interfaces).

Try switching to port 25 and see if that helps any.

update:

Connection refused is better than "connection timed out". It means at least that the initial data packet got somewhere and was actively refused. Timed out means things just got dropped silently somewhere en-route.

max_execution_time would only come into play if the php script itself went over the max time. If that was the case, you wouldn't be getting a swiftmailer error, because the script would have simply terminated.

Is your webserver running sendmail? Change the connection host to 'localhost' and see if that helps. If you just want to send an email, then that should work. The only reason you might want to connect to a remote SMTP server is for the From: headers to be correctly set and not be possibly flagged as SPAM on the receving end.

Marc B
thanks for answering, I tried the port 25 and I have this message:'The SMTP connection failed to start [mail.somedomain.net:25]:fsockopen returned Error Number 111 and Error String 'Connection refused'' I think the problem might be from the server configuration as the same script works fine on my local apache server when I use the same smtp server domain detail. I runned the phpinfo and the only thing i could notice was that the max_execution_time is set to 30 on the server but set to 60 on my local apache server. Any idea please
Selom