2

I am using phpmailer to send e-mails and had some problems with sending on my hosting when I need to connect to remote mail server. I got info from tech support that I need to bind my server ip with remote server. It's the first time I am messing with sockets ever.

Unfortunately phpmailer uses fsocketopen, so this is how I changed it:

//my replacement code
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$conn = socket_connect($socket, $host, $port);
if($conn) {
    $this->smtp_conn = $socket;
} else {
    throw new Exception("Failed to connect to server: ".socket_last_error($socket));
}

//original phpmailer code
/**
$this->smtp_conn = @fsockopen($host,    // the host of the server
                             $port,    // the port to use
                             $errno,   // error number if any
                             $errstr,  // error message if any
                             $tval);   // give up after ? secs
 */

But after that change I receive warnings:

  • Warning: fputs(): supplied resource is not a valid stream resource

  • Warning: socket_get_status(): supplied resource is not a valid stream resource

How can I create resource that would be compatible with resource returned from fsockopen? Using var_dump it says those two vars are both sockets. But I still get warnings for resource created usign socket_create.

Somal Somalski
  • 578
  • 1
  • 7
  • 21

2 Answers2

0

You need a stream resource returned, not a socket resource. Trying using the function stream_socket_client().

$this->smtp_conn = stream_socket_client("tcp://".$host.":".$port,
$errno,
$errstr,
$tval);

Also, are you sure your hosting provider allows you to send email outbound directly from your server? Sometimes they will provide a relay mail server for you to use for outbound email.

Derek Wright
  • 1,452
  • 9
  • 11
  • Thanks for answer, but on live regarding to tech-support I should socket_bind my ip with remote mail server. Using stream_socket_client I will have already created connection. Or cant I bind after connecting? Sorry for so many questions, I am no master on network issues. – Somal Somalski Jan 11 '12 at 17:20
  • You dont "bind" on an IP when you connect to a client. Binding is for creating a listening connection and then you "bind()" to a local interface/address to listen for incoming connections on. On an outbound client connection, you provide the destination Host(IP) and Port and it will connect to the far end service. Do you have any errors or logs that show any more information? – Derek Wright Jan 11 '12 at 18:07
  • So do I need this "binding" to send SMTP message? Does it make any sense or my host tech-support is wrong? More info from telnet connection: 450 Client host rejected: cannot find your hostname, [213.134.162.98] 421 server.na.me Error: timeout exceeded – Somal Somalski Jan 13 '12 at 12:16
  • Is it some reverse dns issue on my mail server? – Somal Somalski Jan 13 '12 at 12:17
  • Looks like you dont have a PTR on that IP, so definitely reverse lookups are going to hurt your ability to send email. In this case see if your tech support provides outbound relay mail and should be able to configure the mailer to relay the message to a diff server. Otherwise, you should put a ptr on that which matches the forward lookup of the webserver. – Derek Wright Jan 13 '12 at 13:15
  • I should set PTR for domain of my website for ip: 213.134.162.98. That's correct? However I have asked about relay mail. – Somal Somalski Jan 13 '12 at 15:05
0

See this answer

And you also can ask me for my own implementation smtp-class plugin for PHPMailer which can send email from diffenet IPs at one computer. See contact in my account.

P.S. Sorry for my English

Community
  • 1
  • 1
Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90