3 # Copyright 2011 Iordan Iordanov <iiordanov (AT) gmail.com>
5 # This file is part of luci-pbx-voicemail.
7 # luci-pbx-voicemail is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # luci-pbx-voicemail is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with luci-pbx-voicemail. If not, see <http://www.gnu.org/licenses/>.
21 # Thanks to http://www.zedwood.com for providing an excellent example of how to
22 # properly assemble an email message with a base64 encoded attachment.
25 LOGFILE=/tmp/voicemail/last_sent_voicemail.log
27 # Redirect standard error and standard output to a log file.
32 # Appends its second argument to a file named in the first argument.
38 # Grab the attachment name, which should be sent as the first argument, and
39 # exit with a warning if there is no voicemail to send.
41 [ ! -f "$ATTACHMENT" ] && echo "WARNING: Found no voicemail recording to send." && exit
43 # Grab the callerID which should have been sent as an argument.
45 [ -z "$CALLERID" ] && CALLERID="An unknown caller"
47 # Determine addresses we would like to send the voicemail to and exit if none are found.
48 TO="`uci -q get pbx-voicemail.global_voicemail.global_email_addresses | tr ' ' ','`"
49 [ -z "$TO" ] && echo "WARNING: Found no addresses to send voicemail to." && exit
51 # See whether we should retain a copy of the voicemail.
52 SAVEPATH="`uci -q get pbx-voicemail.global_voicemail.global_save_path`"
54 DATE="`date +%Y-%m-%d`"
55 TIME="`date +%H:%M:%S`"
57 REPLY="do-not-reply@pbx"
58 SUBJECT="Voicemail from $CALLERID, $DATE, $TIME"
59 MSGBODY="$CALLERID has left voicemail for you on $DATE at $TIME."
61 TMP1="/tmp/voicemail/tmpemail1.$$";
62 TMP2="/tmp/voicemail/tmpemail2.$$";
63 BOUNDARY="`date +%s | md5sum | awk '{print $1}'`"
64 FILENAME="voicemail-$DATE-$TIME.WAV"
66 # Clean up just in case.
69 append_to_file $TMP1 "From: $FROM"
70 append_to_file $TMP1 "To: $TO"
71 append_to_file $TMP1 "Reply-To: $REPLY"
72 append_to_file $TMP1 "Subject: $SUBJECT"
73 append_to_file $TMP1 "Content-Type: multipart/mixed; boundary=\""$BOUNDARY"\""
74 append_to_file $TMP1 ""
75 append_to_file $TMP1 "This is a MIME formatted message. If you see this text it means that your"
76 append_to_file $TMP1 "email software does not support MIME formatted messages."
77 append_to_file $TMP1 ""
78 append_to_file $TMP1 "--$BOUNDARY"
79 append_to_file $TMP1 "Content-Type: text/plain; charset=ISO-8859-1; format=flowed"
80 append_to_file $TMP1 "Content-Transfer-Encoding: 7bit"
81 append_to_file $TMP1 "Content-Disposition: inline"
82 append_to_file $TMP1 ""
83 append_to_file $TMP1 "$MSGBODY"
84 append_to_file $TMP1 ""
85 append_to_file $TMP1 ""
86 append_to_file $TMP1 "--$BOUNDARY"
87 append_to_file $TMP1 "Content-Type: $MIMETYPE; name=\"$FILENAME\""
88 append_to_file $TMP1 "Content-Transfer-Encoding: base64"
89 append_to_file $TMP1 "Content-Disposition: attachment; filename=\"$FILENAME\";"
90 append_to_file $TMP1 ""
92 append_to_file $TMP2 ""
93 append_to_file $TMP2 ""
94 append_to_file $TMP2 "--$BOUNDARY--"
95 append_to_file $TMP2 ""
96 append_to_file $TMP2 ""
98 # Cat everything together and pass to msmtprc to send out.
100 cat "$ATTACHMENT" | base64
101 cat $TMP2 ) | msmtp -t -C /etc/pbx-msmtprc
103 # Clean up email temp files.
106 # Either delete or move the attachment based on the SAVEPATH variable.
107 if [ -z "$SAVEPATH" ]
112 mv --backup=t "$ATTACHMENT" "$SAVEPATH/$FILENAME"