带有IP地址的PHP SMTP邮件

时间:2018-05-28 12:11:13

标签: php email smtp ip contacts

这是我的联系邮件PHP代码:

<?php
      require "class.phpmailer.php";
      $mail=new PHPMailer();
      $mail->IsSMTP();
      $mail->SMTPDebug = 1;
      $mail->SMTPAuth = true;
      $mail->Host = "domain.mail.com";
      $mail->Port = 000; 
      $mail->Username = 'username@gmail.com';
      $mail->Password = 'pass';
      $mail->SetFrom($mail->Username, $_POST['name']);
      $mail->AddAddress('username@gmail.com', 'username');
      $mail->CharSet = 'UTF-8';
      $mail->Subject = $_POST["topic"];
      $mail->MsgHTML('Name: '.$_POST["name"].'<br/>
                      Subject: '.$_POST["topic"].'<br/>
                      E-Mail: '.$_POST["email"].'<br/>
                      Message: '.$_POST["message"].'<br/>');

      if($mail->Send()) {
          echo "<script>alert('Message successfully sent.');</script>";

          header ("Refresh:0; url=index.html");
      }else { 
          echo  $mail->ErrorInfo;
      }
  }}
?>

我的MsgHTML包括名称,主题,电子邮件和消息,我想查看用户的IP地址。我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:0)

你可以使用以下功能。

 function GetIpAddress()
 {
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check if its shared
    {
       $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
   elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //if ip is from proxxyfrom proxy
     {
        $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
     }
    else
    {
        $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
  }

答案 1 :(得分:0)

您可以使用此功能获取客户端IP地址

function get_client_ip() {
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP']))
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_X_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if(isset($_SERVER['REMOTE_ADDR']))
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}

答案 2 :(得分:0)

没问题,但我希望从联系表单中看到收到的邮件中的IP地址。

在我的代码中,MsgHTML包括名称,主题,邮件和消息如何添加IP地址信息?