本文介绍了php mail()函数导致找不到404文件页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有html表单的简单php页面。它的设置允许您填写表格并自动向我的电子邮件发送电子邮件。没有发生这种情况,我没有收到任何电子邮件,页面重定向到找不到文件(404错误)。



这里是PHP

 <?php 

if(isset($ _ POST ['submit'])){

//名称
$名称= $ _POST ['名称'];
$ Name = mysqli_real_escape_string($ Name);

//电子邮件
$电子邮件= $ _POST [‘电子邮件’];
$ Email = mysqli_real_escape_string($ Email);

//主题
$ Subject = $ _POST [’Subject’];
$ Subject = mysqli_real_escape_string($ Subject);

//消息
$ Message = $ _POST [’Message’];
$ Message = mysqli_real_escape_string($ Message);

$ email_subject = $ Subject。 ’来自:’。 $ Email;
$ email_from = $ Email;
$ email_body = $ Message;

$ webEmail = [email protected];
$ headers =发件人:$ email_from \r\n;
$ headers。=回复:$ Email \r\n;
$ headers。=‘X-Mailer:PHP /’。 phpversion();
$ headers。=‘MIME版本:1.0’。 \r\n;
$ headers。=’内容类型:text / html; charset = iso-8859-1’。 \r\n;



邮件($ webEmail,$ email_subject,$ email_body,$ headers);
}
?>

以下是html表单

 < form action = action_page.php name = myform> 
< input onfocus = this.value =’’; type = text name =名称 value =名称 id =名称>
< input onfocus = this.value =’’; type = text name =电子邮件 value =电子邮件 id =电子邮件>
< input onfocus = this.value =’’; type = text name = Subject value = Subject id = subject>
< textarea onfocus = this.value =’’;; cols = 50 rows = 4 placeholder = Message name = Message id = message>< / textarea>
< input type = submit value = submit id = submit>

< / form>

有什么想法吗?

解决方案

这里有一些错误。



让我们从顶部开始,并从您的条件语句开始(然后向下进行操作)。

  if(isset($ _ POST ['submit'])){...} 

您的提交按钮<输入类型= submit value =提交 id = submit> 没有名称属性。




  • 您可能依赖于 id,将不起作用,它需要 name属性。



对其进行修改,使其读为:

 <输入类型= submit name = submit value = submit id = submit> 




  • 由于未命名,因此该条件语句中的任何内容均不会执行。



然后,您正在使用从理论上讲,它要求将连接传递给该函数。



即:

  $ Name = mysqli_real_escape_string($ connection,$ Name); 

其余操作相同;



但是,尚不清楚您是否确实在与数据库进行交互,因此,如果您不与数据库进行交互,请删除所有您的POST数组中的 mysqli_real_escape_string 中的一个。



即:

  $ Name = $ _POST ['Name']; 

并为其他人做同样的事情。



您还缺少POST作为表单中的方法:

 < form action = action_page.php名称= myform> 

,省略时默认为GET。因此,将其更改为:

 < form action = action_page.php name = myform method = post > 

要检查是否已发送邮件,请更改

  mail($ webEmail,$ email_subject,$ email_body,$ headers); 

  if(mail($ webEmail,$ email_subject,$ email_body,$ headers)){
echo Sent;
}

如果看到已发送,则说明邮件已经完成。如果没有收到邮件,请检查您的垃圾邮件。或者,您的服务器可能被列入黑名单。



请确保您确实可以使用 mail()进行访问。 / p>

您还应确保使用条件 empty()填充了所有输入。



即:

  if(!empty($ _ POST ['Name'])) {
// $ Name = mysqli_real_escape_string($ Name);
$ Name = $ _POST [’名称’];
}

添加到文件顶部,这将有助于查找错误。

 <?php 
error_reporting(E_ALL);
ini_set(‘display_errors’,1);

//代码的其余部分

侧注:错误报告仅应在登台中进行,而不是在生产过程中进行。






脚注:



我不知道这些用于 onfocus = this.value =''; ,如果某个地方发生了某些JS / Ajax,则需要检查这些内容。




  • 如果不使用它们任何东西,您都可以删除。



您还应该尝试删除 value = Name ,其他情况也一样,以防万一。



侧注:



如果仍然出现404错误,请尝试将PHP放在顶部,将HTML表单放在下面,并在同一文件中放入 .php 扩展名,并执行以下操作:

 < form action = name = myform method =发布 

使用 action = 作为操作






附加脚注:



我在您放置在另一个答案中的注释中注意到:

首先,请确保您



另外,还应该包括完整的 http:// 调用和 exit;

  header( Location:http://www.lizbethmcgee .com / pages / Contact.php); 
出口;

避免进一步执行可能存在于其下方的代码,如果其中包含任何代码。






AdamT在评论中指出:



发送a使用php mail 命令,无法保证邮件会到达您发送至的收件箱。如前所述,那里有很多垃圾邮件发送者,但不幸的是,垃圾邮件过滤的程度很高。如果您确实希望有更好的机会来发送邮件,则应联系GoDaddy,并询问他们如何为您的帐户设置SMTP电子邮件。


I have a simple php page with an html form. Its set up to let you fill out the form and automatically send an email to my email. Instead of this happening, I don't get any email and the page redirects to a "File not found (404 error)".

Here's the PHP

<?php

if (isset($_POST['submit'])){

    //Name
    $Name = $_POST['Name'];
    $Name = mysqli_real_escape_string($Name);

    //Email
    $Email = $_POST['Email'];
    $Email = mysqli_real_escape_string($Email);

    //Subject
    $Subject = $_POST['Subject'];
    $Subject = mysqli_real_escape_string($Subject);

    //Message
    $Message = $_POST['Message'];
    $Message = mysqli_real_escape_string($Message);

    $email_subject = $Subject . ' from: ' . $Email;
    $email_from = $Email;
    $email_body = $Message;

    $webEmail = "[email protected]";
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $Email \r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion();
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";



    mail($webEmail,$email_subject,$email_body,$headers);
}
?>

Here's the html form

<form action="action_page.php" name="myform">
            <input onfocus="this.value='';" type="text" name="Name" value="Name" id="name">
            <input onfocus="this.value='';" type="text" name="Email" value="Email" id="email">
            <input onfocus="this.value='';" type="text" name="Subject" value="Subject" id="subject">
            <textarea onfocus="this.value='';" cols="50" rows="4" placeholder="Message" name="Message" id="message"></textarea>
            <input type="submit" value="submit" id="submit">

        </form>

Any ideas?

解决方案

There are a few things wrong here.

Let's start from the top and with your conditional statement (and work our way down).

if (isset($_POST['submit'])){...}

Your submit button <input type="submit" value="submit" id="submit"> does not have a name attribute for it.

  • You may be relying on an "id" alone, which won't work, it needs the "name" attribute.

Modify it so that it reads as:

<input type="submit" name="submit" value="submit" id="submit">

  • Since it's not named, anything inside that conditional statement will not execute.

Then you're using mysqli_real_escape_string() which theoretically requires the connection be passed to that function.

I.e.:

$Name = mysqli_real_escape_string($connection,$Name);

and do the same for the rest; assuming you are interacting with a database.

However, it's unclear whether you are indeed interacting with your database or not, so if you're not interacting with it, remove all of those mysqli_real_escape_string from your POST arrays.

I.e.:

$Name = $_POST['Name'];

and do the same for the others.

You're also missing POST as the method in your form:

<form action="action_page.php" name="myform">

which defaults to GET when omitted. Therefore, change it to:

<form action="action_page.php" name="myform" method="post">

To then check if mail has been sent, change

mail($webEmail,$email_subject,$email_body,$headers);

to

if(mail($webEmail,$email_subject,$email_body,$headers)){
echo "Sent";
}

If you see "Sent", then mail would have done its job. If no mail is received, check your Spam. Or, your server could be blacklisted.

Make sure that you do have access to using mail().

You should also make sure that all inputs have been filled using a conditional empty().

I.e.:

if(!empty($_POST['Name'])){
// $Name = mysqli_real_escape_string($Name);
   $Name = $_POST['Name'];
   }

Add error reporting to the top of your file(s) which will help find errors.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.


Footnotes:

I don't know what these are used for onfocus="this.value='';" so, if you've got some JS/Ajax happening somewhere, you'll need to check those.

  • If they're not used for anything, you can remove those.

You should also try removing value="Name" and for the others as well, just in case.

Sidenote:

If you're still getting a 404 error, then try putting your PHP on top and your HTML form below that, and inside the same file with a .php extension, and doing:

<form action="" name="myform" method="post">

using action="" as the action to take place on the same page.


Additional footnote:

I noticed in a comment you placed in another answer:

First, make sure you're not outputting before header.

Plus, you should also include a full http:// call and exit;:

header("Location: http://www.lizbethmcgee.com/pages/Contact.php");
exit;

to avoid further execution of code that may be present below that, should there contain any.


As AdamT stated in a comment:

Sending a mail with the php mail command, there is no guarantee that the mail will arrive to the inbox you send it to. As mentioned, there are lots of spammers out there, and unfortunately, a heavy degree of spam filtering. If you really want to have a better chance at getting the mail delivered, you should contact GoDaddy and ask them how you can set up SMTP emailing for your account.

这篇关于php mail()函数导致找不到404文件页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:53