简体   繁体   English

当用户输入电子邮件包含在其他PHP文件中时,PHP mail()将不会发送

[英]PHP mail() won't send when included in other PHP file with user input email

I am generating a page of information from a database. 我正在从数据库中生成信息页面。 I need to send an email with the page content as a reminder. 我需要发送一封包含页面内容的电子邮件作为提醒。 (example here takes input as message) It takes in an email address and is supposed to send the details to that address. (这里的示例以输入作为消息)。它接收一个电子邮件地址,并应将详细信息发送到该地址。

<div class="container">
        <?php
            $name = $_GET['info'];

            if(isset($name)){

                $info = explode('|', $name);

                /*****************************************************
                        open conection to the mySQL database
                ******************************************************/
                                         ....
                /*****************************************************
                        Populate the page 
                ******************************************************/
                $sql="Select information from table";

                $result = mysqli_query($con,$sql);


                while($row = mysqli_fetch_array($result))
                {
                    /*Title*/
                    echo '<h1>'.$row['post_title'].'</h1><hr>';

                    /*content*/
                    echo '<h2>Details: </h2><br>'.$row['post_content'].'<br>';

                    $content = $row['post_title'];

                    /*Reminder*/
                    echo '<div data-role="collapsible">
                        <h1>Send a reminder</h1>';
                        include("includes/email_reminder.php");             
                    echo'</div>';
                }


                /*****************************************************
                        Close connection
                ******************************************************/
                mysqli_close($con);

            } else {

                echo'Nothing selected. Go back <br> 
                <a href="#" data-rel="back"> <img src="img/icon/Back.png" style="height: 3em" > </a>';

            }
        ?>

    </div>

That creates a form at the bottom of the page to take in the email that needs that needs a reminder. 这将在页面底部创建一个表单,以接收需要提醒的电子邮件。 This is email_reminder.php: 这是email_reminder.php:

<?php
function spamcheck($field)
{
    // Sanitize e-mail address
    $field=filter_var($field, FILTER_SANITIZE_EMAIL);
    // Validate e-mail address
    if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}
?>

<?php
    // display form if user has not clicked submit
    if (!isset($_POST["submit"]))
    {
?>    

<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">

Your Email: <input type="text" name="to"><br>

Subject: <input type="text" name="subject"><br>

Message: <textarea rows="10" cols="40" name="message"></textarea><br>

<input type="submit" name="submit" value="Submit Feedback">

</form>

<?php 
    }
    else    // the user has submitted the form

    {
        // Check if the "from" input field is filled out
        if (isset($_POST["to"]))
        {
            // Check if "from" email address is valid
            $receivecheck = spamcheck($_POST["to"]);

            if ($receivecheck==FALSE)
            {
                echo "Invalid input";
            }

            else
            {
                $to = $_POST["to"]; //receiver

                $subject = $_POST["subject"];

                $message = $_POST["message"];

                // message lines should not exceed 70 characters (PHP rule), so wrap it
                $message = wordwrap($message, 70);

                // send mail
                mail("$to",$subject,$message,"From: myaddress@mail.com\n");

                echo "reminder has been sent";
            }
        }
    }
?>    

I have used that form in isolation (just opened email_reminder.php on its own) and it sent emails correctly from whichever email address I used. 我已经孤立地使用了该表单(只是自己打开了email_reminder.php),它从我使用的那个电子邮件地址正确发送了电子邮件。 It just doesn't send when included in the other script. 当包含在其他脚本中时,它只是不发送。

include(emai_reminder.php); needs single quotes surrounding the file name (and email correctly spelled: include('email_reminder.php'); 需要用单引号引起来的文件名(以及正确拼写的电子邮件: include('email_reminder.php');

But, it looks like you need more help than just this. 但是,您似乎需要的不仅仅是这些。 For example, there is no field FROM in your form, although you reference $_POST["from"] . 例如,尽管您引用$_POST["from"] ,但表单中没有FROM字段。 You're running validation against that variable, which doesn't exist, which fails validation, which prevents the else if block from running, which prevents mail() from ever being called. 您正在针对该变量运行验证,该变量不存在,验证失败,从而阻止了else if块运行,从而阻止了mail()调用。

Several reasons why mailing to different user-defined addresses may not work: 邮寄到不同的用户定义地址可能不起作用的几个原因:

1.) Check the result from the mail() call is TRUE , just to make sure the underlying sendmail (if you are on Linux) did not return an error. 1.)检查mail()调用的结果是否为TRUE ,只是要确保基础sendmail (如果在Linux上)没有返回错误。

2.) Add additional headers to prevent problems when delivering mails to foreign SMTP hosts, an example may be found on PHP doc for mail() 2.)添加其他标头,以防止在将邮件传递到外部SMTP主机时出现问题,可能在PHP doc上找到mail()的示例

3.) In some cases (ie when using SMTP on Windows) using the PEAR Mail package may solve your problem. 3.)在某些情况下(例如,在Windows上使用SMTP时),使用PEAR Mail软件包可能会解决您的问题。 It also supports ASMTP and error trapping is much easier compared to the mail() function. 它还支持ASMTP,与mail()函数相比,错误捕获要容易得多。

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

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