Asterisk & FreePbx – Voicemail toggle

VoicemailIMHO many users don’t want the voicemail funct, but they want be able to access to the user portal (ARI) to see the CDR call list and the other stuff or, more easily, they want to activate-deactivate individually voicemail funct using a code on the phone, as in other pbx.

So far this feature has not been implemented in Asterisk/FreePBX: so I have implemented a very simple voicemail toggle, so a user can enable/disable the voicemail (like the Follow-me).

What I’ve implemented is very simple and I think that needs improvement, but…….. still works and for a long time.


function eseguiquery($sql) {
//Here you need to change with a MySql valid account that can update the freepbx db (see /etc/amportal.conf)
 if ($db=@mysqli_connect('localhost','','')) {
  print_r("Connessione fallita\n");

function eseguiwork(){
 $filerun = glob('/var/spool/voicemailed/run/reload.txt');

 if (count($filerun)==1 ){
  $lineefile = file("/var/spool/voicemailed/run/reload.txt");
  foreach ($lineefile as $line_num => $line) {
   $pieces = explode(":", $line);

   $azione = preg_replace('~[[:cntrl:]]~', '', $azione);
   if (!is_numeric($interno))

   if (!is_numeric($azione))

   if ($azione>4||$azione<0) return; 
   //1->On Busy Attiva
   if ($azione==1) {
     eseguiquery("UPDATE `users` SET busy_dest='' WHERE extension like '".$interno."'");
   //2->On Busy Disattiva
   if ($azione==2) {
    eseguiquery("UPDATE `users` SET busy_dest='app-blackhole,busy,1' WHERE extension like '".$interno."'");

   //3->On No Ans. Attivata
   if ($azione==3) {
    eseguiquery("UPDATE `users` SET noanswer_dest='',chanunavail_dest='' WHERE extension like '".$interno."'");

   //4->On No Answ Disattiva
   if ($azione==4) {
    eseguiquery("UPDATE `users` SET noanswer_dest='app-blackhole,busy,1',chanunavail_dest='app-blackhole,busy,1' WHERE extension like '".$interno."'");
 system ("/usr/sbin/amportal a r");


$filescanc = glob('/var/spool/voicemailed/run/*'); // get all file names
foreach($filescanc as $file){ // iterate files
  unlink($file); // delete file



exten => 4041,1,NoOp(--->VM On Busy Attivata)
exten => 4041,n,Playback(beep)
exten => 4041,n,NoOp(${CALLERID(number)})
exten => 4041,n,system(/bin/echo "${CALLERID(number)}:1" >> /var/spool/voicemailed/run/reload.txt)
exten => 4041,n,Hangup
exten => 4042,1,NoOp(--->VM On Busy DisAttivata)
exten => 4042,n,Playback(beep)
exten => 4042,n,Wait(1)
exten => 4042,n,Playback(beep)
exten => 4042,n,NoOp(${CALLERID(number)})
exten => 4042,n,system(/bin/echo "${CALLERID(number)}:2" >> /var/spool/voicemailed/run/reload.txt)
exten => 4042,n,Hangup
exten => 4043,1,NoOp(--->VM On NO Answer Attivata)
exten => 4043,n,Playback(beep)
exten => 4043,n,NoOp(${CALLERID(number)})
exten => 4043,n,system(/bin/echo "${CALLERID(number)}:3" >> /var/spool/voicemailed/run/reload.txt)
exten => 4043,n,Hangup
exten => 4044,1,NoOp(--->VM On NO ANSW DisAttivata)
exten => 4044,n,Playback(beep)
exten => 4044,n,Wait(1)
exten => 4044,n,Playback(beep)
exten => 4044,n,NoOp(${CALLERID(number)})
exten => 4044,n,system(/bin/echo "${CALLERID(number)}:4" >> /var/spool/voicemailed/run/reload.txt)
exten => 4044,n,Hangup


0,15,30,45 * * * * root /usr/bin/php /var/spool/voicemailed/reload.php

Little explanation on how the system works
We have “merged” voicemail on No Reachable with voicemail on No Answer (only for simplicity).
To activate the VoiceMail for On Busy the user dial on the phone 4041: a file will be created on /var/spool/run/reload.txt with the following content.
< Extension number >:1
(remember to set write permissions for user / group asterisk !).
Periodically a php code will check this path, and analyze all requests in the file, in order to perform the correct update to the database: at the end will perform a reload with re-creation of all the config file.

Similar for disable voicemail on No Answer: now the content in the file will be
< Extension number >:2

Even if this system is very raw and needs improving I hope it can help someone !

Voicemail toggle like Follow-Me