3

We currently have a PHP Script that accesses our emails and pulls the .wav files to be accessed for voicemail purposes.

But the new voicemail system sends the .wav like this: 01359272577 03-14-16 20h14.wav, where the old system did it like this: msg100.wav.

The script download the wav files when there are no spaces. I can't workout why it won't do it when the file has spaces in the name.

EDITED See below new script without SQL. It does everything as it should but it will not extract the file if the original file name has spaces.

EDITED Playing around even more. If you send the email again. But first drag the file to the desktop then back into the email. It drops 1kb and it then puts the file in the folder and works 100%

Can someone help me figure this out?

A little debugging, in regarding to the file can be seen below.

file that didn’t extract.

 Extracting D:/Voicemail/Messages/test/01359272577 2016-03-14 19-47-30.wav... 
stdClass Object ( [type] => 1 [encoding] => 0 [ifsubtype] => 1 [subtype] => MIXED [ifdescription] => 0 [ifid] => 0 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => boundary [value] => multipart-mixed-boundary ) ) [parts] => Array ( [0] => stdClass Object ( [type] => 0 [encoding] => 0 [ifsubtype] => 1 [subtype] => HTML [ifdescription] => 0 [ifid] => 0 [lines] => 1 [bytes] => 247 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 0 [parameters] => stdClass Object ( ) ) [1] => stdClass Object ( [type] => 3 [encoding] => 3 [ifsubtype] => 1 [subtype] => OCTET-STREAM [ifdescription] => 0 [ifid] => 0 [bytes] => 48250 [ifdisposition] => 1 [disposition] => attachment [ifdparameters] => 1 [dparameters] => Array ( [0] => stdClass Object ( [attribute] => filename [value] => 01359272577 03-14-16 19h47.wav ) ) [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => name [value] => 01359272577 03-14-16 19h47.wav

File that did.

Extracting D:/Voicemail/Messages/test/01359272577 2016-03-15 16-15-11.wav... 
stdClass Object ( [type] => 1 [encoding] => 0 [ifsubtype] => 1 [subtype] => MIXED [ifdescription] => 0 [ifid] => 0 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => boundary [value] => _004_6774455887777E4C8AEAC737C2F28B0D95E27FISERVER2infusions_ ) ) [parts] => Array ( [0] => stdClass Object ( [type] => 1 [encoding] => 0 [ifsubtype] => 1 [subtype] => ALTERNATIVE [ifdescription] => 0 [ifid] => 0 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => boundary [value] => _000_6774455887777E4C8AEAC737C2F28B0D95E27FISERVER2infusions_ ) ) [parts] => Array ( [0] => stdClass Object ( [type] => 0 [encoding] => 4 [ifsubtype] => 1 [subtype] => PLAIN [ifdescription] => 0 [ifid] => 0 [lines] => 6 [bytes] => 152 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => charset [value] => us-ascii ) ) ) [1] => stdClass Object ( [type] => 0 [encoding] => 4 [ifsubtype] => 1 [subtype] => HTML [ifdescription] => 0 [ifid] => 0 [lines] => 70 [bytes] => 2137 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => charset [value] => us-ascii ) ) ) ) ) [1] => stdClass Object ( [type] => 4 [encoding] => 3 [ifsubtype] => 1 [subtype] => WAV [ifdescription] => 1 [description] => 01359272577 03-15-16 16h05.wav [ifid] => 0 [bytes] => 87660 [ifdisposition] => 1 [disposition] => attachment [ifdparameters] => 1 [dparameters] => Array ( [0] => stdClass Object ( [attribute] => filename [value] => 01359272577 03-15-16 16h05.wav ) [1] => stdClass Object ( [attribute] => size [value] => 67660 ) [2] => stdClass Object ( [attribute] => creation-date [value] => Tue, 15 Mar 2016 16:14:54 GMT ) [3] => stdClass Object ( [attribute] => modification-date [value] => Tue, 15 Mar 2016 16:14:54 GMT ) ) [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => name [value] => 01359272577 03-15-16 16h05.wav

This is my code:

<?php

$MSG_DIR = "D:/Voicemail/Messages";
$USER = "XX";
$PASS = "XX";

// connect to server
echo "Opening mailbox...\n<br/>";
$host = "{localhost:143/imap4/novalidate-cert}Inbox";
$mbox=imap_open($host,$USER,$PASS, NULL, 1, array('DISABLE_AUTHENTICATOR' => 'GSSAPI')) or die("Can't connect: " . imap_last_error());

// iterate over mailbox
echo "Finding emails...\n<br/>";
$MC = imap_check($mbox) or die("Can't check for messages");

$overview = imap_fetch_overview( $mbox, "1:".$MC->Nmsgs, 0 ) or die( "Can't get headers");
$countThis = 0;
foreach ( $overview as $email )
{       
    $subject = $email->subject;
    $udate = $email->udate;
    if( preg_match('/Voice Message from ([0-9]+)/', $subject, $groups) )
    {
        $filename = $MSG_DIR."/".$groups[1].date(" Y-m-d H-i-s", $udate).".wav";

        if( file_exists( $filename ) )
        {
            echo "Skipping $filename...\n<br/>";
        }
        else
        {
            echo "Extracting $filename...\n<br/>";
             $structure = imap_fetchstructure($mbox,$email->uid, FT_UID) or die( "could not fetch structure");
             foreach ( $structure->parts as $part )
             {
                if( $part->subtype == "X-WAV" || $part->subtype=="WAV")
                {
                    // found it!
                    $body = imap_base64( imap_fetchbody( $mbox, $email->uid, 2, FT_UID ) ) or die( "Could not fetch part");
                    file_put_contents( $filename, $body );
                }
                else
                {
                }
             }
        }
        $countThis++;
    }
}
echo "Found $countThis voicemails\r\n";

imap_close($mbox);  ?>
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
Law
  • 51
  • 1
  • 4
  • 1
    I'm guessing this regexp maybe: `/Caller:[\s\t]+\".*\"[\s\t]+<([0-9]+)>/` and/or the one just after that? I don't think anyone here is going to debug 156 lines of code they can't even run/test for you by the way unless you're going to start paying people. Sorry :-( You'll need to isolate the problem yourself by creating a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Also see [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). – Martin Tournoij Mar 15 '16 at 11:12
  • Thank you for the reply. Sorry. It may just be the case I will have to pay someone to adjust it. – Law Mar 15 '16 at 11:20
  • `$filename = $MSG_DIR."/".$username.date(" Y-m-d H-i-s", $udate).".wav";` _"the new voicemail system sends the .wav like this: 01359272577 03-14-16 20h14.wav"_ To me, these look nothing alike. – Lightness Races in Orbit Mar 15 '16 at 11:39

1 Answers1

0

Added the below

$part->subtype=="OCTET-STREAM"

on to the end of

 if( $part->subtype == "X-WAV" || $part->subtype=="WAV")

It would seem a NEC SV8100 voicemail file subtype array is not WAV it's OCTET-STREAM.

Law
  • 51
  • 1
  • 4