8

I need to send emails from PHPMailer using proxies IP addresses, I know that to do so, I need to use the fsockopen function so I can connect to the SMTP account, I also know that if I have to connect to the proxy I have to use the fsockopen function again. But using it fsockopen inside another fsockopen is not doable.

I have transparent proxy and require no authentication. I need to send this to a distant SMTP server of an external Email Service Provider.

The code I have tried :

<?php

    //SMTP params
    $server      = 'smtp.espdomain.com';
    $server_port = '25';
    $username = 'smtp_login';
    $password = 'smtp_pass';

    //Proxy
    $proxy      = '1.1.1.1';
    $proxy_port = 1111;

    //Open connection
    $socket = fsockopen($proxy, $proxy_port);

    //Send command to proxy
    fputs($socket, "CONNECT $server:$server_port HTTP/1.0\r\nHost: $proxy\r\n\r\n");
    fgets($socket, 334);

    //SMTP authorization  
    fputs($socket, "AUTH LOGIN\r\n");
    fgets($socket, 334);

    fputs($socket, base64_encode($username)."\r\n");
    fgets($socket, 334);

    fputs($socket, base64_encode($password)."\r\n");
    $output = fgets($socket, 235);

    fputs($socket, "HELO $server \r\n"); 
    $output = fgets($socket, 515);

?>

And it's not working I'm not sure why?

Could socat commands help in this situation or is there any solution or alternative solution to achieve that?

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101

2 Answers2

4

I finally found the solution using socat, Kindly follow these steps :

  1. First of all, you'll need to install socat on the server, you can do that simply using the command below :

    yum install socat
    
  2. Then run the following socat command that will bind PROXY_IP:PORT with HOST_ESP:PORT :

    socat TCP4-LISTEN:proxy_port,bind=proxy_IP,fork,su=nobody TCP4:host:port,bind=proxy_IP
    
  3. Then instead of making a send to the ESP through HOST_ESP:PORT you could just make it using PROXY_IP:PORT and socat will do the redirection automatically towards HOST_ESP:PORT using the output of PROXY_IP:PORT.

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
  • 1
    just for my ignorance, which of the words in your sample command need to be replaced with actual IPs? – PKHunter Sep 26 '18 at 00:36
  • Thank you, very helpful. @zakaria – PKHunter Sep 26 '18 at 14:28
  • 2
    @zakaria You just saved my day man, I'm extremely thankful for you. By the way, if anyone having confusion with the syntax, it's simple: The **proxy** here is your server (the same or any other) that will act as a proxy/medium between your application and the SMTP server. `proxy_IP` = your server's hostname or IP. `proxy_port` = Any available port in your server, this will become the SMTP port in your application to connect to. `host:port` = This is the actual SMTP host & port in that format via which you want to send emails. Run this command in the server which you want to make a proxy. – Sukanta Paul Mar 08 '20 at 20:39
  • Glad I could help brother. – Zakaria Acharki Mar 09 '20 at 09:15
2

Isn't this a repeat of your earlier question? I don't see that much has changed.

You are not using the proxy correctly (you can't do sockets inside sockets), but PHPMailer doesn't have any specific proxy support. If it was going to be anywhere, I'd look at setting properties in SMTPOptions, though as far as I can see PHP only offers proxy support in HTTP streams so you may be SOL. It's probably easier to run a local mail server to relay rather than proxy.

Community
  • 1
  • 1
Synchro
  • 35,538
  • 15
  • 81
  • 104
  • 1
    Thanks @Synchro for your quick replay, not sure how I could use SOL in this situation please add an example if you can. (check my update). – Zakaria Acharki Mar 23 '16 at 10:50
  • Um, I don't think you know [what SOL means](http://onlineslangdictionary.com/meaning-definition-of/sol)... Sorry! – Synchro Mar 23 '16 at 10:59
  • When you have a proxy, you don't talk to the final server directly (as you're trying to do). You connect to the proxy and send commands to it *as if* you were talking to the real server. The proxy relays the commands to the target server, and relays responses back to you. In systems that support proxies you normally establish connection to the proxy and pass along the target that you really want to connect to as an additional wrapper around whatever protocol you're talking. PHP doesn't have SMTP proxy support, only HTTP, so you would need to roll your own proxy code, or find a proxy library. – Synchro Mar 23 '16 at 11:01
  • @Synchro have you come across a proxy library to use with PHP? Google hasn't been helpful for mail purposes. – PKHunter Sep 26 '18 at 00:32