我今天一直在努力(几天前刚刚开始学习PHP)。我一直在尝试从php文件,gmail等发送一个表单。这很好用。但是,在完成所有操作后,它仍然会出错。
所以电子邮件发送,但它使用户感到困惑,它使它看起来没有。
我是用tutorial做的。解释所有这一切的家伙真的投入了大量的JavaScript和CSS。我试图避免大部分内容。现在,在邮件发送后我留下了很多代码,我不明白,它打破了我的联系页面,因为一切都很好,然后当它试图完成脚本服务器出错时。
有没有办法让任何人都可以查看我当前的设置,看看在发送邮件后可以做些什么来使php文件与HTML一致?
的 HTML:
<form method="post" action="contact_form.php">
<p>
<label for="name">Name:</label>
<input name="name" type="text" class="txt" id="name" size="30" />
</p>
<p>
<label for="company">Company:</label>
<input name="company" type="text" class="txt" id="company" size="30" />
</p>
<p>
<label for="city">Email:</label>
<input type="text" name="email" id="email" class="txt" />
</p>
<p>
<label for="comments">Comments:</label>
<textarea name="comments" cols="30" rows="5" class="txtarea" id="comments"></textarea>
</p>
<p>
<label for="blank"> </label>
<input type="submit" name="btn_submit" id="btn_submit" class="btn" value="Send"/>
</p>
</form>
的 Contact_form.php:
<?php
$name = trim($_POST['name']);
$company = $_POST['company'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$site_owners_email = 'support@macnx.com'; // Replace this with your own email address
$site_owners_name = 'Landin'; // replace with your name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name";
}
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address";
}
if (strlen($comments) < 3) {
$error['comments'] = "Please leave a comment.";
}
if (!$error) {
require_once('phpMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->From = $email;
$mail->FromName = $name;
$mail->Subject = "Website Contact Form";
$mail->AddAddress($site_owners_email, $site_owners_name);
$mail->Body = $email . $company . $comments;
// GMAIL STUFF
$mail->Mailer = "smtp";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "inquiry@getgearhead.com"; // SMTP username
$mail->Password = "B******A"; // SMTP password
$mail->Send();
echo "<li class='success'> Congratulations, " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>";
} # end if no error
else {
$response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : "";
$response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : "";
$response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : "";
echo $response;
} # end if there was an error sending?>
这是$ mail-&gt; Send()之后的所有代码;那给了我麻烦。一切都发送花花公子,但然后错误。任何人都知道一种方法,我可以修改php和html文件的代码,以获得发送表单的人的成功消息,而不是服务器500错误。
谢谢!