为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕丝7291255 2022-06-17 10:26:26
所以我的网站有多个页面,我希望人们可以选择向我们发送消息。我已经设置了一个 php 邮件程序,但所有邮件的布局都相同。如果邮件是从特定页面发送的,我如何在邮件中添加特定单词?邮件发件人可以在两个页面上工作,但布局和一切都是一样的。这是对我有用的代码:<?php// Check for empty fieldsif(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;?>
查看完整描述

2 回答

?
胡说叔叔

TA贡献1804条经验 获得超8个赞

您需要做的是在每个特定页面中添加一个隐藏字段,以便能够了解消息请求的来源,例如:


在一个页面上:


<input type='hidden' name='from' value='page1'>

在另一页上:


<input type='hidden' name='from' value='page2'>

然后在您的 php 文件中检查该$_POST['from']值,以便能够判断请求来自哪个页面。


所以你会做这样的事情:


<?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']));


if(!empty($_POST['from'])) { //if the from post var is not empty

    if($_POST['from'] == 'page1') { //if request came from page1

        $message .= " requested from page1"; //we append this text in the $message var

    }

}


// 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";

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

?>

或者,如果您无法添加该隐藏字段,则可以$_SERVER['HTTP_REFERER']在 php 代码中使用标头,该标头将包含请求来自的完整 URL。


查看完整回答
反对 回复 2022-06-17
?
茅侃侃

TA贡献1842条经验 获得超21个赞

这对我有用


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

?>


查看完整回答
反对 回复 2022-06-17
  • 2 回答
  • 0 关注
  • 132 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号