Page 1 of 1

[Updated] How to: Send email when video is played

PostPosted: Wed May 02, 2012 7:39 pm
by nutt318
EDIT (10/15/2012): I've updated the code a little bit, it should work better than before. This is test on SubSonic ver. 4.7
This tutorial will show you how to modify SubSonic to send an email when a video file is played along with the user and IP address. This is my first one so be nice if there are any errors.

Currently this doesn't work in IE9, if someone knows why I would like to hear.

Edit playAddDownload.jsp (yours may be different but here is mine) - located in C:\subsonic\jetty\2583\webapp\WEB-INF\jsp
Add the following code to the top of the file

Code: Select all
<script>
function sendMail(video, user) {
var xmlhttp;
         if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
         } else {// IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.open("GET", "http://www.yourdomain.com/somedirectory/sendemail.php?video="+video+"&user="+user+'&ip=<%= request.getHeader("X-Real-IP") %>', false); // Use this if using a reverse proxy
         xmlhttp.open("GET", "http://www.yourdomain.com/somedirectory/sendemail.php?video="+video+"&user="+user+"&ip=<%= request.getRemoteAddr()) %>", false); // Use this if using a NAT on your firewall
         xmlhttp.send();
}
</script>


Then in the same file look for this chuck of code, around line ~54
Code: Select all
            <a href="${videoUrl}" target="main">


Make it look like this
Code: Select all
            <a href="${videoUrl}" target="main" onclick="sendMail(parentElement.parentElement.getElementsByTagName('span')[2].attributes['title'].value, '${model.user.username}')">


Now hopefully you have a hosting provider that supports .php or you could use your local machine. Create a .php file called sendemail.php and put it on your hosting account like in the above code. In this .php file you will want to put the following code.

Code: Select all
<?php
$videoplaying = $_GET['video'];
$username = $_GET['user'];
$ip = $_GET['ip'];
$to = "youremail@email.com";
$subject = "Video Streaming";
$message = $username . " has just started watching " . $videoplaying . " from IP Address: " . $ip;
$from = "subsonic@yourdomainname.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

Re: How to: Send email when video is played

PostPosted: Wed May 02, 2012 10:00 pm
by GJ51
Great job getting this figured out. This would be a great feature to incorporate into a future release. It would be nice to be able to select email notifications to the admin or designated user with a range of trigger options based on who/what the admin wants to monitor, perhaps by username, media type, ip address, and whatever else makes sense to ensure that your site isn't being abused or compromised.

I'll link this post to an entry in the feature request section.

EDIT: Please comment in the feature request forum if you have interest. Here's the link:

viewtopic.php?f=3&t=9444

Re: [Updated] How to: Send email when video is played

PostPosted: Mon Oct 15, 2012 2:56 pm
by nutt318
Guide has been updated.

Re: [Updated] How to: Send email when video is played

PostPosted: Sat Dec 08, 2012 12:17 am
by qtip920
Maybe a dumb question. But does this only work if the user is using IE?

Re: [Updated] How to: Send email when video is played

PostPosted: Sat Dec 08, 2012 12:23 am
by nutt318
Haven't tested this in a while (currently on my phone) but it should work just fine with Firefox and chrome. IE was having problems. I'll have to retest and post what I find.

Re: [Updated] How to: Send email when video is played

PostPosted: Sat Dec 08, 2012 12:35 am
by qtip920
All my users are using Chrome. I've been trying to get this to work. But can't seem to figure it out.

Re: [Updated] How to: Send email when video is played

PostPosted: Sat Dec 08, 2012 12:42 am
by nutt318
Hmm, I can try to help you out. Do you use gmail, easier to chat instead of post in the forum. PM me you contact if you want.

Re: [Updated] How to: Send email when video is played

PostPosted: Sat Dec 08, 2012 4:23 pm
by qtip920
I actually got it working. Thanks for the offer tho and thanks for the awesome mod.

Re: [Updated] How to: Send email when video is played

PostPosted: Sat Dec 15, 2012 3:20 am
by gurutech
How would I code this to use my Gmail account as an SMTP relay? I run SS on my own computer, and my ISP blocks port 25, so I have to use an SMTP relay through my Gmail account to send mail from my computer.

Will this just pick up the settings automatically (use what's built in to my Linux configuration), or is there a setting I need to adjust somewhere?