2
votes

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.

2

2 Answers

0
votes

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.

0
votes

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