Skip to main content

Lua Mail Call example

About

This script takes one argument, a filename, and mails that file using lua. You must have the lua socket lib loaded.http://w3.impa.br/\~diego/software/luasocket/old/luasocket-2.0-beta/smtp.html

Call it like this in the dialplan:

<action application="set" data="hangup_after_bridge=false"/>
<action application="set" data="exec_after_bridge_app=transfer"/>
<action application="set" data="exec_after_bridge_arg=mail_this"/>
<action application="set" data="RECORD_TITLE=Recording ${destination_number} ${caller_id_number} ${strftime(%Y-%m-%d %H:%M)}"/>
<action application="set" data="RECORD_DATE=${strftime(%Y-%m-%d %H:%M)}"/>
<action application="set" data="RECORD_STEREO=true"/>
<action application="set" data="rec_file=$${base_dir}/recordings/${strftime(%Y-%m-%d-%H-%M-%S)}_${destination_number}_${caller_id_number}.mp3"/>
<action application="record_session" data="${rec_file}"/>
<action application="bridge" data="user/${dialed_extension}@${domain_name}"/>

<extension name="mail_the_file">
<condition field="destination_number" expression="^mail_this$">
<action application="lua" data="mail_call.lua ${rec_file}"/>
<action application="hangup" data="NORMAL_CLEARING" />
</condition>
</extension>

Here is mail_call.lua

session:sleep(500);
freeswitch.consoleLog("NOTICE", "Mail Call!"..argv[1].." \n");

-- load the smtp support and its friends
local smtp = require("socket.smtp")
local mime = require("mime")
local ltn12 = require("ltn12")

-- creates a source to send a message with two parts. The first part is
-- plain text, the second part is an mp3, encoded as base64.
source = smtp.message{
headers = {
-- Remember that headers are *ignored* by smtp.send.
from = "FreeSWITCH <freeswitch@servercorps.com>",
to = "Mail Call<mailcall@freeswitch.org>",
subject = "Here is a message with mp3 attachment"
},
body = {
-- first part: no headers means plain text, us-ascii.
-- The mime.eol low-level filter normalizes end-of-line markers.
[1] = {
body = mime.eol(0, [[
Here is a call logged and recorded by FreeSWITCH.
]])
},
-- second part: headers describe content to be an mp3 file
-- sent under the base64 transfer content encoding.
-- notice that nothing happens until the message is actually sent.
-- small chunks are loaded into memory right before transmission and
-- translation happens on the fly.
[2] = {
headers = {
["content-type"] = 'audio/mpeg; name='..argv[1],
["content-disposition"] = 'attachment; filename='..argv[1],
["content-description"] ='Call Recording mp3',
["content-transfer-encoding"] = "BASE64"
},
body = ltn12.source.chain(
ltn12.source.file(io.open(argv[1], "rb")),
ltn12.filter.chain(
mime.encode("base64"),
mime.wrap()
)
)
},

}
}

-- finally send it
r, e = smtp.send{
from = "<freeswitch@servercorps.com>",
rcpt = "<mailcall@freeswitch.org>",
source = source,
}

--Todo : Delete the mp3 file here