【问题标题】:Send email and write to database not working [duplicate]发送电子邮件并写入数据库不起作用[重复]
【发布时间】:2016-08-16 01:30:49
【问题描述】:

我有一个表单,允许非用户向用户发送消息,脚本获取表单内容,将它们存储在我的messages 表中,然后最终将内容通过电子邮件发送到用户的电子邮件地址。

我无法弄清楚为什么我的表单无法提交并在未正确完成时返回我设置的错误消息。没有发送电子邮件,也没有在数据库表中创建任何条目。

我的表单代码;

<form class="signup-form" action="sendmail.php" id="email_submit" method="POST">
<fieldset>
<input type="text" name="msg_touserid" id="msg_touserid" value="<?php echo htmlentities($_GET["uid"], ENT_QUOTES, 'UTF-8'); ?>" style="display: none;">
<input type="text" name="msg_tousername" id="msg_tousername" value="<?php echo htmlentities($_GET["username"], ENT_QUOTES, 'UTF-8'); ?>" style="display: none;">
<input type="text" placeholder="Your name..." name="msg_fromname" id="msg_fromname" value=""><br />
<input type="text" placeholder="Your mobile number..." name="msg_mobile" id="msg_mobile" value=""><br />
<input type="email" name="msg_toemail" id="msg_toemail" value="XXX@gmail.com" style="display: none;">

<input type="email" placeholder="Your email..." name="msg_fromemail" id="msg_fromemail" value=""><br />
<input type="text" placeholder="Message subject..." name="msg_subject" id="msg_subject" value=""><br />
<textarea name="msg_messagebody" id="msg_messagebody" style="height: 282px; background-image: none; background-position: 0px 50%; background-repeat: repeat;"></textarea>
</fieldset>
<input type="submit" class="btn-colored submit-send-email" value="Send email" />
</form>

我的 sendmail.php 代码;

$connection = mysql_connect('localhost', 'XXX', 'XXX');
    if (!$connection){
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db( 'DB' );
    if (isset($_POST['email_submit'])){
        $msg_touserid = $_POST['msg_touserid'];
        $msg_tousername = $_POST['msg_tousername'];
        $msg_fromname = $_POST['msg_fromname'];
        $msg_mobile = $_POST['msg_mobile'];
        $msg_toemail = $_POST['msg_toemail'];
        $msg_fromemail = $_POST['msg_fromemail'];
        $msg_subject = $_POST['msg_subject'];
        $msg_messagebody = $_POST['msg_messagebody'];

        $sql = "INSERT INTO messages (msg_touserid, msg_tousername, msg_fromname, msg_mobile, msg_toemail, msg_fromemail, msg_subject, msg_messagebody) 
                VALUES ('$msg_touserid', $msg_tousername', '$msg_fromname', '$msg_mobile', '$msg_toemail', '$msg_fromemail', '$msg_subject', '$msg_messagebody')";
        if (!mysql_query($sql,$connection)){
            die('Error: ' . mysql_error());
        }

        $emailID = "$msg_toemail";
        $subject = "Enquiry from. $msg_fromname . through our website";
$body = <<<EOD

        <table cellspacing="0" cellpadding="1" border="1">
            <tbody>
                <tr>
                    <td style="padding: 5px 10px;" width="150">Name: </td>
                    <td style="padding: 5px 10px;">$msg_fromname</td>
                </tr>
                <tr>
                    <td style="padding: 5px 10px;" width="150">Mobile: </td>
                    <td style="padding: 5px 10px;">$msg_mobile</td>
                </tr>
                <tr>
                    <td style="padding: 5px 10px;" width="150">Email: </td>
                    <td style="padding: 5px 10px;">$msg_fromemail</td>
                </tr>
                <tr>
                    <td style="padding: 5px 10px;" width="150">Message: </td>
                    <td style="padding: 5px 10px;">$msg_messagebody</td>
                </tr>
            </tbody>
        </table>

EOD;

        $headers = "From: admin@domain.com\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
        $headers .= "X-Priority: 1\r\n";
        $headers .= "X-MSMail-Priority: High\n";
        $headers .= "X-Mailer: PHP". phpversion() ."\r\n";

        mail($emailID, $subject, $body, $headers );
        echo "<h4>Thank you for your message.</h4>";

    } else {
        echo("Oops... Please check you have completed the form correctly.");
    };

我的数据库结构;

id                      int(10) (AI)
msg_touserid            int(10) 
msg_tousername          varchar(100)
msg_fromname            varchar(100)
msg_mobile              varchar(20)
msg_toemail             varchar(100)
msg_fromemail           varchar(100)    
msg_subject             varchar(200)
msg_messagebody         varchar(1000)   
msg_sent                timestamp

【问题讨论】:

标签: php mysql forms email


【解决方案1】:

提交按钮没有name 属性。

<input type="submit" class="btn-colored submit-send-email" value="Send email" />

所以,条件

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

不满意。

解决方案:

添加name 提交按钮。

更正的代码:

<input type="submit" class="btn-colored submit-send-email" value="Send email" name="email_submit" />

【讨论】:

  • 谢谢。这似乎已经取得了进展,但后来我意识到我错过了数据库的选择,所以我添加了它,但现在出现语法错误。我已经更新了上面的 sendmail.php 代码以反映更改。
【解决方案2】:

它没有提交/发送电子邮件,因为它从来没有机会这样做。

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

永远不会满足email_submit 未在您的表单中定义。您将其设置为表单 ID &lt;form id="email_submit"&gt;,但 ID 不会像 name 那样传递给 $_POST

在您的表单中为您的提交按钮命名为email_submit

<input name="email_submit" type="submit" class="btn-colored submit-send-email" value="Send email">

现在初始条件将满足。

【讨论】:

    猜你喜欢
    • 2015-02-23
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 2012-08-01
    • 2018-10-30
    • 2017-10-21
    相关资源
    最近更新 更多