0

这是我第一次尝试制作联系表。而且我遇到了一些问题它的工作原理,我收到了电子邮件,但我没有在电子邮件中的姓名字段中获得姓名。

HTML:

<form method="post" name="contactform" action="scripts/contact.php">
<h3>Name</h3>
<input type="text" name="name">
<h3>Email Address</h3>
<input type="text" name="email">
<h3>Message</h3>
<textarea name="message"></textarea>
<br/><input class="submit" type="submit" value="Send Form">
</form>

PHP:

<?php
$to = "name@domane.com";
$subject = "Contact Us";
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $name, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "One of the field are not filled as requirred"; }
?>

$name是我的问题。我有它,电子邮件来自 hostmaster@domane.com,如果我删除它,一切正常。但我不想把名字发给我。如何?还是我应该完全不同?

此外,如果您将所有字段留空,“用户”不会收到任何错误消息,并且会向我发送一封空白电子邮件。

希望您能够帮助我。:)

4

3 回答 3

1

迈克尔·伯科夫斯基是正确的。您需要做的是将名称添加到您的邮件正文(不是输入name=属性的意义上,而是电子邮件的正文)。

像这样的东西:

<?php
$to = "name@domane.com";
$subject = "Contact Us";
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$headers = "From: $email";

$body = "Name: $name\r\n";
$body .= "Message: $message";

$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "One of the field are not filled as requirred"; }
?>



修改:

HTML:

<form method="post" name="contactform" action="scripts/contact.php">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
<label for="email">Email Address</label>
<input type="text" name="email" id="email" />
<label for="message">Message</label>
<textarea name="message" id="message"></textarea>
<br/><input class="submit" type="submit" value="Send Form" />
</form>

PHP:

<?php
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;

$body = "Name: $name\r\n";
$body .= "Message: $message";

$to = "name@domane.com";
$from = "automailer@mydomainname.com (Website Automailer)";
$subject = "Contact Us";
$headers =  "From: $from\r\n" .
        "Reply-To: $email ($name)";
$sent = mail($to, $subject, $body, $headers) ;
if($sent) { echo "Your mail was sent successfully"; }
else { echo "One of the field are not filled as requirred"; }
?>
于 2012-10-22T19:52:02.677 回答
0

您应该阅读mailphp.net 上的函数文档。

看一下函数签名:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

现在您将$name作为 " $additional_headers" 参数放置。您应该在参数中传递$name任何额外的相关数据$message

话虽如此,这是发送消息的正确代码:

$sent = mail($to, $subject, "A message from $name: $message", $headers);
于 2012-10-22T19:56:24.903 回答
-1

您应该阅读更多关于如何构建电子邮件的信息。除了将用户定义的消息放在那里,您可能还想指定一些电子邮件标题,包含更漂亮FROM:的标题等等......

于 2012-10-22T19:52:14.983 回答