Listen to your Emails – Part I January 28, 2009

Continuing with the series of  integrating voice into the web, here is an IMAP email client application. This code shows how VoicePHP can be used to easily write web applications which speaks and listens.

This demo does the following:

The application can be easily customized to your needs. For sake of demonstrating it, we’ve customized it to read emails from test.voicephp@gmail.com. To try it out, just send an email to test.voicephp@gmail.com and then click on the widget below to hear your message being read out.

We can’t wait to see how a VoiceXML based email client code would look like. Any takers?

<?php
echo "Welcome To VoicePHP E Mail Reader Demo";

$mail imap_open('{imap.gmail.com:993/imap/ssl}INBOX''test.voicephp@gmail.com''********')
              or die(
"Connection to mail server failed: "imap_last_error());

//Search mailbox for unread messages 
$messages=imap_search ($mail'UNSEEN SINCE ' date('d-M-Y'time() - 7200) . '');
if(
$messages 1) { 
    
rsort($messages);
    
$count count($messages);
    echo 
"You have $count new unread mails";
    echo 
"To hear the body of a mail, press 1 after the subject is read,
    press 0 to exit anytime"
;

    
get_subject($messages,$count,$subject,$header,$mail);
    for(
$i=0;$i<$count;$i++){ 
        
$voicetype = ($i&1)?"female":"male";
        
setvoiceoption("voicetype"$voicetype);
        
$op prompt($subject[$i], 11"dtmf:1");
        if(
$op == "1") {
            
read_mail($messages[$i], $mail);        
            echo 
"Returning to subject menu";
        }
        elseif(
$op == "0") { 
            break;
        }
    }
}
else {
    echo 
"You have no new mails";
}
imap_close($mail);
echo 
"Thank you for trying Voice PHP Demo";

//Get the subject of the mail ..
function get_subject($messages,$count,&$subject,&$header,$mail){
    for(
$i=0;$i<$count;$i++) { 
        
//Get IMAP header info for mail
        
$header imap_headerinfo($mail$messages[$i]);
        
$subject[$i] = "Mail from " $header->from[0]->personal " with subject " $header->subject;
    }
}

//Read a mail body..
function read_mail($num,$mail) {    
    
// Get the message body
    
$st imap_fetchstructure($mail$num);
    if (!empty(
$st->parts)) {
        for (
$i 0$j count($st->parts); $i $j$i++) {
            
$part $st->parts[$i];
            if (
$part->subtype == 'PLAIN') {
                 
$body imap_fetchbody($mail$num$i+1);
            }
         }
    } else {
        
$body imap_body($mail$num);
    }    
    echo 
$body;
}
?>

Download source code

Try It Now!

Click the button below or call +15672440715 to run the application.

Does'nt it look easy? Try VoicePHP for yourself and feel free to modify the above script. VoicePHP is easy and accessible from your free TringMe account. More details at http://voicephp.com/howitworks.html

Leave a Comment

If you would like to make a comment, please fill out the form below.

Name (required)

Email (required)

Website

Comments

© Copyright TringMe., All rights reserved. - Blog powered by Wordpress - Designed by Speckyboy