简体   繁体   English

初学者PHP邮件功能问题+服务器错误

[英]Beginner PHP Mail Function Issue + Server Error

So I'm a super beginner to PHP, but I'm currently building my first website and I wanted to have a contact form on the site that a user can fill out, and then have their information be sent to an email. 因此,我是PHP的超级初学者,但我目前正在建立我的第一个网站,我想在该网站上创建一个联系表单,供用户填写,然后将其信息发送到电子邮件中。

So I wrote the PHP for this using the "mail()" function, but for some reason whenever the user hits submit, it just goes to a page with the code on it and not the page I linked it to in the echo statement. 因此,我使用“ mail()”函数为此编写了PHP,但是由于某种原因,每当用户单击“提交”时,它只会转到带有代码的页面,而不是我在echo语句中链接到的页面。

Here's my PHP: 这是我的PHP:

<?php 
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Phone: $phone \n Message: $message";
$recipient = "rowan.krishnan@tufts.edu";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
$url = 'contact.html';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'  
?>

And here's my html: 这是我的html:

<div id="emailform">
   <form action="mail.php" method="POST">
       Name: <input type="text" name="name">
       Email: <input type="text" name="email">
       Phone: <input type="text" name="phone">
       Message: <textarea name="message" rows="6" cols="25"></textarea><br />
       <input type="submit" value="Send"><input type="reset" value="Clear">
   </form>
</div>

My final problem is related to the web server/host that I use (hostgator). 我的最后一个问题与我使用的Web服务器/主机(hostgator)有关。 For some reason, whenever I upload any documents that contain PHP in them, I get a 500 Internal Server Error and I'm not really sure what's going on there. 出于某种原因,每当我上载任何包含PHP的文档时,都会收到500 Internal Server Error,并且我不确定该怎么回事。 If any of you know what I'm talking about, it would be great if you had advice. 如果你们中的任何人知道我在说什么,那么如果您有建议,那将是很好的。

Thanks so much for reading all this and helping out. 非常感谢您阅读所有这些内容并提供帮助。

You are missing a ; 你想念一个; at the end of your echo which is most likely causing the white page 回声结束时很可能导致白页

 echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
                                                           ^

try checking your errorlogs to see what errors you have been getting and try turning on error messages 尝试检查您的错误日志,以查看您遇到的错误并尝试打开错误消息

also the message needs to have \\r\\n for each line 邮件的每一行也必须有\\ r \\ n

$formcontent="From: $name \r\n Phone: $phone \r\n Message: $message";

You cannot run php locally, you need to first be running a web server that has php installed. 您无法在本地运行php,首先需要运行已安装php的网络服务器。 Try xamp as it is really easy to install and get going and allows you to run your php locally. 尝试使用xamp,因为它确实很容易安装和运行,并允许您在本地运行php。 http://www.apachefriends.org/en/xampp.html http://www.apachefriends.org/en/xampp.html

After generating your variables, a simple php email script will look like this.. (which you seem to have correct) 生成变量后,一个简单的PHP电子邮件脚本将如下所示:(您似乎正确)

//Generate the email
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: '. $senders_email;

mail($to, $subject, $message, $headers);

As Kai Qing mentioned, use header('location: contact.html'); 正如Kai Qing所述,请使用header('location: contact.html'); to redirect the page. 重定向页面。 echo simply prints to screen whatever you send it. 无论发送什么内容,echo都只会打印到屏幕上。 A better approach is to use a callback function in your html document. 更好的方法是在HTML文档中使用回调函数。 So you would send the form using a javascript funciton for example, once the response is recieved (your email sent) a callback function would then do what you like, such as show a different page or print a confirmation message. 因此,例如,您将使用javascript函数发送表单,一旦收到响应(发送您的电子邮件),则回调函数将执行您喜欢的操作,例如显示不同的页面或打印确认消息。

A jQuery approach would look something like this jQuery方法看起来像这样

//send form to order.php for further processing
var formData = $('#promo-form').serialize();
$.post('order.php',formData,formSent);
//formSent is the callback funciton

Your server 500 errors may be due to the permissions set on your php files. 您的服务器500错误可能是由于在php文件上设置的权限所致。 In your ftp client, check the permissions of the php files are set to 0644. Further information about this error can be found at: 在您的ftp客户端中,检查php文件的权限是否设置为0644。有关此错误的更多信息,请参见:

Hope this helps.. 希望这可以帮助..

Give this a try: 试试看:

<?php
//
///////////////////////////////////////////////////////////////////////
//  This would be your mail.php file
///////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////
//  Lets Just Check Basic PHP Installation Is Working
///////////////////////////////////////////////////////////////////////
//
if (!function_exists('mail')) {
    die('mail() is not available');
}
//
//
//
///////////////////////////////////////////////////////////////////////
//  Validate Required Passed Fields 
///////////////////////////////////////////////////////////////////////
//
if ( isset($_POST['name']) && $_POST['name'] !='' && isset($_POST['email']) && $_POST['email'] !='' && isset($_POST['phone']) && $_POST['phone'] !='' && isset($_POST['message']) && $_POST['message'] !='' ) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
}
else
{
    die('Sadly you did not complete all of the fields on our contact form');
}
//
//
///////////////////////////////////////////////////////////////////////
//  Validate The Email... This Is Just An Example
///////////////////////////////////////////////////////////////////////
//
if (preg_match('/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+@([-0-9A-Z]+\.)+([0-9A-Z]){2,4}$/i', trim($email))) {
    die('Email Address Invalid. Please Check Your Email.');
}
//
//
///////////////////////////////////////////////////////////////////////
//  Build Your Email Message
///////////////////////////////////////////////////////////////////////
//
$EmailMessage .= "Name: " . $name . " \n\n";
$EmailMessage .= "Email: " . $email . " \n\n";
$EmailMessage .= "Phone: " . $phone . " \n\n";
$EmailMessage .= "Message: " . $message . " \n\n";
//
//
///////////////////////////////////////////////////////////////////////
//  Build Email Header
///////////////////////////////////////////////////////////////////////
//
$EmailHeader .= "From: " . $name . " <" . $email . "> \n";
$EmailHeader .= "Reply-To: " . $name . " <" . $email . "> \n";
$EmailHeader .= "Return-Path: " . $name . " <" . $email . "> \n"; // running a windows server??
//
//
///////////////////////////////////////////////////////////////////////
//  Send The Email & Confirm It Processed
///////////////////////////////////////////////////////////////////////
//
if (mail('Rowan Krishnan <rowan.krishnan@tufts.edu>', 'Contact Form', $EmailMessage, $EmailHeader)) {
    //
    // it appears to have worked, so redirect somewhere else
    //
    header("Location: contact.php");
    die('Contact Form Redirect');
} else {
    echo('Contact Form Failed');
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM