Jump to content

email script sends two emails


drranch

Recommended Posts

need some assist... the script below sends out two emails and I cant seem to find out why...Im semi new to php and Im thinking Im missing something...

 

<?php

function dprint_r($array) {
  echo "<pre>";
  print_r($array);
  echo "</pre>";
  die();
}

function get_status($status_code) {
  switch ($status_code) {
  case 0:
    $status = "New";
    break;
  case 1:
    $status = "Assigned";
    break;
  case 2:
    $status = "Work In Progress";
    break;
  case 3:
    $status = "Pending";
    break;
  case 4:
    $status = "Resolved";
    break;
  case 5:
    $status = "Closed";
    break;
  default:
    $status = "??????";
  }
  return $status;
}

function get_report()
{
  global $holidays, $unix_now, $unix_morning;
  switch(date('l', time())) {
  case 'Monday':
    if (in_array($curdate, $holidays))
      $report = "none";
    elseif ($unix_now < $unix_morning)
      $report = "weekend";
    elseif ($unix_now > $unix_morning)
      $report = "daily";
    break;
  case 'Tuesday':
    if (in_array($yesterday, $holidays))
      $report = "weekend";
    else
      $report = "daily";
    break;
  case 'Wednesday':
  case 'Thursday':
  case 'Friday':
    $report = "daily";
    break;
  case 'Saturday':
  case 'Sunday':
    $report = "none";
    break;
  default:
    die('Invalid day');
  }
  return $report;
}

require_once('include.php');
require_once('Spreadsheet/Excel/Writer.php');
require_once('email.class.php');

$xls =& new Spreadsheet_Excel_Writer('daily.xls');
$sheet =& $xls->addWorksheet('Sheet 1');
$email_to = '[email protected];
$email_from = 'your<[email protected]>';
$email_subject = 'Daily report for ' . date('m/d/Y');
$email = new Email($email_to, $email_from, $email_subject);

$holidays = array("1/1/2007",
	  "1/15/2007",
	  "2/19/2007",
	  "5/28/2007",
	  "7/4/2007",
	  "9/3/2007",
	  "11/22/2007",
	  "11/23/2007",
	  "1/1/2008");

$fields = array('Case ID',
	'Requestor',
	'New.TIME',
	'Summary',
	'Status',
	'Assigned Group');


$unix_now = time();
$curtime = date('H:i:s');
$curdate = date('m/d/Y');
$yesterday = date('m/d/Y', strtotime('yesterday'));
$curmorning = "$curdate 09:30:00";
$midnight = "$curdate 00:00:00";
$unix_morning = strtotime($curmorning);
$unix_midnight = strtotime($midnight);
$report = get_report();


switch($report) {
case 'daily':
   $time = strtotime('Yesterday 6:30am');
   $sqldate = "AND sh.new_time >= $time";
   break;
case 'weekend':
   $time = strtotime('Last Friday 6:30am');
   $sqldate = "AND sh.new_time >= $time";
   break;
default:
   die('No report generated');     
}
    

    
$sql = "SELECT h.case_id_, h.requester_name_, sh.new_time, h.summary, h.status, h.assigned_to_group_
            FROM hpd_helpdesk AS h, sh_hpd_helpdesk as sh
            WHERE h.priority = 2
            AND sh.case_id_ = h.case_id_
            $sqldate
            ORDER BY h.case_id_ ASC";
$rs = $db->Execute($sql);


$colheading =& $xls->addFormat();
$colheading->setBold();
$colheading->setAlign('center');
$sheet->write(0, 0, 'Daily');
$text[] = 'Daily High Ticket Report';
$sheet->write(1, 0, '1234567');
$text[] = '1234567';
$sheet->write(2, 0, '1234567');
$text[] = '1234567';
$sheet->writeRow(4, 0, $fields, $colheading);
$text[] = implode("\t", $fields);
$rowcount = 5;

$rowformat =& $xls->addFormat();
$rowformat->setTextWrap(true);

//die(print_r($rs));


while ($row = $rs->FetchRow()) {
  $row['status'] = get_status($row['status']);
  $row['new_time'] = date('m/d/Y H:i', $row['new_time']);
  $sheet->setColumn(0, 0, 20);
  $sheet->setColumn(0, 1, 20);
  $sheet->setColumn(0, 2, 20);
  $sheet->setColumn(0, 3, 40);
  $sheet->setColumn(0, 4, 15);
  $sheet->setColumn(0, 5, 20);
  $sheet->writeRow($rowcount++, 0, $row, $rowformat);
  $text[] = implode("\t", $row);
}

// save the text file
$fp = fopen('daily_high_tickets.txt', 'w');
foreach($text as $row) {
  $row .= "\r\n";
  fwrite($fp, $row);
}

// save the xls file
$xls->close();

// attach the files and send the email
$email->Attach('daily.xls', 'application/vnd.ms-excel');
$email->Attach('daily.txt', 'text/plain');
$email->Send();
?>

Link to comment
https://forums.phpfreaks.com/topic/135543-email-script-sends-two-emails/
Share on other sites

Archived

This topic is now archived and is 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.