用户收到多个电子邮件通知

时间:2016-12-16 16:56:51

标签: php mysql

我有一个代码可以在更新记录后更新表并发送电子邮件通知。 这是一个休假审批系统,注册会员登录申请休假。当您尝试批准或拒绝请假申请时,此代码会向两个用户(员工)和(员工经理)发送电子邮件。

当有三个人申请休假并且您尝试批准一个用户时,用户会收到3次电子邮件通知。

<?php
error_reporting(E_ALL);
$pdo = new PDO("sqlsrv:Server=$serverName;Database=$database", $user, $pass);

?>


<?php
require_once './config.php';
include './header.php';

?>

<div class="container">
  <div class="row">
    <div class="col-lg-12">
      <?php
function mssql_escape($str) {
    return str_replace("'", "''", $str);
}
filter_var($_SESSION['kt_login_user'], FILTER_VALIDATE_EMAIL);
$email =  $_SESSION['kt_login_user'];
$empID = $row['No_'];
$year = date("Y");
$sql = 'SELECT
dbo.[Leave Request].[timestamp],
dbo.[Leave Request].leave_type,
dbo.[Leave Request].date_applied,
dbo.[Leave Request].start_date AS start,
dbo.[Leave Request].end_date AS resume,
dbo.[Leave Request].supervisor,
dbo.[Leave Request].supAprrove_date,
dbo.[Leave Request].leave_reason,
dbo.[Leave Request].reject_reason,
dbo.[Leave Request].[year],
dbo.[Leave Request].partner,
dbo.[Leave Request].partnerAprrove_date,
dbo.[Leave Request].id,
dbo.[Leave Request].phone,
dbo.[Leave Request].total_left,
dbo.[Leave Request].supAprrove,
dbo.[Leave Request].partnerApprove,
dbo.[Leave Request].leave_days,
dbo.[Cost Center].Description AS cost,
dbo.[First Category Level].[First Level] AS dept,
dbo.[Leave Request].email,
dbo.[Employee].[First Name],
dbo.[Employee].[Middle Name],
dbo.[Employee].[Last Name],
dbo.[Employee].No_,
dbo.[Employee].[Job Titles],
dbo.[Employee].[Employment Date],
dbo.[Employee].[Birth Date],
dbo.[Leave].Description AS leave,
dbo.[Leave].[Maximum Days],
dbo.[Supervisor List].[Supervisor Name],
dbo.[Supervisor List].[Supervisor Email],
dbo.[Partner List].[Employee Name],
dbo.[Partner List].email AS partMail

FROM
dbo.[Leave Request]
LEFT JOIN dbo.[Cost Center] ON dbo.[Leave Request].cost_centre = dbo.[Cost Center].Code
LEFT JOIN dbo.[First Category Level] ON dbo.[Leave Request].dept = dbo.[First Category Level].Code
LEFT JOIN dbo.[Employee] ON dbo.[Leave Request].email = dbo.[Employee].[Company E-Mail]
LEFT JOIN dbo.[Leave] ON dbo.[Leave Request].leave_type = dbo.[Leave].Code
LEFT JOIN dbo.[Supervisor List] ON dbo.[Leave Request].supervisor = dbo.[Supervisor List].[Supervisor ID]
left JOIN dbo.[Partner List] ON dbo.[Leave Request].partner = dbo.[Partner List].[Employee No]

WHERE
dbo.[Leave Request].supAprrove = \'pending\'
AND dbo.[Leave Request].supervisor=\''.mssql_escape($email).'\'';
$leave = sqlsrv_query( $conn, $sql );
$id = $data['id'];
?>

     <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                 <th>Start Date</th>
                <th>Resumption Date</th>
              <th>Leave Days Requested</th>
              <th>Leave Days Left</th>
                <th>Leave Days Entitled</th>
                <th>Type of Leave</th>
                <th>Department</th>
                <th>Cost Centre</th>
                <th>Year</th>

                <th>Status</th>
            </tr>
        </thead>
        <tfoot>
        </tfoot>
        <tbody>
           <?php while ($data = sqlsrv_fetch_array($leave)) {?>
     <tr>
                <td><a href="sup_approval.php?id=<?php echo $data['id'];?>">.</a>  <?php echo $data['First Name'].' '.$data['Middle Name'];  ?></td>
                <td><?php echo date_format($data['start'],'d-m-Y');  ?></td>
                <td><?php echo date_format($data['resume'],'d-m-Y');  ?></td>
                <td><?php echo intval($data['leave_days']);  ?></td>
                <td><?php echo $data['total_left'];  ?></td>
                <td><?php echo $data['Maximum Days'];  ?></td>
                <td><?php echo $data['leave'];  ?></td>
                <td><?php echo $data['dept'];  ?></td>
                <td><?php echo $data['cost'];  ?></td>
                <td><?php echo $data['year'];  ?></td>

                <td>
  <?php 
  require_once './config.php';
if (isset($_POST["sub"])) {
  require_once "phpmailer/class.phpmailer.php";
//Connect to MySQL and create our PDO object.
$pdo = new PDO("sqlsrv:Server=$serverName;Database=$database", $user, $pass);

//Our UPDATE SQL statement.
if ($_POST['supAprrove']==='approved'){$usql = 'UPDATE dbo.[Leave Request] SET dbo.[Leave Request].supAprrove_date = :adate,dbo.[Leave Request].supAprrove=:approve,dbo.[Leave Request].partnerApprove = \'pending\' WHERE id = :id';}
elseif ($_POST['supAprrove']==='rejected') {$usql = 'UPDATE dbo.[Leave Request] SET dbo.[Leave Request].supAprrove_date = :adate,dbo.[Leave Request].supAprrove=:approve, dbo.[Leave Request].partnerApprove = \'rejected\' WHERE id = :id';}
//Prepare our UPDATE SQL statement.
$statement = $pdo->prepare($usql);

//The Primary Key of the row that we want to update.
$id = $_POST['lid'];

//The new model value.
$adate = date('Y-m-d');

//The new model value.
$approve = $_POST['supAprrove'];

//Bind our value to the parameter :id.
$statement->bindValue(':id', $id);

//Bind our :model parameter.
$statement->bindValue(':approve', $approve);

//Bind our :model parameter.
$statement->bindValue(':adate', $adate);

//Execute our UPDATE statement.
$update = $statement->execute();

///send mail

$partMail = $_POST[partMail];
$empName = $_POST[ename];
$lemail = $data['email'];
$fname = $data['First Name'];


        $message = '<html><head>
                <title>Leave Request Application</title>
                </head>
                <body>';



        $message .= '<h1>Leave Request</h1>';
        $message .= '<p><strong>'.$fname.'</strong> has '.$_POST[leave_days].' day(s) of leave '.$_POST[supAprrove].' by his/her supervisor/manager.</p>';
        $message .= '<p>Leave start date <strong>'.date_format(date_create($_POST[start_date]),'d-m-Y').'</strong></p>';
        $message .= '<p> Resumption date <strong>'.date_format(date_create($_POST[end_date]),'d-m-Y').'</strong></p>';
        $message .= '<p>Leave reason: <strong>'.$_POST[leave_reason].'</strong></p>';
        $message .= '<p>Phone Number: <strong>'.$_POST[phone].'</strong></p>';
        $message .= '<p>Please login into Amanfo via this link <a href="'.SITE_URL.'part_pending_leave.php"> to Approve or Reject Leave Request</a>';
        $message .= '<p>Thank you.</p>';
        $message .= '<h3>HR.</h3>';
        $message .= "</body></html>";


        // php mailer code starts
        $mail = new PHPMailer(true);
        $mail->IsSMTP(); // telling the class to use SMTP
        $mail->Mailer = "smtp";

        $mail->SMTPDebug = 0;                     // enables SMTP debug information (for testing)
        $mail->SMTPAuth = false;                  // enable SMTP authentication
        $mail->SMTPSecure = "";                 // sets the prefix to the servier
        $mail->Host = "******";      // sets GMAIL as the SMTP server
        $mail->Port = 25;                   // set the SMTP port for the GMAIL server

        $mail->Username = '*****'; //admin email address
        $mail->Password = '*****';                    // admin email password



        $mail->Subject = trim("Leave Request");
        $mail->MsgHTML($message);

// php mailer code starts
        $mail2 = new PHPMailer(true);
        $mail2->IsSMTP(); // telling the class to use SMTP
        $mail2->Mailer = "smtp";

        $mail2->SMTPDebug = 0;                     // enables SMTP debug information (for testing)
        $mail2->SMTPAuth = false;                  // enable SMTP authentication
        $mail2->SMTPSecure = "";                 // sets the prefix to the servier
        $mail2->Host = "******";      // sets GMAIL as the SMTP server
        $mail2->Port = 25;                   // set the SMTP port for the GMAIL server

        $mail2->Username = '*******'; //admin email address
        $mail2->Password = '******';                      // admin email password


        $mail2->SetFrom('*****', 'HR Department');
        $mail2->AddAddress($lemail);

        $mail2->Subject = "Leave ".$_POST['supAprrove']." by Supervisor";
        $mail2->MsgHTML($message);






        try {

// now send to user.
$mail2->AddAddress($lemail);
//$mail2->AltBody    = ""; // optional, comment out and test


$mail2->Body = "Hello <b>".$fname."</b>, Your leave request has been <b>".$_POST['supAprrove']."</b> by Supervisor";
$mail2->Send();

          if ($_POST['supAprrove']==='rejected'){echo ""; }else {$mail->send();}
          $msg = "You have ".$_POST['supAprrove']." the application, the requestor has been notified";
          header('Location: pending_leave.php?msg');
          $msgType = "success";
        } catch (Exception $ex) {
          $msg = $ex->getMessage();
          $msgType = "warning";
        }


     $insertGoTo = "pending_leave.php?msg"; 
     header(sprintf("Location: %s", $insertGoTo));


    }


    ?>

    <form id="form1" name="form1" method="post" action="pending_leave.php">
      <label>
      <select name="supAprrove" autofocus class="textBox" id="supAprrove" >
      <option value="approved">Approve</option> 
      <option value="rejected">Reject</option>
      </select>

      </label>
      <input name="supAprrove_date" type="hidden" id="supAprrove_date" value="<?php echo date('Y-m-d'); ?>" />
      <input type="hidden" name="lid" id="lid"  value="<?php echo $data['id'];  ?>"/>

      <input type="hidden" name="supName" id="supName" value="<?php echo $data['Supervisor Name'];  ?>" />
      <input type="hidden" name="partMail" id="partMail"  value="<?php echo $data['partner'];  ?>"/>
      <input type="hidden" name="leave_days" id="leave_days"  value="<?php echo intval($data['leave_days']);  ?>"/>
      <input type="hidden" name="start_date" id="start_date"  value="<?php echo date_format($data['start'],'d-m-Y');  ?>"/>
      <input type="hidden" name="end_date" id="end_date"  value="<?php echo date_format($data['resume'],'d-m-Y');  ?>"/>
      <input type="text" name="leave_reason" id="leave_reason"  value="<?php echo $data['leave_reason'];  ?>" size="20"/>
      <input type="hidden" name="phone" id="phone"  value="<?php echo $data['phone'];  ?>"/>
      <input name="ename" type="hidden" id="ename" value="<?php echo $data['First Name'].' '.$data['First Name'].' '.$data['Middle Name'];  ?>" />
      <label>
      <input type="submit" name="sub" id="button" value="Submit" />
      </label>
    </form>
  </td>
     </tr><?php }?>
        </tbody>
    </table>   
    <?php if ($msg <> "") { ?>
  <div class="alert alert-dismissable alert-<?php echo $msgType; ?>">
    <button data-dismiss="alert" class="close" type="button">x</button>
    <p><?php echo $msg; ?></p>
  </div>
<?php } ?> 
</div>
  </div>
</div>
</body>

1 个答案:

答案 0 :(得分:0)

您可以通过做某事来使表单提交独特吗? 喜欢以下

$my_index = "sub_".$data['id'];
if (isset($_POST[$my_index])) {
.............
.............
}

并以您的形式

<input type="submit" name="sub_<?php echo $data['id']; ?>" id="button" value="Submit" />

如果有任何变化,请查看结果。 使您的表单提交独特。我认为这就是问题所在。