Guide : Sending Notification and Status mails from a Torrent Client

asingh

Staff member
Super Mod
Many a times we want some sort of status updates from our Torrent clients. Bittorent clients have a provision of executing/sending command line prompt signals to the Windows OS. Kind of similar to how the Task Manager works on the Windows layer, i.e. it is constantly polling vs. the internal clock, and can seamlessly execute. The task today, I will highlight is: How to send an email from a torrent client to an external entity from the system without using a Windows based program or an install. So following this basic guide and the links/tools provided you should be able to do so. Here we go:

You will need the following:

1. Bittorrent client. The latest is ver. 7.8; but I have tested this to work just fine on my 7.5 build too.
2. Powershell as per your Windows BIT flavor. Win7 has this as a default, for XP you may need to download the framework. This is a MS enhancement, and works kind of similar to the CMD prompt which we all use.
3. A web based email account which allows SMTP access and has exposed ports. Gmail, by choice, is the best option here. I would suggest, you create a new account just for this, cause we will be sending credentials to the email SP.
4. A .torrent file which is really small in size, and has a high number of seeds (something which can complete in 30-40 seconds). This is for testing purposes. Of course, I cannot give any links for this. But if you are reading this, you know what I mean.

Explanation of methodology and architecture:
We are going to use the Windows Powershell to send email parameters to a free email service, which in turn will be a notification. The email will be triggered when a certain event is completed on a torrent client. So moving forward we prepare our system for this to be possible.

Step 1: Configuring Powershell to allow scripts to run from the host machine.

You will need to go to RUN and type in 'powershell' [move these to shortcuts if need be]; and configure for local scripting. These are the versions and tools available. If you are on a 64BIT machine, you get both x86 and 64BIT default.

1_Powershell_Launcher_zpsf482ddf0.jpg



The first two are the Powershell executables and the latter two, are editors, which we can use for testing. Upon clicking the first link, you shall see a window like this. The x86 and 64BIT look the same, but the toolbar title on the top, will tell which is which version. This is important, as you will understand later on.

2_Powershell_zps3ec5022c.jpg


First of all, we are going to set the execution levels for scripting. There is a high probability, if you have not used Powershell scripts before on the host system, it is going to be restricted. We check the following, by typing in the command: get-executionpolicy. You will see the status below as, RESTRICTED. Which basically means, that script execution via the Powershell Windows module is disabled on the system. We are now going to change it: So the host machine can execute scripts. You will need to type in the command: set-executionpolicy remotesigned. It will ask for a verification, and upon that, the policy will be implemented to the host system. You should be logged in as ADMIN to do all this. You can verify that the change has been committed by typing in: get-executionpolicy, and it should report in fine. Below are all the steps, outlined. You will notice, that 'remotesigned' is now enabled. Ideally you should only do this for the installation version of your torrent client. So if you installed it in the x86 folder, set scripting for x86 domain. Respectively for 64BIT. Just for security. Do remember, both these domains are independent.

3_Powershell_zps8c4ba3ea.png


After doing the above steps, our system is ready to allow the Windows Powershell for scripting, and external elements are still restricted.

Step 2: Writing up the Powershell script for sending emails from the host system.

We are going to use a standardized script to send credentials to an email provider, and in turn initiate a notification. The script looks like this:

param ([string]$w)
$EmailFrom = “**********@gmail.com”
$EmailTo = “*******************”
$Subject = “Email for Torrent Status”
$Body = “A torrent has just completed on your client and it is called $w"
$SMTPServer = “smtp.gmail.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“*****************@gmail.com”, “************”);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

Explanation of terms is as:
param ([string]$w) : Is a parameter/variable we are going to pass from the torrent client to this script. This can be the name of the torrent completed/status changed.
$EmailFrom : Where the email has come from.
$EmailTo : Where the email is going to go. The recipient.
$Subject : Subject line for the email. [Can be appended with the dynamic parameter].
$Body : The body text. [Can be appended with the dynamic parameter].
$SMTPServer : The smtp address of the email provider.
$SMTPClient : The port details, and method being used.
$SMTPClient.EnableSsl : If SSL should be maintained. The port is specific respectively.
$SMTPClient.Credentials : The actual credentials for the email account being used.

Best is to copy the above script in notepad and do the changes. Note, where I have uses quoted "********"; that type cast should be maintained. I did not test without the quotes.

Step 3: Testing the Script.

Testing the script is fairly simple. After you have made the changes in notepad, just copy them to the clipboard. Open up a Powershell session, and press right click. The contents will be dumped immediately to the window. Press Enter...! The script will run. For some reason, even if your system policy is RESTRICTED, the script will go through. For verification, check your target mail box. It should be there. It will look like this, the cursor will blink, and the CLP will be enabled. again.

4_Powershell_zpsc0b4c685.jpg


After this step, the script is going to be tested for two integral functions:

1. System policy restrictions.
2. Ability to pass parameters to the script.

For the first test, you can copy the working script to a notepad file, and as it is: save it in "ps1" format. Save the file as .txt, and change the file extension to "ps1". Else you can launch the Powershell [screenshot 1] editor (remember the BIT version you are working with); and paste in the working script. Subsequently, you can save it as a ps1 file. It will look like this:

5_Powershell_zps6f7f8c3a.png


Now to test the system authorization, we are going to call this Powershell script using a batch file. Using the below syntax:

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe F:\Email1.ps1


PAUSE

You can create a batch file. The red portion is the full path of where the Powershell module is installed on your system (remember the BIT version you are working on); and the blue portion is where the ps1 file is saved. Save the above as a .txt notepad file, and change the extension to .BAT. That will convert the file to a batch executable. Double click the file. If policies are correct, you should get a mail, else you will see an error like this:

6_Powershell_zpse204d4cf.png


Once you are receiving mails using the batch file, we will test if parameters can be passed. Modify the batch file syntax as below, and re-save it:

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe F:\Email1.ps1 'this is a parameter I want to pass'


PAUSE

The burnt sienna color is, the dynamic parameter we are sending, to test if our script can receive and send. Double click your batch file, and check the target in box. It should look like the below screenshot, see the highlighted portion. We are able to pass information to our script, which can be relayed forward.

7_Powershell_zps825a560e.png


Once the batch file is working, we are in the green, and all is left, is to migrate this working script to our torrent client.

Step 4: Setting up the Torrent Client for notifications:

This is a real simple process, and hardly takes time. All you need to do is, on the torrent client go to: Options>>>>>Preferences>>>>>Advanced>>>>>Run Program. You will see an interface like this. The highlighted portion is important to us. That is where the command line is going to go, for the notifications to execute.

8_Powershell_zpsecb0970d.jpg


And obviously, since we tested out our script using a batch protocol it will easily work on the torrent interface. All you need to do is, open up the .bat file (right click>>>edit); and paste the details in the area highlighted above. NOTE: Do note paste the dynamic parameter. The torrent client is going to supply this variable. The bittorent client offers these parameters:

You can use the following parameters:

%F - Name of downloaded file (for single file torrents)
%D - Directory where files are saved
%N - Title of torrent
%P - Previous state of torrent
%L - Label
%T - Tracker
%M - Status message string (same as status column)
%I - hex encoded info-hash
%S - State of torrent
%K - kind of torrent (single|multi)

So the line which "can be" pasted to the above window is:

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe F:\Email1.ps1 %N

In this case, I am passing the title of the torrent (highlighted in red). Hit, apply and load the small .torrent file (I mentioned this as a requirement). You should receive an email. Note here: the batch file is not being used, and the Powershell command is being executed directly.

Hope this helps you all to be more aware of the status of your torrent files. And remember: ALWAYS SEED BACK.

Note:
If anyone needs help setting this up, please post in this thread.

:)
 
A very good effort and nice write up. I remember wanting something similar to this and used Sendmail.exe along with uTorrent to achieve the same result. While PowerShell isn't everyone's cuppa tea, Sendmail is fairly simple.
My only gripe with this whole setup and what I did was; the gmail credentials in clear text. I mean its a disaster waiting to happen. Else its just awesome !!
 
A very good effort and nice write up. I remember wanting something similar to this and used Sendmail.exe along with uTorrent to achieve the same result. While PowerShell isn't everyone's cuppa tea, Sendmail is fairly simple.
My only gripe with this whole setup and what I did was; the gmail credentials in clear text. I mean its a disaster waiting to happen. Else its just awesome !!

I had tried SendMail, and it never worked for me. Not sure. That is why I made the write detailed, so people can sequentially follow. If you would have noticed, I recommended creating a proxy mail account just for this purpose. Thanks, for the feedback though.
 
Wouldn't it be simpler to just setup the webinterface and then remotely open/manage it?

True, could be. This is an option one can exercise. :) It will probably take 5-10 minutes to do it. The only place where someone can really mess up, is the file - pathing.
 
I dont think option of running program on download complete is there in Transmission. I searched all tabs and settings and didn't find it.
 
I had tried SendMail, and it never worked for me. Not sure. That is why I made the write detailed, so people can sequentially follow. If you would have noticed, I recommended creating a proxy mail account just for this purpose. Thanks, for the feedback though.

Having worked as a System Admin for quite sometime, I kinda feel enraged when passwords are shared in clear text. You just get into that mode, one cannot help it. :bleh:
On a different note, looking at the logic of getting an email, I think if one receives an email he can as well log into an Web Interface and check the torrents. SMS OTOH.... could not figure it out tho...
 
Having worked as a System Admin for quite sometime, I kinda feel enraged when passwords are shared in clear text. You just get into that mode, one cannot help it. :bleh:
On a different note, looking at the logic of getting an email, I think if one receives an email he can as well log into an Web Interface and check the torrents. SMS OTOH.... could not figure it out tho...

It depends. The notification is a push. Checking it yourself on a client is pull. Ones own choice. :) Will try tonight to compile the ps1 into an EXE. That way the passwords can be more restrictive.
 
Back
Top