在电子邮件中显示表单提交的页面

时间:2018-03-07 15:14:13

标签: php wordpress email post superglobals

我在每个页面都有一个html表单,我需要能够在接收者的电子邮件中显示访问者提交表单的页面。我怎样才能在PHP中实现这一点?我曾尝试使用$_SERVER['REQUEST_URI'],但它根本就没有输出任何东西。我正在使用Wordpress。

<?php
  global $post;
  $post_slug=$post->post_name;
  $name = $_POST['firstname'];
  $email = $_POST['email'];
  $message="$name.$email";
    mail('example@gmail.com', "Hello", "$name \n $email \n $_SERVER['REQUEST_URI']");
    echo "works";
?>

3 个答案:

答案 0 :(得分:1)

您的代码很好,除非您应该将数组变量括在带有卷曲大括号{}的字符串中:

mail('example@gmail.com', "Hello", "$name \n $email \n {$_SERVER['REQUEST_URI']}");

如果您查看官方php文档:http://php.net/manual/en/language.types.string.php#language.types.string.parsing,您可以在“复杂(卷曲)语法”部分中看到:

// Works, quoted keys only work using the curly brace syntax
echo "This works: {$arr['key']}";

答案 1 :(得分:0)

您可以通过以下方式获取实际网址:

$actual_link = "http(s)://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

答案 2 :(得分:0)

你可以尝试一下这个。将页面标题(或永久链接)传递给隐藏的html输入,并将值取为:

 $titlePage = get_the_title($post->ID);

 <input type="hidden" value="<?php echo $titlePage; ?>" name="pagetitle">

然后在您的电子邮件地址中:

 $pagetitle = $_POST['pagetitle'];

现在你有了参数,可以在你想要的电子邮件中使用它。