Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHPMail立即发送电子邮件_Php - Fatal编程技术网

PHPMail立即发送电子邮件

PHPMail立即发送电子邮件,php,Php,当我试图从数据库中提取数据时,一旦页面显示数据,它就已经在发送电子邮件了。如何防止它立即发送电子邮件。我想做的是从数据库中检索数据>将数据显示到页面>编辑页面>更新数据库并发送电子邮件。这是我唯一希望发送电子邮件的地方 这是我根据建议编辑的全部代码 $update = filter_input_array(INPUT_POST, $update_args); $date = filter_input_array(INPUT_POST, $date_args); $result = NULL; $

当我试图从数据库中提取数据时,一旦页面显示数据,它就已经在发送电子邮件了。如何防止它立即发送电子邮件。我想做的是从数据库中检索数据>将数据显示到页面>编辑页面>更新数据库并发送电子邮件。这是我唯一希望发送电子邮件的地方

这是我根据建议编辑的全部代码

$update = filter_input_array(INPUT_POST, $update_args);
$date = filter_input_array(INPUT_POST, $date_args);
$result = NULL;
$colcomments = NULL;

$dsn = 'mysql:dbname='.DBname.';host='.DBhost.';port=';
try {
    $conn = new PDO($dsn, DBuser, DBpswd);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

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

    if(!isset($_SESSION['update'])) {

        $_SESSION['update'] = true;

        //retrieve values from database
        if($date['from'] !== NULL && $date['to'] !== NULL){
        // get table data
        $sql = 'SELECT `id`, `changeid`,  
        FROM `tracker` WHERE `scheduled_start_date` BETWEEN :d1 AND :d2';
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':d1', $date['from'], PDO::PARAM_STR);
        $stmt->bindParam(':d2', $date['to'], PDO::PARAM_STR);
        $stmt->execute();
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }

    }else {

        unset($_SESSION['update']);

        //put your mail function here
    function two_dim_array_to_html_table($arr){
    $ret = "<table border='1' width='auto' cellpadding='1px' cellspacing='0px' align='center'>\n";
    $ret .= "\t<tr>\n";
    foreach($arr[0] as $key => $val){
        $ret .= "\t\t<th>".$colcomments[$key]."</th>\n";
        }
    $ret .= "\t</tr>\n";
    foreach($arr as $row){
        $ret .= "\t<tr>\n";
        foreach($row as $column){
            $ret .= "\t\t<td>".$column."</td>\n";
            }
        $ret .= "\t</tr>\n";
        }
    $ret .= "<table>\n";
    return $ret;
    }

    if($result) {
        $Body = "<html>\n"
            . "<head>\n"
            . "</head>\n"
            . "<body>\n"
            . two_dim_array_to_html_table($result, $colcomments)
            . "</body>\n"
            . "</html>\n";
    //Setting up Mail

        $mail = new PHPMailer();
        if (EMAIL_USE_SMTP) {
            // Set mailer to use SMTP
            $mail->IsSMTP();
            //useful for debugging, shows full SMTP errors
            //$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
            // Enable SMTP authentication
            $mail->SMTPAuth = EMAIL_SMTP_AUTH;
            // Enable encryption, usually SSL/TLS
            if (defined(EMAIL_SMTP_ENCRYPTION)) {
                $mail->SMTPSecure = EMAIL_SMTP_ENCRYPTION;
            }
            // Specify host server
            $mail->Host = EMAIL_SMTP_HOST;
            $mail->Username = EMAIL_SMTP_USERNAME;
            $mail->Password = EMAIL_SMTP_PASSWORD;
            $mail->Port = EMAIL_SMTP_PORT;
        } else {
            $mail->IsMail();
        }
        $mail->From = EMAIL_FROM_ADDRESS;
        $mail->FromName = EMAIL_FROM_NAME;
        $mail->AddAddress('test.test@domain.COM');
        $mail->Subject = 'Daily Tasks - "'.date('d-m-Y').'"';
        $mail->WordWrap = 100;
        $mail->IsHTML(true);
        $mail->Body = $Body;
        $mail->Send();
        }


        //update database records
        if(isset($update['id']) && is_array($update['id']) && !empty($update['id'])){
        $sql = "UPDATE `tracker`
            SET `changeid` = :bv_changeid
            WHERE `id` = :bv_id ";
        if($stmt = $conn->prepare($sql)){
            $stmt->bindParam(':bv_changeid', $changeid, PDO::PARAM_INT);
            $stmt->bindParam(':bv_id', $id, PDO::PARAM_INT);
            $updateRowCount = 0;

            // update multiple rows - all of selected in form
            foreach($update['id'] as $key => $val){
                $changeid = $update['changeid'][$val];
                $id = $val;
                $stmt->execute();
                $updateRowCount += $stmt->rowCount();
                }
            if($updateRowCount > 0){
                $message['info'][] = "Updated ".$updateRowCount." row/s";
                }
            else {
                $message['warning'][] = "Tracker db not updated.";
                }
            }
        else {
            $message['error'][] = "Prepare error!!!";
            }
        }

    }


}else {

    //show the normal calender/form
    if(is_array($result)){
    echo '
    <fieldset>
        <legend>Assign</legend>
        <div>Changes will affect updated rows only.</div>
        <p></p>
        <table width=auto cellpadding=1px cellspacing=0px border=1 align=center id=assign>

            <thead>
            <tr>';      

    // column comment from DB as column header
    foreach($result[0] as $key => $val){
        echo '<th align=center>'.$colcomments[$key].'</th>';
        }
    echo '
            </tr>
            </thead>
            <tbody>';
    foreach($result as $row => $info){
    echo '<tr>';
    foreach($info as $key => $val){
    if($key=='id'){
    echo '<td title="'.$colcomments[$key].'">'.$val.'.<input type="hidden" name="'.$key.'['.$info['id'].']" value="'.$val.'" id="rowid_'.$val.'" /></td>';
         }
    else {
    echo '<td title="'.$colcomments[$key].'"><input type="text" name="'.$key.'['.$info['id'].']" value="'.$val.'" /></td>';
         }
         }
    echo '</tr>'; 
         }
    echo '
            </tbody>
            </table>


    </fieldset>';

}
}
}

您提供的代码不具备所有功能,因此我将解释它应该如何工作

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

    if(!isset($_SESSION['update'])) {

        $_SESSION['update'] = true;
        //retrieve values from database


    }else {

        unset($_SESSION['update']);
        //put your mail function here
        //update database records

    }


}else {

    //show the normal calender/form
}
我希望这对你有帮助


如果你还需要什么,请告诉我。我是来帮忙的。祝你好运

因此,您希望仅在编辑日期并单击“提交”后发送电子邮件?这就是现在实际发生的情况,我不希望它在我仍在使用日期过滤器从数据库检索数据时发送电子邮件。我想做的是,当我第一次单击submit(检索数据)时,它只会将检索到的数据加载到页面中。然后我将编辑该数据并再次发送submit以更新数据库,同时发送电子邮件。因此,第一次单击时,它将检索数据,第二次编辑时,它将使用新值更新数据库,向您显示值并将其发送?是的,这是正确的。这就是我想发生的。是的,好吧,简单。是不是只有一个提交按钮可以同时处理这两个问题?谢谢,我试过你的建议,我已经编辑了上面的代码,但是我仍然有同样的问题。仍然像我检索数据时一样发送电子邮件。在从数据库检索值后,您忘记设置$\u会话['update']。此外,您没有启动会话。在任何php代码之前添加:session_start();并添加:$_SESSION['update']=true;我在php代码上添加了这个-
session_start();如果($_会话['update']='true')。我想我不明白这个
你忘了设置$\u会话['update']
,是不是不在这部分
//获取表数据
是的,我很抱歉。现在正在编辑,但我想你会知道的。无论如何,在代码的末尾有一个额外的“}”。删除一个,记住小错误会毁掉整个代码!谢谢你,我跟踪了一切(我猜),但它仍然不起作用。这是我的
 if(isset($_POST['submit'])) {

    if(!isset($_SESSION['update'])) {

        $_SESSION['update'] = true;
        //retrieve values from database


    }else {

        unset($_SESSION['update']);
        //put your mail function here
        //update database records

    }


}else {

    //show the normal calender/form
}