简体   繁体   中英

PHP SMTP Mail with IP Address

Here is my contact mail PHP code:

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

My MsgHTML include name, topic, e-mail and message and I want to see IP address of the user. How can I do that?

you can get using following function.

 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;
  }

you can use this function to get client IP address

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;
}

Its ok but i want to see IP address in incoming mail from contact form.

In my code, MsgHTML include name, subject, mail and message how to add IP address information?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM