这是我第一次尝试建立联系表格。而且我遇到了一些问题 这是有效的,我收到了电子邮件,但我没有在电子邮件中将名称字段与我联系。
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,如果删除它,一切正常。但我不想把这个名字发给我。怎么样? 或者我应该完全不同吗?
此外,如果您将所有字段留空,则“用户”不会收到任何错误消息,并且会向我发送一封空白电子邮件。
希望你能帮助我。 :)答案 0 :(得分:2)
Michael Berkowski是对的。您需要做的是将名称添加到邮件正文中(不是输入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"; }
?>
答案 1 :(得分:1)
您应该阅读php.net上的mail
函数文档。
查看函数签名:
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);
答案 2 :(得分:-1)
您应该详细了解如何构建电子邮件。您可能希望指定一些电子邮件标题,包含更漂亮的FROM:
等等,而不仅仅是在其中放置用户定义的邮件...