【问题标题】:add identifier to each page on site为网站上的每个页面添加标识符
【发布时间】:2019-12-13 11:32:29
【问题描述】:

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

这是适合我的代码:

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

【问题讨论】:

    标签: php html email


    【解决方案1】:

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

    在页面上:

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

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

    【讨论】:

    • @DaanVandeVoorde “我如何检查 $_POST['from'] 值”...与您当前在代码中读取其他 POST 值的方式相同。例如像这样$from = $_POST["from"];。然后if ($from == "page1") { //do something } else if ($from == "page2") { //do something else }...等,
    • @DaanVandeVoorde 我已经编辑了我的答案并添加了完整的代码
    • 每页需要添加输入字段
    • 我编辑了代码,所以如果页面无法识别 $from_page = no page。邮件保持不变,因此不会添加数据。我该如何解决这个问题
    • 它还没有修复,但我编辑了代码,它工作了,但我又试了一次,它又坏了
    【解决方案2】:

    这对我有用

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      相关资源
      最近更新 更多