Jump to content

PHPMail sending email right away


cainam29

Recommended Posts

While I'm just trying to pull up data from the db, as soon as the page shows up with the data it is already sending an email. How can I prevent it from sending the email right away. What I want to happen is I retrieved data from the db > shows it to a page > edit the page > update the db and send an email. This is the only point where I want an email to be sent out. I am just using a single Submit button to First retrieved the data from db (it is already sending an email here which I don't want) and Second to update the db with new values and send an email (it is here that I want it to send an email). 

 

Here is the code that I am using,

$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($date['from'] !== NULL && $date['to'] !== NULL){

// get table data
if(isset($_POST['submit'])) {
if(!isset($_SESSION['update'])) {

$sql = 'SELECT `id`, `changeid`, `taskid`, `summary`, `type`,
`reviewed_approved_by`, `scheduled_start_date`, `implemented_by`
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);
// get column comments
$sql = "select t.column_name, t.column_comment
from information_schema.columns t
where t.table_name = 'tracker'";
$stmt = $conn->prepare($sql);
$stmt->execute();
$colcomments = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);

}else {

unset($_SESSION['update']);

function two_dim_array_to_html_table($arr, $colcomments){
$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();

}

if(isset($update['id']) && is_array($update['id']) && !empty($update['id'])){
$sql = "UPDATE `tracker`
SET `changeid` = :bv_changeid
,`taskid` = :bv_taskid
,`summary` = :bv_summary
,`type` = :bv_type
,`reviewed_approved_by` = :bv_reviewed_approved_by
,`scheduled_start_date` = :bv_scheduled_start_date
,`implemented_by` = :bv_implemented_by
WHERE `id` = :bv_id ";
if($stmt = $conn->prepare($sql)){
$stmt->bindParam(':bv_changeid', $changeid, PDO::PARAM_INT);
$stmt->bindParam(':bv_taskid', $taskid, PDO::PARAM_INT);
$stmt->bindParam(':bv_summary', $summary, PDO::PARAM_STR);
$stmt->bindParam(':bv_type', $type, PDO::PARAM_STR);
$stmt->bindParam(':bv_reviewed_approved_by', $reviewed_approved_by, PDO::PARAM_STR);
$stmt->bindParam(':bv_scheduled_start_date', $scheduled_start_date, PDO::PARAM_STR);
$stmt->bindParam(':bv_implemented_by', $implemented_by, PDO::PARAM_STR);
$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];
$taskid = $update['taskid'][$val];
$summary = $update['summary'][$val];
$type = $update['type'][$val];
$reviewed_approved_by = $update['reviewed_approved_by'][$val];
$scheduled_start_date = $update['scheduled_start_date'][$val];
$implemented_by = $update['implemented_by'][$val];
$id = $val;
$stmt->execute();
$updateRowCount += $stmt->rowCount();
}
if($updateRowCount > 0){
$message['info'][] = "Updated ".$updateRowCount." row/s";
}
else {
$message['warning'][] = "CRQ Tracker db not updated.";
}
}
else {
$message['error'][] = "Prepare error!!!";
}
} 
}

}else {

if(is_array($result)){
echo '
<fieldset>
<legend>Assign Ticket</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>';
} 
}
}
}
}
catch(PDOException $e){
$message['error'][] = $e->getMessage();
}

This is the code for the Submit button,

    <div>
    <form action="assign_test1.php" method="post">


<p></p>  
<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" value="Submit" id="submit"/></div>
</fieldset>
    </form>
    </div>

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.