PHPMail立即发送电子邮件

时间:2017-08-20 09:23:14

标签: php

虽然我只是想从数据库中提取数据,但只要页面显示数据,它就会发送电子邮件。如何阻止它立即发送电子邮件。我想要发生的是我从数据库中检索数据>将其显示在页面>编辑页面>更新数据库并发送电子邮件。这是我希望发送电子邮件的唯一点。

根据建议,这是我的整个编辑代码,

$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>';

}
}
}

以下是Submit按钮的完整代码,

<html>
<head>
<title>Ticket Assignment</title>
</head>
<body class="oneColFixCtrHdr">
<script>
</script>

<div id="mainContent">

<?php
foreach($message as $key => $val){
    // show error, warning and info messages
    if(!empty($val)){
        echo '<div style="margin:4px;background:'.$bgcol[$key].';border:2px solid grey;border-radius:8px;">';
        foreach($val as $item){
            echo '<div style="margin:8px;">'.$item.'</div>';
            }
        echo '</div>';
        }
    }
?>
    <div>
    <form action="assign_test1.php" method="post">
<fieldset>
        <legend>Select Date</legend>
        <div>Select Date from and Date to</div>
        <p></p>
        <input type="date" name="from" id="from" value="<?=$date['from']; ?>" />
        <input type="date" name="to" id="to" value="<?=$date['to']; ?>" />
        <div><input type="submit" name="submit" id="submit" value="Submit"/></div>
</fieldset>
</form>
</div>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您提供的代码并不具备所有内容,因此我将解释它应如何运作

 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
}

我希望这会对你有所帮助。

如果您还有其他需要,请告诉我。我在这里帮忙。祝你好运!

相关问题