Connecting to remote SMTP mail server

  • wpas
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post August 31st, 2012, 8:09 pm

Hi All

I have the following script on one of my websites that I use to test for responses from the smtp mail server for the website domain:

Code: [ Select ]
<?php
$smtpServer = "mail.smtp.com";
$port = 25;
$timeout = 30;
//connect to the host and port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect)) {
  $output = "Failed to connect: $smtpResponse";
  echo $output;
  return $output;
}
else {
  $logArray['connection'] = "<p>Connected to: $smtpResponse";
  echo "<p />connection accepted<br>".$smtpResponse;
}
?>
  1. <?php
  2. $smtpServer = "mail.smtp.com";
  3. $port = 25;
  4. $timeout = 30;
  5. //connect to the host and port
  6. $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
  7. $smtpResponse = fgets($smtpConnect, 4096);
  8. if(empty($smtpConnect)) {
  9.   $output = "Failed to connect: $smtpResponse";
  10.   echo $output;
  11.   return $output;
  12. }
  13. else {
  14.   $logArray['connection'] = "<p>Connected to: $smtpResponse";
  15.   echo "<p />connection accepted<br>".$smtpResponse;
  16. }
  17. ?>


When I run it, I do get a connection and a response from the mail server.

Next, I put the same script on my other website and try to test the same smtp server as above(fictious name).
When I run it, I cannot connect to the server, which I guess would be remote now.

Could someone please tell me why I cannot connect?

Thanks
http://www.schembrionics.com
The Ultimate Solutions Center
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post August 31st, 2012, 8:09 pm

  • Zealous
  • Guru
  • Guru
  • User avatar
  • Joined: Apr 15, 2011
  • Posts: 1195
  • Loc: Sydney
  • Status: Offline

Post August 31st, 2012, 10:21 pm

The code looks fine and if you got it working in one place then it should work so going to ask stupid questions but lets run through the basic's first.

Does file need chmod permissions

if home server/ ports open.

Firewall on 2nd server allowing connection/ rules on 2nd server applied.

Also any notes in error logs?
  • wpas
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post September 1st, 2012, 11:09 am

Hi Zealous

I don't know if this makes any difference but both websites are on two different shared web hosting sites.

No files permissions are needed.
On each website both have chmod of 644

Checked port 25 on both websites and they were both open.

I have no idea about firewall on server allowing connections as it is on shared hosting.

I did look at error during connection attempt and it was:
110 Connection timed out

hope this helps
http://www.schembrionics.com
The Ultimate Solutions Center
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Joined: Apr 15, 2011
  • Posts: 1195
  • Loc: Sydney
  • Status: Offline

Post September 1st, 2012, 8:13 pm

600 : Only owner can read/write
644 : Only owner can write, others can read
666 : All uses can read/write.

^ notes

hhmmm researching now lol,

safe mode on? some functions don't work without it

reviewed php.ini

is there a lot of executions that it mite time out due to too many requests.
Code: [ Select ]
<?//header
set_time_limit(20*60);
?>
  1. <?//header
  2. set_time_limit(20*60);
  3. ?>

^ allows 20 min to solve :P
  • wpas
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post September 2nd, 2012, 1:02 am

I put my script on two other separate shared hosting websites.

If I use the script on each website to connect to the mail servers for the website, I can connect to both no problem.

If I try to connect from one website to the other website's mail server then I always get the 110 Connection timed out error.

I know port 25 is open on both websites or else I would not have been able to connect to the local mail servers.

There must be something about when you try to connect remotely, that seems to come into play. What it is, I do not know.

As you can see, the script is very simple and works on each website.
http://www.schembrionics.com
The Ultimate Solutions Center
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Joined: Apr 15, 2011
  • Posts: 1195
  • Loc: Sydney
  • Status: Offline

Post September 2nd, 2012, 2:05 am

o.k better understanding now, sorry about that mate.

o.k lets throw something out there, i have known some applications when connecting all on the same port there are conflicts over a network. I do understand there is 2 servers as well as the email system that does not often fail and settings has not changed in 10 years.

1 suggestion is running on different ports which i only see as a stupid solution. But you may need to complicate the script a little for remote play.

lets say you have to define each server and server connection, it may be requesting local information when you want remote.

Thinking outside the box but i would look into having a server side script that will manage the email responses and then a client side for connecting from the outside.

going to have a quick Google and see what snippets i can find.
  • wpas
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post September 2nd, 2012, 12:41 pm

I went to a couple of websites that verify email addresses by connecting to the SMTP servers:

http://verify-email.org/

http://www.email-unlimited.com/tools/verify-email.aspx

http://tools.email-checker.com/

http://my-addr.com/free-email-verification-tool/verify-email-address/reverse-email-lookup/verify_email.php/

At each website, I put in email addresses used on both my websites.
All of them were able to connect to the mail servers.

It would really be nice to know what they are doing to connect to the smtp servers
http://www.schembrionics.com
The Ultimate Solutions Center
  • wpas
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post September 2nd, 2012, 11:00 pm

I have what I hope is a simple question.

When I do get to the smtp server, I can get back 1, 2 or 3 220 responses dependent on the server.

Right now to get the response I use:

Code: [ Select ]
echo trim(fgets($con));


however, the above only gets the first response.
To get all 3 responses I need to get it 3 times.

Code: [ Select ]
echo trim(fgets($con))."<br />";
echo trim(fgets($con))."<br />";
echo trim(fgets($con))."<br />";
  1. echo trim(fgets($con))."<br />";
  2. echo trim(fgets($con))."<br />";
  3. echo trim(fgets($con))."<br />";


As I stated before, you may not always get 3 responses and so the script keeps running to get a response that is not there.

is there anyway I can add a some type of loop that waits for all the responses and then prints them all out.

I tried this:

Code: [ Select ]
if ($con)
  {
  while (($buffer = fgets($con, 4096)) !== false)
    {
    echo $buffer;
    }
  }
  1. if ($con)
  2.   {
  3.   while (($buffer = fgets($con, 4096)) !== false)
  4.     {
  5.     echo $buffer;
  6.     }
  7.   }


$con is the fsockopen() function.

This works but it puts everything on one line:

220-........220-........220-.....

I would like it to come out on separate lines:

220-.....
220-.....
220-....

Any way I can change my script or is there another way of doing it.

Thanks
http://www.schembrionics.com
The Ultimate Solutions Center
  • wpas
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post September 3rd, 2012, 1:32 pm

Hi Zealous

I was able to figure out my script by adding stream timeout and and HTML <br /> break as follows:

Code: [ Select ]
if ($con)
{
 while (($buffer = fgets($con,4096)) !== false)
    {
    //Set stream ouput time so doesn't take forever
    stream_set_timeout($con, 5);
    echo $buffer."<br />";
    }
 }
  1. if ($con)
  2. {
  3.  while (($buffer = fgets($con,4096)) !== false)
  4.     {
  5.     //Set stream ouput time so doesn't take forever
  6.     stream_set_timeout($con, 5);
  7.     echo $buffer."<br />";
  8.     }
  9.  }


Now it gives the stream a 5 second output and prints on separate lines.

Any luck on finding more help for my connection problem
http://www.schembrionics.com
The Ultimate Solutions Center
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Joined: Apr 15, 2011
  • Posts: 1195
  • Loc: Sydney
  • Status: Offline

Post September 4th, 2012, 6:33 pm

o.k so i am reading over 50 pages atm collecting notes lol, lets hope i find something useful.

http://kb.siteground.com/article/The_SM ... tempt.html

i had a look at this and from the read, they say to use alternative port which could be a possibility. Now in another article ISP allows TCP 25 and not UDP so some ISP restrict STMP access, but we are going from server to server with predefined mail server setup and active. Like my stupid thinking before lol... there could be port confict. as my network software each device that runs from mobile to computer needs it's own port.

Again i would suggest trying a high port number.

http://technet.microsoft.com/en-us/libr ... 65%29.aspx

Trouble shooting exchange 2003, i am sure you have something different but has a few notes tho.

Quote:
The SMTP server is refusing my connection attempts

The standard port used for SMTP connection is 25. Most often the reason for refused SMTP connection is that your Internet service provider blocks this port. That's why we recommend using the alternative port 2525 for your email SMTP connection.



For more information on how to check whether your ISP is blocking port 25, you can refer to this article.

Code: [ Select ]
Above: http://kb.siteground.com/article/The_SMTP_server_is_refusing_my_connection_attempt.html



idk about you but running out of idea's lol
  • wpas
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post September 5th, 2012, 11:16 am

The only ports open on both websites is port 25

I have no control over the other ports.

Is there another way, other than fsockeopen to connect to the smtp server.

There must be something as other websites can connect to both of my smtp servers on both websites
http://www.schembrionics.com
The Ultimate Solutions Center
  • wpas
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post September 5th, 2012, 8:31 pm

Hi Zealous

I found a website where you could enter the SMTP host and port number for testing at:

[url]
http://www.checktls.com/perl/TestReceiver.pl?FULL
[/url]

I entered my SMTP host names and port 25 and it was able to connect to both hosts on port 25.

If they can connect I should somehow also be able to connect.
This is really frustrating.
http://www.schembrionics.com
The Ultimate Solutions Center
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Joined: Apr 15, 2011
  • Posts: 1195
  • Loc: Sydney
  • Status: Offline

Post September 5th, 2012, 8:47 pm

dude we need BMW for this one, i am sending him a msg and asking for help. We need reinforcements on this.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8925
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post September 7th, 2012, 10:36 am

wpas, do you happen to have shell access to any of your shared hosting accounts that you are trying to get this to work on? Are they hosting accounts with different companies, or the same web host? My initial theory is that your two websites can't even connect to each other, which a simple ping test might be helpful for (as long as your hosts don't block ping). You could first try pinging from your local machine and if that works then try and ping from each host to each other to make sure that works. If that doesn't work then that means there are connection issues between the two web hosts that don't even relate to ports or SMTP.
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • wpas
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jul 12, 2010
  • Posts: 214
  • Loc: Canada
  • Status: Offline

Post September 7th, 2012, 11:32 am

Hi BWM

Both websites are hosted on different hosting account.

First I used my home computer to ping both websites.
I was able to ping one but not the other from my PC

I then went to each website and pinged each website from the other and that was OK. Each website was able to ping the other.
http://www.schembrionics.com
The Ultimate Solutions Center
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post September 7th, 2012, 11:32 am

Post Information

  • Total Posts in this topic: 56 posts
  • Users browsing this forum: No registered users and 245 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.