Last active
November 19, 2021 12:07
-
-
Save mrcgrtz/a9713513f2be4f8ed5cda1889850f064 to your computer and use it in GitHub Desktop.
Simple PHP script for mailing an HTML5 "ping"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<meta charset="utf-8"> | |
<title>Ping Test</title> | |
<p>Click this <a href="https://example.com" ping="/ping.php">test link</a>. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
// Configuration: Set allowed host. | |
$host = 'https://my.host'; | |
// Configuration: Set mail from/to. | |
$from = $_SERVER['SERVER_ADMIN']; | |
$to = $_SERVER['SERVER_ADMIN']; | |
// Get the Ping-From header value. | |
$headers = apache_request_headers() ?? []; | |
$url = $headers['Ping-From'] ?? ''; | |
// Check for a valid ping from my host. | |
if (!empty($url) && str_starts_with($url, $host)) { | |
// Prepare the mail. | |
$subject = 'Ping'; | |
$headers = [ | |
'MIME-Version' => '1.0', | |
'Content-Type' => 'text/plain; charset="utf-8"', | |
'Content-Transfer-Encoding' => '8bit', | |
'From' => $from, | |
'X-Mailer' => 'PHP/' . phpversion(), | |
]; | |
$body = implode("\r\n", [ | |
'Ping from: ' . $url, | |
'Date/time: ' . strftime('%c'), | |
]); | |
// Mail the ping. | |
if (!mail($to, $subject, $body, $headers)) { | |
// Sending a mail did not work, respond with server error. | |
http_response_code(500); | |
exit(); | |
} | |
// Sending the mail worked, a ping mail was created. | |
http_response_code(201); | |
exit(); | |
} | |
// The request was bad. | |
http_response_code(400); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment