我看了很多教程,但我似乎仍然无法让我的联系表格正常工作。这真让我感到沮丧。我联系表单的HTML代码是:
form action="server.php" method="post">
<p>
<label for="name">Name:</label>
<input name="name" id="name" type="text" class="required">
<span>Please enter your name</span>
</p>
<p>
<label for="email">Email:</label>
<input name="email" id="email" type="text" class="required">
<span>Please enter a valid email address</span>
</p>
<p>
<label for="subject">Subject:</label>
<input name="subject" id="subject" type="text">
<span>Please enter your subject</span>
</p>
<p>
<label for="message">Message</label>
<textarea name="message" id="message" class="required"></textarea>
<span>Please enter your message</span>
</p>
<p class="submit">
<input type="submit" value="Submit" class="btn-submit">
</p>
</form>
我的PHP代码是:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: F-Sharp Media';
$to = 'f.ajibade@f-sharpmedia.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail( "khanyasser@live.co.uk" , "New Project" , $body);
header("Location:email_success.php");
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all required fields!!</p>';
}
}
?>
这是第二个php文件:
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<title>F-Sharp Media | Welcome</title>
<link rel="stylesheet" href="css/foundation.css" />
<link rel="stylesheet" href="css/main.css" />
<script src="js/vendor/modernizr.js"></script>
</head>
<body>
<h1> Email Sent</h1>
<p>Your e-mail has been sent<p/>
<p><a href="JavaScript:history.go(-1);">Back</a></p>
</body>
</html>
答案 0 :(得分:1)
<强>更新强>
最终答案(经过全面测试并与现场验证配合使用)
好的,对于您的表单页面..
<form action="server.php" method="post">
<h1> Contact us </h1>
<label for="name">Name:</label>
<span>Please enter your name</span>
<p> <input type="text" name="name"> </p>
<label for="email">Email:</label>
<span>Please enter a valid email address</span>
<p> <input type="text" name="email"> </p>
<label for="subject">Subject:</label>
<span>Please enter your subject</span>
<p> <input type="text" name="subject"> </p>
<label for="message">Message</label>
<span>Please enter your message</span>
<p> Message: </p> <p> <textarea rows="4" cols="50" name="message"> </textarea> </p>
<p> <input type="submit" value="submit"> </p>
</form>
server.php页面:
如果您决定更改此页面的名称,请记住在“表格”操作中更新名称。第一页表格上的部分
PHP:
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message= $_POST["message"];
//Sending information to an email
$to = "khanyasser@live.co.uk";
$from = $email;
$headers = "From: " . $from;
if ($name != '' && $email != '' && $subject != '' && $message != '')
{
mail($to,$subject,$message,$headers);
echo "mail sent";
} else {
echo "Please fill in all of the required fields";
}
如果您有任何问题,请告诉我,我会进一步帮助您,但我认为这可以解决您的问题。
答案 1 :(得分:0)
你犯的是非常愚蠢的错误,实际上你并没有将<
放在form
的开头。如果你在php文件中收到任何错误,请发布它,以便我们可以帮助你。我没有在php中发现任何错误但我只在html语法中发现了错误。我认为你的服务器在压力下被粉碎,给你的服务器一些时间,而不是检查你的电子邮件,我相信你很快就会收到消息。
答案 2 :(得分:0)
<form action="server.php" method="post">
<p>
<label for="name">Name:</label>
<input name="name" id="name" type="text" class="required">
<span>Please enter your name</span>
</p>
<p>
<label for="email">Email:</label>
<input name="email" id="email" type="text" class="required">
<span>Please enter a valid email address</span>
</p>
<p>
<label for="subject">Subject:</label>
<input name="subject" id="subject" type="text">
<span>Please enter your subject</span>
</p>
<p>
<label for="message">Message</label>
<textarea name="message" id="message" class="required"></textarea>
<span>Please enter your message</span>
</p>
<p class="submit">
<input type="submit" value="Submit" class="btn-submit" name="send" />
</p>
</form>
您的server.php脚本
<?php
define('FROM', 'F-Sharp Media');
define('RECIPIENT', 'f.ajibade@f-sharpmedia.com');
define('SUBJECT', 'Hello');
if ( ! isset($_POST['send']))
{
header('Location: form.html');
die;
}
$errors = array();
if ( ! isset($_POST['name']) || empty($_POST['name']))
$errors[] = '"name" can not be empty';
if ( ! isset($_POST['email']) || empty($_POST['email']))
$errors[] = '"email" can not be empty';
if ( ! isset($_POST['message']) || empty($_POST['email']))
$errors[] = '"message" can not be empty';
if ( ! isset($_POST['human']) || ((int) $_POST['human'] !== 4))
$errors[] = 'are you human?';
if (count($errors) > 1)
{
foreach($errors as $error)
echo $error,"\n";
die('Aborting due to form errors');
}
$body =<<<MAIL
From: {$_POST['name']}
E-Mail: {$_POST['email']}
Message:
{$_POST['message']}
MAIL;
$headers = 'From: '.FROM;
if (mail(RECIPIENT, SUBJECT, $body, $headers))
echo 'Email sent!';
else
echo 'Failed sending email';
注意mail()需要php.ini上的一些配置
您最好使用SwiftMailer,PHPMailer或其他邮件库
答案 3 :(得分:-1)
如果您尝试从本地主机发送邮件,则必须查看How to send a mail from localhost
OR
您可以使用SwiftMailer发送邮件。它非常简单易用。
还尝试使用SMTP身份验证发送邮件,可能会发生在服务器上发送邮件需要身份验证的情况。
这可能会对你有所帮助:
require_once('swift/swift_required.php');
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: F-Sharp Media';
$to = 'f.ajibade@f-sharpmedia.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$transport = Swift_SmtpTransport::newInstance('SMTP_HOST', SMTP_PORT)
->setUsername('SMTP_USERNAME')
->setPassword('SMTP_PASSWORD')
;
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject($subject) // Message subject
->setTo(array($to => 'TO_NAME')) // Array of people to send to
->setFrom(array($email => $name)) // From:
->setBody($body , 'text/html');
if($mailer->send($message)){
header("Location:email_success.php");
}
else{
echo 'Some error occurred while sending mail';
exit;
}