簡體   English   中英

如何通過電子郵件通過PHP發送整個請求

[英]How to Send via email the Entire Request in PHP

我正在嘗試創建一個通過電子郵件發送有關該請求的所有可能信息的php。 目前,我正在使用類似:

電子郵件請求php:

<?php

$remoteIp = $_SERVER['REMOTE_ADDR'];
$remoteHost = $_SERVER['HTTP_HOST'];
$remoteRef = $_SERVER['HTTP_REFERER'];
$remoteUrl = $_SERVER['REQUEST_URI'];
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$yourEmailAddress = "user@mail.com";
$emailSubject = "New request from: ".$remoteIp;

$emailContent = "The URL Request was made to: $remoteUrl 
The request REFERER was: $remoteRef
The IP was $remoteIp
The User Agent was: $userAgent";

// send the message
mail($yourEmailAddress, $emailSubject, $emailContent);
?>

我想將Entire http request (GET或POST)添加到已發送的電子郵件中,因此$emailContent如下所示:

    $emailContent = "The URL Request was made to: $remoteUrl 
    The request REFERER was: $remoteRef
    The IP was $remoteIp
    The User Agent was: $userAgent"
    The Full request was: 
    $fullrequest";

環顧四周,我發現了這個https://gist.github.com/magnetikonline/650e30e485c0f91f2f40 ,它允許您創建文件,但是我不確定如何將其與PHP放在一起,因此它向我發送了一封包含請求的電子郵件。 (無需創建文件)

我喜歡將此PHP集成到我的電子郵件請求php中

<?php
// https://gist.github.com/magnetikonline/650e30e485c0f91f2f40
class DumpHTTPRequestToFile {
    public function execute($targetFile) {
        $data = sprintf(
            "%s %s %s\n\nHTTP headers:\n",
            $_SERVER['REQUEST_METHOD'],
            $_SERVER['REQUEST_URI'],
            $_SERVER['SERVER_PROTOCOL']
        );
        foreach ($this->getHeaderList() as $name => $value) {
            $data .= $name . ': ' . $value . "\n";
        }
        $data .= "\nRequest body:\n";
        file_put_contents(
            $targetFile,
            $data . file_get_contents('php://input') . "\n"
        );
        echo("Done!\n\n");
    }
    private function getHeaderList() {
        $headerList = [];
        foreach ($_SERVER as $name => $value) {
            if (preg_match('/^HTTP_/',$name)) {
                // convert HTTP_HEADER_NAME to Header-Name
                $name = strtr(substr($name,5),'_',' ');
                $name = ucwords(strtolower($name));
                $name = strtr($name,' ','-');
                // add to list
                $headerList[$name] = $value;
            }
        }
        return $headerList;
    }
}
(new DumpHTTPRequestToFile)->execute('./dumprequest.txt');

誰能幫我將class DumpHTTPRequestToFile$fullrequest嗎?

就像是

<?php

$message="The following request was made:\n";

foreach($_REQUEST as $k=>$v){

$message.=$k." : ".$v."\n\n";

}

mail($to_address, $subject, $message, $headers);

?>

$_REQUEST替換$_REQUEST您選擇的超級全局$_REQUEST 如果$_REQUEST中沒有您想要的全部內容,也可以在多個超全局變量( $_SERVER等)中運行類似的循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM