Passing Variables to Mail Command

They are not set as environment variables. They appear to be set as a side effect of using the internal string manipulation code to construct the contents of the email message, so they are passed to the mailcmd on its standard input, and you would have to extract them the same way as if you were doing this at the point where the mail is delivered.

voicemail.conf specifies templates for parts of the email, and those templates contain dialplan variable references, e.g. some of the variables you mention are used to construct the email Subject header, which can be set to a non-default value here:

You could probably redefine the mail format to be easier for you to parse, but it still going to arrive in standard input.

Also, do you actually want to be able to do normal telephone access, to read the mailbox. If not, I suspect people capturing automated voice calls as text, would use Record(), or use ARI or AGI with real time streaming of the call to the recognizer. (Not withstanding which I would want to save the original audio, as the text will, almost certainly, contain transcripiton errors, some of which may be difficult to decipher.)

What the OP seems to want to do is replace the email that has the recording attached, with a pure text one that has an AI transcription of the audio, possibly because the agents are not fluent in the callers’ spoken language.

The script is fairly simple. The operation sending the data to openai is using a python application.

MSGNUM=$2
VM_EMAIL=$3
VM_CALLERID=$4

_# Path to the virtual environment
VENV_PATH=“/etc/asterisk/custom/ai_env”

_# Activate the virtual environment
source “$VENV_PATH/bin/activate”

_# Run the transcription script and capture the output
TRANSCRIPTION=$(“$VENV_PATH/bin/python3” >/etc/asterisk/custom/vm_transcribe.py “$MAILBOX” “$MSGNUM”)

_# Deactivate the virtual environment
deactivate

_# Fallback if transcription fails
if [ -z “$TRANSCRIPTION” ]; then
TRANSCRIPTION=“Transcription unavailable or failed.”
fi

_# Log transcription output
echo “$(date) - Voicemail Transcription: $TRANSCRIPTION” >> >/var/log/asterisk/transcriptions.log
echo “$(date) - Variables Used: MAILBOX = $MAILBOX, MSGNUM = $MSGNUM, >VM_EMAIL = $VM_EMAIL, VM_CALLERID = $VM_CALLERID” >> >/var/log/asterisk/transcriptions.log

_# Construct the email body (Ensure EOF is properly formatted)
EMAIL_BODY=$(cat <<EOF
You have a new voicemail from $VM_CALLERID.

Transcription:
$TRANSCRIPTION

Access the full voicemail, log into your user control panel at:
https://example.freepbxurl.com/ucp/#
EOF
) # ← Ensure EOF is on a line by itself with no spaces

_# Send the email
echo “$EMAIL_BODY” | mail -s “Voicemail from $VM_CALLERID” “$VM_EMAIL”

_# Log email success
if [ $? -eq 0 ]; then
echo “$(date): Email sent successfully to $VM_EMAIL” >> /var/log/asterisk/transcriptions.log
else
echo “$(date): Failed to send email to $VM_EMAIL” >> /var/log/asterisk/transcriptions.log

In Settings->Voicemail Admin->Settings->Email Config->Mail Command, I entered:

/etc/asterisk/custom/transcription.sh ${VM_MAILBOX} ${VM_MSGNUM} ${VM_EMAIL} ${VM_CALLERID}

Log results were:

29 Thu Jan 30 03:09:46 PM CST 2025 - Voicemail Transcription: Transcription unavailable or failed.
30 Thu Jan 30 03:09:46 PM CST 2025 - Variables Used: MAILBOX = , MSGNUM = , VM_EMAIL = , VM_CALLERID

If I put in the Mail Command field:

/etc/asterisk/custom/transcription.sh 1001 1 [email protected] 80055551212
the information is passed and I receive the data for message 1 for mailbox 1001. So FreePBX is passing data, just not from the system variables.

Yes, translation is certainly something to consider for down the road, but right now, I’m strictly concerned with actually getting the transcription to the recipient. I’m not married to sending it with or without the audio file. As a matter of fact, when I started this process, I was expecting to be able somehow to create a ${TRANSCRIPTION} variable that could be included in the emailbody field. That would be the simplest solution, but in my research, it appears that the actual variables are fixed and can’t be added to.

It has been what I have been saying this entire time. Which is why I gave you the link to the gist that I did. It allows to add the transcript into the existing body of the email because it’s the only piece that needs to be added.

Okay Tom,

Taking a hard look at that code. I guess I was thrown by the fact that it’s going to Watson, but I guess transcription service doesn’t really matter. I also missed that this is still placed in mailcmd.

So if I’m understanding correctly, the line

cat >> stream.org

captures what is being sent to the email recipient by freepbx correct? Then everything that follows is about splitting the message into parts, sending the attachment to, in your case Watson, in mine Whisper, and then appending the results to part 2 of the reconstructed file. Right?

Lastly, you send the reconstructed file to sendmail with the line:

|cat stream.new | sendmail -t

Am I properly understanding what you are doing here?

If that’s the case, let me try to split out the parts that are Watson specific and replace them with the return from vm_transcribe.py. Will let you guys know the results.

You can modify it to use what you’d like. It was more to give you a jumping point at the solution you wanted.

Doesn’t seem to be working. I just tried adding some lines of text to the final output, but nothing got changed from the original emailbody form…Mind looking this over and seeing what I might be missing?

Edit: Nevermind… figured it out… put the script in the wrong field in the Email Config tab. And I wondered why it started sending the path and filename of the script file in the email address of the message!

This looks like it’s going to work. I’ll post my final code as well as the python program in case someone else is facing the same challenges. Thanks again Tom. Appreciate your patience.

If you are using Python, there is a class for parsing multipart email: email.parser: Parsing email messages — Python 3.13.1 documentation

Okay guys,

Here is the final program. I decided that instead of opening a virtual environment and running the python compiler, it would be far simpler to just use curl. If I ever want to do anything more fancy than just send the file and then receive a transcription, then I’ll use python to do it.

I’m putting this script in /etc/asterisk/custom, being sure to assign it to the asterisk user and group with chmod and making the script executable.

chmod 755 /etc/asterisk/custom/transcription.sh

chown asterisk:asterisk /etc/asterisk/custom/transcription.sh

Thank you guys for all your help, and thank you for your patience.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.