From FreeSWITCH Wiki
Jump to: navigation, search

Email to Callback system

These instructions assume you are using postfix on a debian based system. Please adapt to your own system appropriately.

To use the following system, send an email to callback@FQDN, with either the subject of "callback NUMERIC" or somewhere in the body with "callback NUMERIC". This will then dial the number NUMERIC based on the dialplan of the email2callback user, for example "callback 1800123123".

1. Install postfix, sudo, python, and have postfix accept mail to local unix users.

2. Make sure 'freepy' (found in the scripts/socket directory) is setup correctly, which may mean installation of the SMC python library as per the README/INSTALL of freepy.

3. Create an extension, with username of email2callback, and choose an appropriate password for the user. Please set the password within the python script below.

4. Add the following to /etc/sudoers, which will allow the nobody user (which postfix uses for spawning scripts from /etc/aliases) to execute the script owned by freeswitch:

nobody	ALL=NOPASSWD: /opt/freeswitch/scripts/socket/

5. Add the following to /etc/aliases, and then run newaliases:

callback: | /usr/local/bin/

6. Create the file /usr/local/bin/

sudo /opt/freeswitch/scripts/socket/

7. Create the file /opt/freeswitch/scripts/socket/

# script to parse email for phonenumber in subject or body, and call back with extension
# Replace DOMAIN with the value of $${domain}
# Replace EXTENSION with the value of the application (&rxfax()) or extension (5000)
# Replace PASSWORD with the password for the email2callback user extension

import sys
import os
import email.Parser
import types
import re
import syslog


from fshelper import *

def initiateCallback():

    # first try to parse the incoming email
    message = email.message_from_file(sys.stdin)

    #  process the message here
    syslog.syslog('Received mail from "%s", subject: "%s"' % (message.get('From'), message.get('Subject') ))

    subject = message.get('Subject')
    m = re.match('callback (\d*)', subject)
    if m:
	    outgoing_number =  

    msg = message.get_payload()
    m = re.match('callback (\d*)', msg)
    if m:
            outgoing_number =
    syslog.syslog('Making outgoing call to "%s"' % outgoing_number) 

    fshelper = FsHelper(host="",

    def worked(result):
        print "Originate succeeded: %s" % result
    def failed(failure):
        print "failed: %s" % failure

    dest_ext_app = "EXTENSION"
    party2dial="[sip_auth_username=email2callback,sip_auth_password=PASSWORD]sofia/external/%s@DOMAIN" % outgoing_number
    d2 = fshelper.originate(party2dial=party2dial,

if __name__=="__main__":