2016-03-21 54 views
8

我需要使用代理IP地址發送來自PHPMailer的電子郵件,我知道這樣做需要使用fsockopen函數,因此我可以連接到SMTP帳戶,知道如果我必須連接到代理服務器,我必須再次使用fsockopen函數。但在另一個fsockopen中使用它fsockopen是行不通的。使用代理服務器從PHPMailer發送電子郵件IP地址

我有透明代理,不需要驗證。我需要將它發送到外部電子郵件服務提供商的遠程SMTP服務器。

我曾嘗試代碼:

<?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); 

?> 

而且它不工作,我不知道爲什麼?

socat命令可以幫助在這種情況下,或有任何解決方案或替代解決方案來實現?

回答

2

我終於找到使用socat,請按照下列步驟操作的解決方案:

  1. 首先,您需要安裝socat在服務器上,您可以簡單地使用以下命令:

    yum install socat 
    
  2. 然後運行以下socat命令,將結合PROXY_IP:PORTHOST_ESP:PORT:通過HOST_ESP:PORT

    socat TCP4-LISTEN:proxy_port,bind=proxy_IP,fork,su=nobody TCP4:host:port,bind=proxy_IP 
    
  3. 然後,而不是做出發送到ESP的,你可以只讓使用PROXY_IP:PORTsocat將自動重定向到HOST_ESP:PORT使用輸出PROXY_IP:PORT

希望這會有所幫助。

2

難道這不是your earlier question的重複嗎?我看不出有太多變化。

您沒有正確使用代理(您無法在套接字內執行套接字),但PHPMailer沒有任何特定的代理支持。如果它將在任何地方,我會看看在SMTPOptions設置屬性,但據我所知PHP只提供proxy support in HTTP streams,所以你可能是SOL。運行本地郵件服務器來傳遞而不是代理可能更容易。

+1

感謝@Synchro爲您的快速重播,不確定如何在這種情況下使用SOL請添加示例如果可以。 (檢查我的更新)。 –

+0

恩,我不認爲你知道[SOL是什麼意思](http://onlineslangdictionary.com/meaning-definition-of/sol)...對不起! – Synchro

+0

當你有一個代理時,你不會直接與最終服務器通話(就像你正在做的那樣)。您連接到代理並向其發送命令*就好像您正在與真實服務器通話一樣。代理將命令轉發給目標服務器,並將響應中繼給您。在支持代理服務器的系統中,您通常會建立與代理服務器的連接,並將您真正想要連接的目標傳遞給您正在談論的任何協議的附加包裝。 PHP沒有SMTP代理支持,只有HTTP,所以你需要推出你自己的代理代碼,或者找到一個代理庫。 – Synchro

1

你可以試試這個...

<?php 

// The mailman object is used for sending and receiving email. 
$mailman = new COM("Chilkat.MailMan2"); 

// Any string argument automatically begins the 30-day trial. 
$success = $mailman->UnlockComponent('30-day trial'); 
if ($success != true) { 
    print 'Component unlock failed' . "\n"; 
    exit; 
} 

// To connect through an HTTP proxy, set the HttpProxyHostname 
// and HttpProxyPort properties to the hostname (or IP address) 
// and port of the HTTP proxy. Typical port numbers used by 
// HTTP proxy servers are 3128 and 8080. 
$mailman->HttpProxyHostname = 'www.my-http-proxy.com'; 
$mailman->HttpProxyPort = 3128; 

// Important: Your HTTP proxy server must allow non-HTTP 
// traffic to pass. Otherwise this does not work. 

// Set the SMTP server. 
$mailman->SmtpHost = 'smtp.chilkatsoft.com'; 

// Set the SMTP login/password (if required) 
$mailman->SmtpUsername = 'myUsername'; 
$mailman->SmtpPassword = 'myPassword'; 

// Create a new email object 
$email = new COM("Chilkat.Email2"); 

$email->Subject = 'This is a test'; 
$email->Body = 'This is a test'; 
$email->From = 'Chilkat Support <[email protected]>'; 
$email->AddTo('Chilkat Admin','[email protected]'); 

// Call SendEmail to connect to the SMTP server via the HTTP proxy and send. 
// The connection (i.e. session) to the SMTP server remains 
// open so that subsequent SendEmail calls may use the 
// same connection. 
$success = $mailman->SendEmail($email); 
if ($success != true) { 
    print $mailman->lastErrorText() . "\n"; 
    exit; 
} 

// Some SMTP servers do not actually send the email until 
// the connection is closed. In these cases, it is necessary to 
// call CloseSmtpConnection for the mail to be sent. 
// Most SMTP servers send the email immediately, and it is 
// not required to close the connection. We'll close it here 
// for the example: 
$success = $mailman->CloseSmtpConnection(); 
if ($success != true) { 
    print 'Connection to SMTP server not closed cleanly.' . "\n"; 
} 

print 'Mail Sent!' . "\n"; 
?> 
相關問題