pbx-send-voicemail 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/sh
  2. #
  3. # Copyright 2011 Iordan Iordanov <iiordanov (AT) gmail.com>
  4. #
  5. # This file is part of luci-pbx-voicemail.
  6. #
  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.
  11. #
  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.
  16. #
  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/>.
  19. #
  20. #
  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.
  23. #
  24. LOGFILE=/tmp/voicemail/last_sent_voicemail.log
  25. # Redirect standard error and standard output to a log file.
  26. rm -f "$LOGFILE"
  27. exec 1>"$LOGFILE"
  28. exec 2>&1
  29. # Appends its second argument to a file named in the first argument.
  30. append_to_file ()
  31. {
  32. echo "$2">>$1;
  33. }
  34. # Grab the attachment name, which should be sent as the first argument, and
  35. # exit with a warning if there is no voicemail to send.
  36. ATTACHMENT="$1"
  37. [ ! -f "$ATTACHMENT" ] && echo "WARNING: Found no voicemail recording to send." && exit
  38. # Grab the callerID which should have been sent as an argument.
  39. CALLERID="$2"
  40. [ -z "$CALLERID" ] && CALLERID="An unknown caller"
  41. # Determine addresses we would like to send the voicemail to and exit if none are found.
  42. TO="`uci -q get pbx-voicemail.global_voicemail.global_email_addresses | tr ' ' ','`"
  43. [ -z "$TO" ] && echo "WARNING: Found no addresses to send voicemail to." && exit
  44. # See whether we should retain a copy of the voicemail.
  45. SAVEPATH="`uci -q get pbx-voicemail.global_voicemail.global_save_path`"
  46. DATE="`date +%Y-%m-%d`"
  47. TIME="`date +%H:%M:%S`"
  48. FROM="voicemail@pbx"
  49. REPLY="do-not-reply@pbx"
  50. SUBJECT="Voicemail from $CALLERID, $DATE, $TIME"
  51. MSGBODY="$CALLERID has left voicemail for you on $DATE at $TIME."
  52. MIMETYPE="audio/wav"
  53. TMP1="/tmp/voicemail/tmpemail1.$$";
  54. TMP2="/tmp/voicemail/tmpemail2.$$";
  55. BOUNDARY="`date +%s | md5sum | awk '{print $1}'`"
  56. FILENAME="voicemail-$DATE-$TIME.WAV"
  57. # Clean up just in case.
  58. rm -f $TMP1 $TMP2
  59. append_to_file $TMP1 "From: $FROM"
  60. append_to_file $TMP1 "To: $TO"
  61. append_to_file $TMP1 "Reply-To: $REPLY"
  62. append_to_file $TMP1 "Subject: $SUBJECT"
  63. append_to_file $TMP1 "Content-Type: multipart/mixed; boundary=\""$BOUNDARY"\""
  64. append_to_file $TMP1 ""
  65. append_to_file $TMP1 "This is a MIME formatted message. If you see this text it means that your"
  66. append_to_file $TMP1 "email software does not support MIME formatted messages."
  67. append_to_file $TMP1 ""
  68. append_to_file $TMP1 "--$BOUNDARY"
  69. append_to_file $TMP1 "Content-Type: text/plain; charset=ISO-8859-1; format=flowed"
  70. append_to_file $TMP1 "Content-Transfer-Encoding: 7bit"
  71. append_to_file $TMP1 "Content-Disposition: inline"
  72. append_to_file $TMP1 ""
  73. append_to_file $TMP1 "$MSGBODY"
  74. append_to_file $TMP1 ""
  75. append_to_file $TMP1 ""
  76. append_to_file $TMP1 "--$BOUNDARY"
  77. append_to_file $TMP1 "Content-Type: $MIMETYPE; name=\"$FILENAME\""
  78. append_to_file $TMP1 "Content-Transfer-Encoding: base64"
  79. append_to_file $TMP1 "Content-Disposition: attachment; filename=\"$FILENAME\";"
  80. append_to_file $TMP1 ""
  81. append_to_file $TMP2 ""
  82. append_to_file $TMP2 ""
  83. append_to_file $TMP2 "--$BOUNDARY--"
  84. append_to_file $TMP2 ""
  85. append_to_file $TMP2 ""
  86. # Cat everything together and pass to msmtprc to send out.
  87. ( cat $TMP1
  88. cat "$ATTACHMENT" | base64
  89. cat $TMP2 ) | msmtp -t -C /etc/pbx-msmtprc
  90. # Clean up email temp files.
  91. rm -f $TMP1 $TMP2
  92. # Either delete or move the attachment based on the SAVEPATH variable.
  93. if [ -z "$SAVEPATH" ]
  94. then
  95. rm -f "$ATTACHMENT"
  96. else
  97. mkdir -p "$SAVEPATH"
  98. mv --backup=t "$ATTACHMENT" "$SAVEPATH/$FILENAME"
  99. fi