Passing Variables to Mail Command

I’m on FreePBX version 17 and am trying to run a bash script after a voicemail message is left. I’ve been looking at using Mail Command (mailcmd) in Voicemail Admin ->Settings->Email Config, but I haven’t been able to figure out how to pass the VM_MAILBOX or other variables to the script. I’ve tried ${VM_MAILBOX} and a few other variations with no success.

Is passing variables to a script even possible in the mailcmd context? If I manually put the values in the field, it passes those through, but not the system variables.

Is anyone aware of any other method of passing voicemail message variables to a script?

IIRC you need to pass the variables like this `^{VAR}

Edit - disregard… What I posted is for the call recording script.

You don’t want to use mailcmd for this because it just replaces /usr/sbin/sendmail -t and Asterisk is going to pass the full formatted email with headers and all into the command. You would have to parse the email contents to do anything with it. Even then, all the variables in voicemail.conf wouldn’t be available. They only exist in voicemail.conf.

What are you actually trying to do with this bash script?

Thank you for responding.

I’m trying to send the message to OpenAI through the API for transcription. The bash script works flawlessly if I run it from the command line, and even works if I manually input the data that’s supposed to be supplied by the variables on the Mail Command line. But whenever I try to include the ${} variables, the log output from the script shows that nothing was passed.

Years ago, I did something similar to this sending messages to Watson, but it’s been so long ago and I don’t recall why I stopped using that functionality.

Any suggestions for how to accomplish this would be appreciated. Once the message is transcribed, I intend to send it to my CRM as well as email. But one step at a time…

I thought about that too. I’ve sent call recording scripts to another AI application and used variables like that. So far I’ve tried using ^{}, %{} or just $VM_MAILBOX, and none of those work.

You would have to do something like this in your bash script but again those variables that can be used in voicemail.conf ${VM_MAILBOX} etc are only usable in the context of the settings within voicemail.conf. They are used internally by app_voicemail to create the email message.

1 Like

Thanks Tom. I’m going to have to look at this in more depth later, but I still don’t see how the data is passed. What is the line to be entered into mailcmd?

The variables you keep referring to are only usable in the settings of voicemail.conf that means that these variables:

VM_NAME, VM_DUR, VM_MSGNUM, VM_MAILBOX, VM_CALLERID, VM_CIDNUM, VM_CIDNAME, VM_DATE

Can only be used in the emailsubject, emailbody, pagersubject, pageremail settings within voicemail.conf. They will not be accessible externally from app_voicemail.

Asterisk is going to generate the email based on what is set in voicemail.conf since each mailbox can have their own emailsubject and emailbody settings and if the message should be attached. This means it is passing a fully formatted email into where ever is set in mailcmd

So are you looking to parse an entire email or are you looking to build your own notifications when a new message is being left?

The default should work on most systems, even if they don’t use sendmail, as all the other Unix/Linux mail systems emulate the basic sendmail options that the default command uses.

No variables are passed on the command line. A completely formatted MIME message is provided on its standard input, and it is expected to parse out the destination from that.

The file is in the same format as you would get by saving a message as a .eml file, in Thunderbird, and probably most other email clients, except that it lacks headers that are added in transit.

I’m not really a stickler on how it gets accomplished, but ultimately, when a message is left, I’m trying to send a copy of the recording to openai’s whisper model to be transcribed into text. When whisper responds, I want to include the results along with the notification email that’s sent.

As it stands, if I manually run the bash script, including the message specifics, I get a message with the transcription from whisper. I can even enter the bash script in the mailcmd field in freepbx with manually supplied data, but that’s not very useful.

for reference, the prototype for the application is: transcription.sh {MAILBOX NUMBER} {MESSAGE NUMBER} {RECIPIENT EMAIL} {CALLER ID}

So if in the mailcmd field I put:

/etc/asterisk/custom/transcription.sh 1001 1 [email protected] 800-555-1212

then if a message is left anywhere on the system, I will get an email on [email protected] with the transcription of message number one from mailbox 1001.

But of course to make this setup useful, I need to somehow automatically pass the information from the freepbx to the bash script. If using mailcmd isn’t the best option, I’m open for suggestions on how to accomplish this.

I’ve considered using the externnotify field in the general tab, but my understanding is that this has limited utility as well.

Happy birthday!

Interesting project. Safety note - please keep in mind how you handle this AI generated text in bash shell commands, paying attention to escape certain characters and generally treating it like unsafe user input.

Additional safety note - when handling raw Caller ID information, you are (potentially) entering into lower-level Asterisk territory, so you may find that functions like FILTER are helpful to your efforts.

You cannot pass extra variables into these commands. It doesn’t matter if it is mailcmd or externotify. Each of the commands have their own variables that Asterisk passes automatically.

The script I shared with you parses the email and looks for an attachment. It sends the attachment out to get the transcript. It then adds the transcript to the body of the email. All the variables from voicemail.conf have already been process and formatted and are in the email.

This is moot. All the settings below that allow the calling of an external program do not accept additional variables. Each of them will internally pass variables defined by Asterisk. You are limited to variables each command will internally pass.

externpass
externpassnotify
externpasscheck
mailcmd
externnotify

I would suggest

pointed at /var/spool/asterisk/voicemail and looking for and operating on ‘write_close’ of any *.msg, you can extract the email from, voicemailconf, message numbers are fluid, extension numbers and dates are intrinsic in the name

externpass=/path/to/app
externpassnotify=/path/to/app

These two settings will pass the following variables when calling the programs defined.

<context> <mailbox> <newpassword>

externpasscheck=/path/to/app
will pass <mailbox> <context> <old pw> <new pw>

mailcmd=/path/to/app
will pass an entirely formatted email to the application. Uses the variables set in emailbody and emailsubject in voicemail.conf

externnotify=/path/to/app
will pass <context> <extension> <new msgs> <old msgs> <urgent msgs>
Note: This command is triggered when a new voicemail is left or the user has accessed the mailbox.

Example
New Message: default 100 1 0 0
User Accessed: 100@default 0 1 0 (context variable is empty)

Thank you Tom! This gives me several options of how to play this. If I can’t get it to work with the information you provided, Dicko’s solution seems like it might be a good second option.

I should be able to look at these options Monday. I’ll let you know what happens.

Thank you both for your help.

I sure as heck ain’t going to be suggesting this solution to any company governed by HIPAA. Thanks for the reminder, though.

The caller ID information is just for information to be included in the email. I don’t plan on manipulating any data supplied from the FreePBX. It’ll just be passed on as-is to the recipient.

Thanks for the birthday wishes.

1 Like

I think you are missing that it is being processed when it is parsed into parameters from a shell command line. That’s the classic way code injection vulnerabilities work.

Hey Tom,

Quick question. The way I’m reading this makes it sound like it should pass ${VM_NAME} or ${VM_MAILBOX} (the way they are noted in emailbody in voicemail.conf) to the application. Am I reading that right?

Reason I ask is that doesn’t seem to be what’s happening. When I have the script write the contents of those variables to the log, they appear to be empty. They do appear in the log if I hard code those values into the mailcmd string. So either my understanding of what you’re saying is incorrect, the variable notation is incorrect or possibly there is a bug in the way FreePBX handles things, or a disconnect between the documentation and the actual operation?

What are you doing? Show an example.