#!/usr/bin/php
<?php

//Define filename format: ie  msg0005 [.wav,.txt]
$fmt = "msg%04d";
$to_email = 'support@mydomain.com';
$from_email = 'voicemail@pbx.mydomain.com';
$replyto_email = 'no-reply@mydomain.com';
$message = "Here are the missing emails";

/* ---------------------------------------------------------- *\
|* perhaps issue a find command to get newer than,            *|
|* or a command line list of files, either of those would     *|
|* require some modification to the code as this assumes      *|
|* standard file names based on a number.                     *|
\* ---------------------------------------------------------- */
//tweak this for statement to control which msgs are sent.

for($x = 0; $x<100; $x++) {
$cid = ''; $ot = ''; $time = '';
  $item = sprintf($fmt,$x); //create file prefix

  //Get data from the corresponding .txt file
  $data = file("${item}.txt",FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  for($y = 0; $y < count($data); $y++) {
    if(strpos($data[$y],'=')) {
      $f = split('=',$data[$y]);
//Capture additional fields from the .txt file with this switch operator
      switch($f[0]) {
	case 'callerid':
	  $cid = $f[1]; 
	  break;
	case 'origtime':
	  $ot = $f[1];
	  break;
      } //end switch
    } //end if
  } //end for
   //format the time, should probably be done in the mail function
  if($ot!='') $time = date('Y-m-d h:iA',$ot);
  //call mail formatting + sending function 
  rob_sendmail($item,$to_email,'Voicemail from: '. $cid,$cid,$time);
//  if($x == 21) rob_sendmail($item,$to_email,'Voicemail from: ' . $cid,$cid,$time);
} //end for


function rob_sendmail($file,$to,$sub,$cid,$time) {
/*******************************************\
| rob_sendmail: create a MIME compatible    |
|  email with attachment and multiple parts |
\*******************************************/

$random_hash = md5(date('r', time())); 
$headers = "From: $from_email\r\nReply-To: $replyto_email";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 

$attachment = chunk_split(base64_encode(file_get_contents($file . '.wav'))); 
$msg = "This is a message with multiple parts in MIME format.\r\n" .
	"--PHP-mixed-${random_hash}\r\n" . 
	"Content-Type: multipart/alternative; boundary=\"PHP-alt-${random_hash}\"\r\n\r\n".
	"--PHP-alt-${random_hash}\r\n" .
	"Content-Type: text/plain; charset=\"iso-8859-1\"\r\n".
	"Content-Transfer-Encoding: 7bit;\r\n\r\n" .

	"Voicemail from: $cid\n" . 
	"Date/time: $time\n$message\r\n\r\n" .

	"--PHP-alt-${random_hash}\r\n" .
	"Content-Type: text/html; charset=\"iso-8859-1\"\r\n".  
	"Content-Transfer-Encoding: quoted-printable\r\n\r\n".
	"<html>\n<head></head>\n<body>\n<p>Voicemail from: $cid</p>" .
	"\n<p>Date/time: $time</p><p>$message</p>\n</body>\n</html>\r\n\r\n" .
	"--PHP-alt-${random_hash}--\r\n" .
	"--PHP-mixed-${random_hash}\r\n" .
	"Content-Type: audio/wave\r\n" .
	"Content-Transfer-Encoding: base64\r\n" .
	"Content-Disposition: attachment; filename=\"${file}.wav\"\r\n\r\n" . 

	$attachment .

	"--PHP-mixed-${random_hash}--\r\n\r\n" ;


  $mail_sent = @mail( $to, $sub, $msg, $headers ); 
} //end function