Connecting to remote SMTP mail server

  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

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
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1240
  • Loc: Sydney

Post 3+ Months Ago

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
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

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
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1240
  • Loc: Sydney

Post 3+ Months Ago

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
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

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.
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1240
  • Loc: Sydney

Post 3+ Months Ago

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
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

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
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

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
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

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
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1240
  • Loc: Sydney

Post 3+ Months Ago

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
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

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
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

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.
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1240
  • Loc: Sydney

Post 3+ Months Ago

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
  • Posts: 9086
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

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.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

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.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9086
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

So it sounds like the connection to the servers are there. I do think this is a firewall problem then. Most websites will allow you to connect port 25 inbound, but not necessarily the other way around which explains why it works for you when you test from your local machine.

When you test from your local machine to port 25 on any server you are inbound to that server, and thus their firewalls are likely allowing it.

However, when you do it from server to server, you are inbound on one and outbound on the other. The firewalls on each server are likely blocking outbound port 25, but allowing inbound port 25. So the only way around this would be either to change the firewall settings, or to use a different port that you can go outbound on.

This is very common, as you can see here for example:

https://csguide.cs.princeton.edu/email/ ... =node%2F86

Quote:
By blocking outbound packets on port 25/tcp, we limit the ability of compromised hosts on our network to send out unsolicited bulk e-mail to other sites.


What you might want to do to confirm this is to e-mail each host and ask them if they block port 25 outbound, make sure you get someone who actually knows the answer too.

Hope that helps identifying the problem, maybe it will get you going in the right direction to find a solution :)
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

I used a script myself to check that port 25 was open on both websites and it was.

A couple of posts back I indicated that the following url

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

could access both mx hosts on port 25 with no problem.

I do not think port 25 is the problem
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9086
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

That script is testing for port 25 inbound though on both servers, not outbound. I believe that site only checks for one direction.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

checking inbound is basically what I want.

I want to be able to verify emails like they do.

I have a script that when someone registers on my site, I check for proper syntax and that an MX mail domain exists.

As a last test, I want to connect to the smtp server to see if it actually exists.

I realize that some smtp servers accept all email and then dispose of them later so this is not as good as actually sending an email and waiting for a response.

This is the basic reason why I need to connect to remote smtp servers.

I was using my websites as testing sites but could not connect to them inbound as the website I mentioned does.

I have not used perl scripts but I read somewhere that you can also connect to smtp servers via some smtp script. I wonder if this might help.

The only problem is I never worked with perl scripts and so I am not sure I could figure it out with weeks of learning how perl works.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

I have not seen any further responses.

Does this mean I am out of luck
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1240
  • Loc: Sydney

Post 3+ Months Ago

this is the 3rd week..... lol

idk what else to do with it really
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9086
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

I really believe you should contact your web host and ask them how their firewall is setup, and if inbound or outbound port 25 is blocked. I believe at this time you still don't know that answer as you are only able to test one direction with that website resource you listed, and it makes it hard to figure out exactly the cause to your issue. If one direction is blocked, then my advice would be to use a different web host, or find a way to use a different port that has both inbound and outbound open. I think you will have this same problem no matter what language your script is in, whether it is PHP or PERL.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

When I do use the website resource I mentioned I can communicate with both SMTP servers on both websites and I do get responses back from each server when I connect and then use the standard HELO, MAIL FROM, RCPT TO, RSET and QUIT commands. The servers respond to each command I send.

If port 25 were blocked, I would expect not to get any responses back.

I must therefore assume that port 25 is not blocked.

Unless I am not thinking correctly here.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9086
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

That is only for inbound on port 25 though. The test that you are running is from another server that is trying to connect to port 25 on your servers and your server responds to that request (as it is an inbound connection and allowed). You are not testing going outbound on port 25 originating from your server, so that is different. It is possible to have a port open in one direction and not the other direction. Many hosts do block outbound on port 25 and leave inbound port 25 open which lets them receive mail. The reason they block outbound is to try and prevent spam from originating from their servers. Many hosts will require outbound to be on other ports besides port 25 if they allow it. This is why I recommend contacting your webhosts to see how they have things configured as this can at least give you the information we need to help you further here and if we can rule this out or not. At the moment it still seems like the likely problem to me.

Hope that makes sense :)
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9086
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Here is a simple PHP script that you can use to test if a port is open or closed, maybe this can help:

PHP Code: [ Select ]
$fp = fsockopen('127.0.0.1', 25, $errno, $errstr, 5);
if (!$fp) {
    // port is closed or blocked
} else {
    // port is open and available
    fclose($fp);
}
  1. $fp = fsockopen('127.0.0.1', 25, $errno, $errstr, 5);
  2. if (!$fp) {
  3.     // port is closed or blocked
  4. } else {
  5.     // port is open and available
  6.     fclose($fp);
  7. }
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

I used your script and changed it to echo a message back.

On both websites port 25 is open and available.

Actually, I had also used a similar script to check port 25 which was found to be open and available on both websites.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9086
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Awesome, so that confirms inbound does work like you mentioned. Now let's check to see if outbound works. Go ahead and modify your script to instead of using '127.0.0.1', use: '67.18.182.194'. That is Ozzu's mail server, and everyone who send's mail to ozzu would connect to that IP address. If your script passes that test, then that confirms you are open on port 25 both inbound and outbound. Let me know the results when you get a chance :)
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

I modified the script to include the OZZU mail server and put it on both websites.

Each website was not able to connect to your mail server.
Got error 110, Connection Timed Out in both cases.

But this is the problem I have been enquiring about in this thread.
I can connect locally, but not remotely.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9086
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Yeah I suppose it is, guess that just re-confirms the problem.

You really just need to contact your webhosts now and confirm they are blocking outbound port 25, and if they have any work-arounds for that. Some hosts just do not allow you to send mail from there servers, and if that is the case you are going to be out of luck and need to find a new host. They may also have an alternate server that you can send mail out of. Please contact them and see what they say.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 321
  • Loc: Canada

Post 3+ Months Ago

I still do not understand the problem.

I do not want to send emails. I only want to try to validate an email address.

You indicated previously that with the web source I was using:

"That is only for inbound on port 25 though. The test that you are running is from another server that is trying to connect to port 25 on your servers and your server responds to that request (as it is an inbound connection and allowed). "

This is really all I want to do.
So if inbound is allowed why can I not connect to the server.
If outbound is for sending emails, I do not want to send emails.

Am I missing something
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 56 posts
  • Users browsing this forum: No registered users and 119 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
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.