为网站上的每个页面添加标识符

所以我的网站有多个页面,我希望人们可以选择向我们发送消息。我已经设置了一个 php 邮件程序,但所有邮件的布局都相同。如果邮件是从特定页面发送的,我如何在邮件中添加特定单词?邮件发件人可以在两个页面上工作,但布局和一切都是一样的。


这是对我有用的代码:


<?php

// Check for empty fields

if(empty($_POST['name'])      ||

   empty($_POST['email'])     ||

   empty($_POST['phone'])     ||

   empty($_POST['message'])   ||

   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))

   {

   echo "No arguments Provided!";

   return false;

   }


$name = strip_tags(htmlspecialchars($_POST['name']));

$email_address = strip_tags(htmlspecialchars($_POST['email']));

$phone = strip_tags(htmlspecialchars($_POST['phone']));

$message = strip_tags(htmlspecialchars($_POST['message']));


$URL = $_SERVER['HTTP_REFERER'];





// Create the email and send the message

$to = 'boonwijkkermis@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.

$email_subject = "Website Contact Form:  $name";

$email_body = "Nieuwe mail ontvangen via boonwijkkermis.com.\n\nNaam:\n $name \n\nEmail:\n $email_address \n\nTelefoon nummer:\n $phone \n\nBericht:\n$message \n\nURL:\n $URL";

$headers = "From: no-reply@boonwijkkermis.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.

$headers .= "Reply-To: $email_address";   

mail($to,$email_subject,$email_body,$headers);

return true;

?>


慕丝7291255
浏览 133回答 2
2回答

胡说叔叔

您需要做的是在每个特定页面中添加一个隐藏字段,以便能够了解消息请求的来源,例如:在一个页面上:<input type='hidden' name='from' value='page1'>在另一页上:<input type='hidden' name='from' value='page2'>然后在您的 php 文件中检查该$_POST['from']值,以便能够判断请求来自哪个页面。所以你会做这样的事情:<?php// Check for empty fieldsif(empty($_POST['name'])&nbsp; &nbsp; &nbsp; ||&nbsp; &nbsp;empty($_POST['email'])&nbsp; &nbsp; &nbsp;||&nbsp; &nbsp;empty($_POST['phone'])&nbsp; &nbsp; &nbsp;||&nbsp; &nbsp;empty($_POST['message'])&nbsp; &nbsp;||&nbsp; &nbsp;!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))&nbsp; &nbsp;{&nbsp; &nbsp;echo "No arguments Provided!";&nbsp; &nbsp;return false;&nbsp; &nbsp;}$name = strip_tags(htmlspecialchars($_POST['name']));$email_address = strip_tags(htmlspecialchars($_POST['email']));$phone = strip_tags(htmlspecialchars($_POST['phone']));$message = strip_tags(htmlspecialchars($_POST['message']));if(!empty($_POST['from'])) { //if the from post var is not empty&nbsp; &nbsp; if($_POST['from'] == 'page1') { //if request came from page1&nbsp; &nbsp; &nbsp; &nbsp; $message .= " requested from page1"; //we append this text in the $message var&nbsp; &nbsp; }}// Create the email and send the message$to = 'boonwijkkermis@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.$email_subject = "Website Contact Form:&nbsp; $name";$email_body = "Nieuwe mail ontvangen via boonwijkkermis.com.\n\nNaam:\n $name \n\nEmail:\n $email_address \n\nTelefoon nummer:\n $phone \n\nBericht:\n$message";$headers = "From: no-reply@boonwijkkermis.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.$headers .= "Reply-To: $email_address";&nbsp; &nbsp;mail($to,$email_subject,$email_body,$headers);return true;?>或者,如果您无法添加该隐藏字段,则可以$_SERVER['HTTP_REFERER']在 php 代码中使用标头,该标头将包含请求来自的完整 URL。

茅侃侃

这对我有用<?php// Check for empty fieldsif(empty($_POST['name'])&nbsp; &nbsp; &nbsp; ||&nbsp; &nbsp;empty($_POST['email'])&nbsp; &nbsp; &nbsp;||&nbsp; &nbsp;empty($_POST['phone'])&nbsp; &nbsp; &nbsp;||&nbsp; &nbsp;empty($_POST['message'])&nbsp; &nbsp;||&nbsp; &nbsp;!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))&nbsp; &nbsp;{&nbsp; &nbsp;echo "No arguments Provided!";&nbsp; &nbsp;return false;&nbsp; &nbsp;}$name = strip_tags(htmlspecialchars($_POST['name']));$email_address = strip_tags(htmlspecialchars($_POST['email']));$phone = strip_tags(htmlspecialchars($_POST['phone']));$message = strip_tags(htmlspecialchars($_POST['message']));$URL = $_SERVER['HTTP_REFERER'];// Create the email and send the message$to = 'boonwijkkermis@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.$email_subject = "Website Contact Form:&nbsp; $name";$email_body = "Nieuwe mail ontvangen via boonwijkkermis.com.\n\nNaam:\n $name \n\nEmail:\n $email_address \n\nTelefoon nummer:\n $phone \n\nBericht:\n$message \n\nURL:\n $URL";$headers = "From: no-reply@boonwijkkermis.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.$headers .= "Reply-To: $email_address";&nbsp; &nbsp;mail($to,$email_subject,$email_body,$headers);return true;?>
打开App,查看更多内容
随时随地看视频慕课网APP