Since I cannot attach the script, it follows below.
So copy the script and save it as "subsonic-mail-new-releases.php", for the rest follow instructions within the script.
- Code: Select all
<?php
####################################################################################
# Script to inform your subsonic users about new releases on your subsonic site #
# Independent from your choice of database, uses the REST api #
# Developed by Cas Nuy, www.NUY.info, June 2020 #
# Feel free to adjust #
####################################################################################
# Installation instructions: #
# Copy this script into the root of your webserver #
# You can run it manually by typing php subsonic-mail-new-releases.php #
# Even better, setup a Cron-job or Task to run the script automatically #
####################################################################################
## Definitions #
####################################################################################
#
## define the url of your subsonic installation ( including port and with trailing slash )
#
$subsonic = "http://www.mysubsonic.nl:4040/";
#
## define userid/password for retrieving data (must have admin rights within subsonic
#
$user = "admin";
$pwd = "password";
#
## define mail settings
## if you add a value for $bcc here, users will not receive an email ( usefull for testing)
#
$bcc = "";
$from = "xx@xgmail.com";
$to = "xx@xgmail.com";
$subject = "Music-News from my Subsonic site";
#
## define rest version
#
$version = "1.9.0";
#
## define number of days to look back for new releases
#
$his = 7;
#
## max albums to check ( no more than 500, limitation of the REST api )
#
$max = 250;
####################################################################################
## No changes required below thiese lines - No changes required below thiese lines #
####################################################################################
$adjust = "-".$his." days" ;
$curdate = date('Y-m-d', strtotime("$adjust"));
$send = false;
$returnpath = "-f$from" ;
$headers = "From: $from"."\r\n";
$headers .= "Return-Path: $from"."\r\n";
// first create url for retrieving users
// someting like http://subsonic.com:4040/rest/getUsers?v=1.9.0&u=user&p=password&f=xml&c=myapp"
$usr_url = $subsonic ;
$usr_url .= "rest/getUsers?v=";
$usr_url .= $version ;
$usr_url .= "&u=" ;
$usr_url .= $user ;
$usr_url .= "&p=" ;
$usr_url .= $pwd ;
$usr_url .= "&f=xml&c=myapp";
// Next create the url for retrieving albumsi
// some thing like http://subsonic.com:4040/rest/getAlbumList2?v=1.9.0&u=user&p=password&f=xml&c=myapp&type=newest&size=50
$album_url = $subsonic ;
$album_url .= "rest/getAlbumList2?v=";
$album_url .= $version ;
$album_url .= "&u=" ;
$album_url .= $user ;
$album_url .= "&p=" ;
$album_url .= $pwd ;
$album_url .= "&f=xml&c=myapp&type=newest&size=";
$album_url .= $max ;
// now we retrieve the xml's and convert those into array's
$usr_xml = simplexml_load_file($usr_url);
$album_xml = simplexml_load_file($album_url);
// create address list as BCC
if ($bcc == ""){
foreach($usr_xml->users as $list) {
foreach($list->user as $user) {
$bcc .= $user['email'];
$bcc .=",";
}
}
}
// Add BCC
$headers .= 'BCC: '. $bcc . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1";
// Create the html message
$message ="<html><body>";
$message .= $subject." \n\n";
$message .= "<table><tr>";
$message .= "<td><b>Genre</td><td><b>Artist</td><td><b>Album</td><td><b>Released</td> <td><b>Date Uploaded</td></tr></b>";
foreach($album_xml->albumList2 as $list) {
foreach($list->album as $album) {
$uploaded = substr($album['created'],0,10) ;
$limit = date( "Y-m-d", strtotime($uploaded));
if ( $limit > $curdate ){
$message .= "<tr><td>";
$message .= $album['genre'];
$message .="</td><td>";
$message .= $album['artist'];
$message .="</td><td>";
$message .= $album['name'];
$message .="</td><td>";
$message .= $album['year'];
$message .="</td><td>";
$message .= substr($album['created'],0,10). "</td></tr>";
$send = true;
}
}
}
$message .= "</table></body></html>";
// email fields: to, from, subject, and so on, only if there is something to send
if ($send){
$ok = mail($to, $subject, $message, $headers, $returnpath);
}
return $ok ;