简体   繁体   English

PHP mail()-服务器错误

[英]PHP mail() - server error

I'm trying to use a simple PHP mail() function to create user feedback from a website. 我正在尝试使用一个简单的PHP mail()函数来从网站创建用户反馈。 I'm using the 'PHP Secure E-mail' sample script from W3 as a basis. 我使用W3中的“ PHP安全电子邮件”示例脚本作为基础。 Because of the nature of the email, there's no need for added security or using some of the libraries, eg swift mailer, that have been suggested in other posts related to this topic. 由于电子邮件的性质,因此无需增加安全性或使用某些库(例如,快速邮件程序),这些在其他与该主题相关的帖子中已经提出。

When the base form is loaded and the 'send mail' button selected, I get a "server error" message and the following supporting information: 加载基本表单并选择“发送邮件”按钮后,我收到“服务器错误”消息和以下支持信息:

"The website encountered an error while retrieving http://www.examplewebsite/formsuccessful.php?forename=Joe&surname=Blogs&email=j.oe.blogs%40example.co.uk&subject=TEST+2&message=Test+Content It may be down for maintenance or configured incorrectly." “该网站在检索http://www.examplewebsite/formsuccessful.php?forename = Joe&surname = Blogs&email = j.oe.blogs%40example.co.uk&subject = TEST + 2&message = Test + Content时遇到错误。维护或配置不正确。”

The file 'formsuccessful.php' file is definitely on the server in the correct folder - unless there's another setting I'm overlooking to enable to this type of PHP scripting? 文件“ formsuccessful.php”文件肯定在服务器上的正确文件夹中-除非我忽略了启用此类型PHP脚本的其他设置?

I've included both the form and PHP code below in case there's an error in those. 我在下面提供了表单和PHP代码,以防万一其中有错误。

Would certainly appreciate some help, I've danced around the houses on this one that many times I can't see the wood from the trees any more. 我一定会很感激的,我在这个房子上跳舞,很多次我再也看不到树木上的木头了。

Cheers. 干杯。

PHP: PHP:

<?php
function spamcheck($field)
  {
  $field1=filter_var($field, FILTER_SANITIZE_EMAIL);
  if(filter_var($field1, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }

if (isset($_REQUEST['email']))
  {

  $mail = $_REQUEST['email'];
  $mailcheck = spamcheck($mail);
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
  else
    {
    $email = $_REQUEST['email'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    $name = $_REQUEST['forename']." ".$_REQUEST['surname'];
    mail("admin@examplewebsite.com", "Subject: "$subject,
    "Message: "$message, "From: "$email," ",$name );
    }
  }
else{
    //reload page with message highlighting error
    }
?>

HTML Form: HTML表单:

<form method="REQUEST" action="formsuccessful.php">
            <fieldset class="master">
            <legend class="master" style="text-align:left;"><b><u>Personal     Details:</u></b></legend>
                <ol class="master">
                    <li class="master"><label class="master"     for"forname">Forename<em class="master">*</em></label><input class="left" type="text"     name="forename" size="30" /></li>
                <li class="master"><label class="master" for"surname">Surname<em class="master">*</em></label><input type="text" name="surname" size="30" /></li>
                <li class="master"><label class="master" for"email">Your Email<em class="master">*</em></label><input type="text" name="email" size="30" /></li>
            </ol>
        <legend class="master" style="text-align: left;"><b><u>Email Contents:</u></b></legend>
            <ol class="master">
                <li class="master"><label class="master" for="subject">Subject:<em class="master">*</em></label><input type="text" name="subject" size="30" /></li>
                <li class="master"><label class="master" for="message">Content:<em class="master">*</em></label><textarea cols="50%" rows="20" onkeyup="checkContent()" name="message" id="message"></textarea></li>
                <li class="master"><input type="submit" value="Send Email" /></li></ol>
        </fieldset>
    </form>

Standing by to have the criminally obvious pointed out to me. 站着向我指出明显的犯罪事实。

You seem to have some errors here: 您似乎在这里有一些错误:

mail("admin@examplewebsite.com", "Subject: "$subject,
  "Message: "$message, "From: "$email," ",$name );

If you want to concatenate, you need to use something like: 如果要串联,则需要使用以下方法:

mail("admin@examplewebsite.com", "Subject: " . $subject,
  "Message: " . $message, "From: " . $email);

You also have one parameter too many, I'm not sure what you want to do with the last part ," ", $name but that's not valid input for the last parameter, see the manual for mail . 您也有一个参数太多,我不确定您要对最后一部分," ", $name做什么," ", $name但这不是最后一个参数的有效输入,请参阅mail手册

Your form is using the method "REQUEST" which is not correct it should be GET or POST. 您的表单使用的方法“ REQUEST”不正确,应为GET或POST。 The php-code that you're using on the other hand seems correct, except for the mail-function which @jeroen already pointed out. 另一方面,您使用的php代码似乎正确,除了@jeroen已经指出的邮件功能。

Change your form method to "post" 将您的表单方法更改为“ post”

Also, i don't think this will make a difference, but use the full URL in the form action. 另外,我不认为这会有所作为,但是在表单操作中使用完整的URL。

ie: form method="POST" action="http://www.example.com/path/to/file/formsuccessful.php" 即:form method =“ POST” action =“ http://www.example.com/path/to/file/formsuccessful.php”

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

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