使用代理IP地址从{PHP}发送来自PHPMailer的电子邮件

时间:2016-03-21 17:37:17

标签: php email proxy phpmailer fsockopen

我需要使用代理IP地址从 PHPMailer 发送电子邮件,我知道要这样做,我需要使用fsockopen函数,以便我可以连接到SMTP帐户,我也知道如果我必须连接到代理,我必须再次使用fsockopen函数。但是在另一个fsockopen中使用它fsockopen是不可行的。

我有透明代理,不需要身份验证。我需要将其发送到外部电子邮件服务提供商的远程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命令可以在这种情况下提供帮助,还是有任何解决方案或替代解决方案来实现这一目标?

3 个答案:

答案 0 :(得分:3)

我终于使用 socat 找到了解决方案,请按以下步骤操作:

  1. 首先,您需要在服务器上安装socat,您只需使用以下命令即可:

    yum install socat
    
  2. 然后运行以下socat命令,该命令将 PROXY_IP:PORT HOST_ESP:PORT 绑定:

    socat TCP4-LISTEN:proxy_port,bind=proxy_IP,fork,su=nobody TCP4:host:port,bind=proxy_IP
    
  3. 然后,您可以通过 HOST_ESP:PORT 和{{1}来发送,而不是通过 PROXY_IP:PORT 发送给ESP我会使用 socat 的输出自动重定向到 HOST_ESP:PORT

  4. 希望这有帮助。

答案 1 :(得分:2)

这不是your earlier question的重复吗?我没有看到太多变化。

您没有正确使用代理(您无法在套接字内部执行套接字),但PHPMailer没有任何特定的代理支持。如果它将在任何地方,我会在SMTPOptions中查看设置属性,但据我所知,PHP仅提供proxy support in HTTP streams,因此您可能是SOL。运行本地邮件服务器来中继而不是代理可能更容易。

答案 2 :(得分: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 <support@chilkatsoft.com>';
$email->AddTo('Chilkat Admin','admin@chilkatsoft.com');

//  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";
?>