Custom email and SMS notifications

I made a script that will send an email or SMS notification with a custom message. It’s only for Linux but Windows 10 users might be able to use the Linux command line feature to use it but I have not tested it. It uses curl to send email since my Sonarr runs in a docker that does not have extra tools like PHP. Just change the variables as needed. Here is the script:

#!/bin/bash

template="/config/scripts/template.txt"
receiver="youremail@gmail.com"
sender="myemail@gmail.com"
smtp="smtps://smtp.gmail.com:465"
apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
sonarr_address="http://localhost:8989"
netrc="/config/scripts/.netrc"

sonarr_episode_title=$(curl -s $sonarr_address/api/episode?seriesId=$sonarr_series_id --header "X-Api-Key:$apikey" | jq -r ".[] | select(.episodeFileId==$sonarr_episodefile_id) | .title")
sonarr_episode_description=$(curl -s $sonarr_address/api/episode?seriesId=$sonarr_series_id --header "X-Api-Key:$apikey" | jq -r ".[] | select(.episodeFileId==$sonarr_episodefile_id) | .overview")

eval "cat <<EOF
$(<$template)
EOF
" 2> /dev/null | curl -s --netrc-file $netrc --ssl-reqd --mail-from $sender --mail-rcpt $receiver --url $smtp -T -

And here is a sample template:

From: "Media Server" <myemail@gmail.com>
To: "Me" <youremail@gmail.com>
Subject:

Hey, there's a new episode of "$sonarr_series_title" available to watch:

Season $sonarr_episodefile_seasonnumber episode $sonarr_episodefile_episodenumbers

$sonarr_episode_title

$sonarr_episode_description

A couple of notes:

  1. Username and password for the email account is stored in .netrc file.

  2. Multiepisode files will give “null” for the description because I did not bother accounting for that circumstance. I don’t have the time nor desire at the moment.

  3. To send SMS, you need to use your carrier’s email to SMS feature. Read here:
    http://www.digitaltrends.com/mobile/how-to-send-e-mail-to-sms-text/
    I would recommend using the MMS gateway as SMS gateways have more restrictions such as character limit. This usually means you cannot send episode description. For an example, sending an email to 5551234567@vzwpix.com will send a message to that phone number. Also, send a blank subject if using SMS.

  4. The two variables “sonarr_episode_title” and “sonarr_episode_description” require you download jq (https://stedolan.github.io/jq/), as it will parse the value from the Sonarr API for those. If you don’t want to use those variables in your notification, just comment those lines out and don’t bother downloading jq. You also won’t need to change the API variable or Sonarr address variable I know I am querying the API twice, once for each variable, instead of just once and then parsing it and assigning it to two variables but I couldn’t get it to work and I don’t yet have the time to fix it. Feel free to school me if you get it figured out.

  5. The “to” and “from” isn’t strictly needed in the template as those are in the Curl command. They are especially not needed for SMS but for an email it will let you personalize the name of the sender and receiver.

You can have multiple templates and multiple copies of the script with different settings for different users. That way you can have custom notifications for multiple users. Let me know if anyone has any questions or issues.

2 Likes

This is great, thanks!!

I’ll ask you because you seem knowledgeable - is it possibile to create a custom Telegram notification which sends only if the original filename picked up by sonarr had amzn or deflate in it?

Eg TV.Show.Title.1080P.AMZN.WEBRIP.DDP5.1.x264-releasegroup

1 Like