您好我是stackoverflow的新手,请原谅我,如果我做错了什么。我对PHP知之甚少。所以我有这个联系表单,我想在用户提交时发送到我的电子邮件。我一直在墙上撞了一会儿,两个文件都上传到服务器,并且在同一个目录中。表单转到成功页面,但我没有收到电子邮件。请帮帮我!
如果有帮助,表单的链接也是http://xtcracingteam.com/apply.html。
这是HTML:
<form method="post" action="http://xtcracingteam.com/send_form_email.php">
<label class="first-name">First Name</label>
<input name="firstName" class="firstName" type="text" placeholder="Joe" required />
<label class="last-name">Last Name</label>
<input name="lastName" class="lastName" type="text" placeholder="Swanson" required />
<label class="Race-number">Race Number</label>
<input name="raceNumber" class="raceNumber" type="text" placeholder="999" required />
<label class="class">Class</label>
<input name="bikeClass" class="bikeClass" type="text" placeholder="Mx 450 Production A" required />
<label class="age">Age</label>
<input name="personAge" class="personAge" type="text" placeholder="21" required />
<label class="home-town">Home Town</label>
<input name="homeTown" class="homeTown" type="text" placeholder="Washougal Washington" required />
<label class="Current-sponsors">Current Sponsors</label>
<textarea name="sponsors" placeholder="Gopro, Redbull, Scott..." class="currentSponsors" required></textarea>
<label class="recent-achievements">Recent Achievements</label>
<textarea name="recentAchievments" placeholder="I won this and that..." class="recentAchievments" required></textarea>
<label class="why">Why should we pick you to represent our brand?</label>
<textarea name="theWhy" placeholder="(hint: Dont say Cuz im the fastest)" class="theWhy" required></textarea>
<label class="email">Email</label>
<input name="email" class="email" type="email" placeholder="Example@emails.com" required />
<input class="submit" type="submit" value="Submit!" />
</form>
这是PHP代码:
<?php
ini_set(‘display_errors’, ‘On’);
error_reporting(E_ALL);
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "xtc.racing.team@gmail.com";
$email_subject = "New Application";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['firstName']) ||
!isset($_POST['lastName']) ||
!isset($_POST['raceNumber']) ||
!isset($_POST['bikeClass']) ||
!isset($_POST['personAge']) ||
!isset($_POST['homeTown']) ||
!isset($_POST['sponsors']) ||
!isset($_POST['recentAchievments']) ||
!isset($_POST['theWhy']) ||
!isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$raceNumber = $_POST['raceNumber']; // required
$bikeClass = $_POST['bikeClass']; // required
$personAge = $_POST['personAge']; // required
$homeTown = $_POST['homeTown']; // required
$sponsors = $_POST['sponsors']; // required
$recentAchievments = $_POST['recentAchievments']; // required
$theWhy = $_POST['theWhy']; // required
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($firstName)."\n";
$email_message .= "Last Name: ".clean_string($lastName)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "race number: ".clean_string($raceNumber)."\n";
$email_message .= "bikeClass: ".clean_string($bikeClass)."\n";
$email_message .= "personAge: ".clean_string($personAge)."\n";
$email_message .= "homeTown: ".clean_string($homeTown)."\n";
$email_message .= "sponsors: ".clean_string($sponsors)."\n";
$email_message .= "recentAchievments: ".clean_string($recentAchievments)."\n";
$email_message .= "theWhy: ".clean_string($theWhy)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
return to <a href="http://xtcracingteam.com">XTCRacingTeam</a>
<?php
}
?>
答案 0 :(得分:0)
在调试代码时,请按小部分逐步调试。
即。尝试:
var_dump($_POST);
看看你从表单中得到了什么。
尝试使用mail()函数发送电子邮件并输入硬编码数据而不是变量。
另外,试试
ini_set('display_errors', '1');
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
mail() returns a bool, even if the mail server rejects the sending.
if(mail()) { echo 'succes'; } else { echo 'no success'; }