PureFTP that send email notification on Upload

Discussion in 'Suggest HOWTO' started by almeister9, Nov 20, 2009.

  1. almeister9

    almeister9 Member

    [RESOLVED] PureFTP that send email notification on Upload

    Hi All,

    I am trying to piece together little snippets from all over the place to get this to work and I just cant get it to work.

    What I want is a stand alone FTP server that sends an email to me when a file is uploaded.

    I have started by building an Ubuntu 9.04 server as per Falkos:
    http://www.howtoforge.com/perfect-server-ubuntu-9.04-ispconfig-2
    up to step 10 and then adding postfix

    Then installing Pure-FTP as per Falkos:
    http://www.howtoforge.com/virtual-h...quota-and-bandwidth-management-on-ubuntu-9.04

    I then try to enable the pure-uploadscript by donig this (found on the net):
    Step 1:
    vi /etc/default/pure-ftpd-common
    Run Pure-FTPd in standalone mode (not inetd mode).
    Change the option "STANDALONE_OR_INETD" to "STANDALONE" in

    Step 2: Tell Pure-FTPd the user ID under which the script should be run. Pure doesn't like usernames, so you have to provide the numeric UID. In case of www-data it is UID 33.
    # if set, pure-uploadscript will spawn $UPLOADSCRIPT running as the # given uid and gid UPLOADUID=2001
    UPLOADGID=2001
    Step 3: Tell pure-ftpd where the script is located: Change the option UPLOADSCRIPT in /etc/default/pure-ftpd.
    # example: UPLOADSCRIPT=/usr/local/sbin/uploadhandler.pl UPLOADSCRIPT=/usr/local/sbin/uploadhandler.pl

    Step 4: Enable the option itself:
    echo "yes" > /etc/pure-ftpd/conf/CallUploadScript
    Step 5: Start or Restart Pure-ftpd
    /etc/init.d/pure-ftpd-mysql restart

    And then I made my first ever shell script which went like this:
    #!/bin/bash
    #
    echo "$1 uploaded" | mail -s "New Upload : $1" [email protected]

    but It doesn't work.

    FTP Server part of things works fine.

    The only function that the mailer side of this needs to be able to do is send me an email when a file is uploaded. It needs no incoming mailboxes or anything like that.
    The Server is a FTP server and nothing else.
    It sits in our office so it could even send me a message across the network if that is possible, but I use a windows machine(Windows 7) and a mac (OSX 10.4) as a pair of work stations.

    If someone could show me a concise way of istalling Pure-FTP on an Ubuntu 9.04 server with upload notification, I would be very appreciative.

    Cheers AL.
     
    Last edited: Nov 24, 2009
  2. falko

    falko Super Moderator Howtoforge Staff

    Have you tried to use the full path for the mail command?
     
  3. almeister9

    almeister9 Member

    I'm sorry Falko, but I don't know what the full path for the mail command is.

    I am trying to learn about the linux world but I find man pages especially unhelpful for newbs and information on this topic, I have found particulary difficult to find.

    I am about to start trying the snail program or ssmtp. As far as I can tell, these don't need a mail server installed on the system, they just use an external mail server and account. (It will also make me feel safer not having a mail server on the system to worry about being compromised. I don't know enough to make it secure.)

    I am hoping that I can find an example of a script that uses one of these programs that will work for me or that I can some how work it out by gathering clues.
     
  4. almeister9

    almeister9 Member

    The Ultra Elusive pure-uploadscript(8)

    I have now started again from scratch.
    I have an Ubuntu 9.04 32-bit server as Falko's:
    http://www.howtoforge.com/perfect-server-ubuntu-9.04-ispconfig-2-p4
    up to stage 10.
    with a working Pure-ftpd server as per Falkos:
    http://www.howtoforge.com/virtual-h...ta-and-bandwidth-management-on-ubuntu-9.04-p2

    I then installed ssmtp:
    sudo apt-get install ssmtp

    and configured:
    sudo vi /etc/ssmtp/ssmtp.conf

    Code:
    [email protected]
    mailhub=smtp.gmail.com:587
    AuthUser=mygmailusername
    AuthPass=mypassword
    UseSTARTTLS=YES
    Then by typing into terminal:
    ssmtp [email protected] and hitting enter, then
    To: [email protected]
    From: [email protected]
    Subject: test email

    hello world!

    and then crtl+D it successfully sends a mail via my gmail account.

    Code:
    I edit Pure-ftp conf file:
    vi /etc/default/pure-ftpd-common
    
    and make sure option "STANDALONE_OR_INETD" is "STANDALONE"
    set
    UPLOADUID=0 
    UPLOADGID=0
    (because it said to on the man page)
    and:
    UPLOADSCRIPT=/usr/local/sbin/uploadscript.sh
    
    All that above I got from one of the very few sources I could find on the internet and it basically said "then create a script that does something useful and you are done.
    This did not eaxctly help me.
    I persisted:
    vi /usr/local/sbin/uploadscript.sh
    Code:
    #! /bin/sh
    echo "$1 uploaded" | ssmtp [email protected]
    To:[email protected]
    From: [email protected]
    Subject: New File Uploaded
    
    "$1 uploaded"
    No Dice
    I found another source that gave a small amount of help:http://blog.derjohn.de/space/start/2006-11-14/1
    which said:
    Step 6: Check if the daemon really run with the new options. (Doesn't say how)
    Code:
    root 18623 0.0 0.0 12120 868 ? Ss 19:39 0:00 pure-ftpd (SERVER) www-data 18671 0.0 0.0 11912 672 ? Ss 19:40 0:00 /usr/sbin/pure-uploadscript -r /usr/local/sbin/uploadhandler.pl -B -u 33 -g 33
    Don't know what that means
    Step 7: Create a test script. E.g. this one will write the filename to a file called /tmp/pure-was-here<datestamp>.
    Code:
    #!/bin/sh echo "$1" > /tmp/pure-was-here$(date +%Y%m%d%H%M%S)
    Step 8: Test the script, log in via ftp and upload a file:
    I tried that and uploaded a file but the file in /TEMP/ doesn't get created.


    I have read the man page for pure-uploadscript over and over but it doesn't seem to tell me how to do anything, just what it can do.

    I assume that what I need to do now is:
    1). Learn how to create a shell script and then place it somewhere and tell the conf page where it is (UPLOADSCRIPT=)
    2). Learn how to get said shell script to use ssmtp to send an email using the available things passed from uploadscript ($1 and others ???)
    3). Prey that it works because if it doesn't I have no where to turn.

    I feel I am so close now,

    Any help that any kind soul could lend me would be greatly appreciated.

    Cheers Al.
     
  5. falko

    falko Super Moderator Howtoforge Staff

    What are the outputs of
    Code:
    which mail
    and
    Code:
    which ssmtp
    ?
     
  6. almeister9

    almeister9 Member

    Outputs

    Hi Falko,
    Thanks for looking at this for me.

    The output of "which mail" is nothing.

    The output of "which ssmtp" is:
    /usr/sbin/ssmtp

    When I started again from scratch, I built it with just the server and Pureftp, then installed ssmtp. Thats why "which mail" is empty.

    Following your first advice
    would that mean that the script should read:
    Code:
    #!/bin/bash
    #
    echo "$1 uploaded" | /usr/sbin/ssmtp [email protected]
    To: [email protected]
    From: originating@address
    Subject: "New Upload : $1"
    
    Body text with hopefully some of the other arguments passed from the pure-uploadscript
    And also, does the script need to have a .sh file extension?

    Thanks again for any help you can give me with this.
    Cheers Al.
     
    Last edited: Nov 23, 2009
  7. almeister9

    almeister9 Member

    A little further

    I have made a little more progress.
    I have changed /etc/default/pure-ftpd-common to read
    UPLOADUID=2001
    UPLOADGID=2001
    This is the userID of pureftpd.
    I then made a script called /usr/sbin/upload2.sh which has the following code:
    Code:
    #!/bin/sh
    echo "$1" > /tmp/pure-was-here$(date +%Y%m%d%H%M%S)
    I then did this
    Code:
    chmod +x upload2.sh
    and restarted Pureftpd.
    I then uploaded a file to the ftp server and went to /tmp and the file was there!! YAY!
    So I made a txt file called /usr/sbin/msg1.txt which contains:
    Code:
    To: recipient@address
    From: Originating@address
    Subject: Test from FTP Server
    
    This is a test
    then created a script called /usr/sbin/upload1.sh which contains:
    Code:
    ssmtp [email protected] < msg1.txt
    and I ran
    Code:
    chmod +x upload1.sh
    then restarted PureFTPd.

    This however does not work.
    If I move to the containing folder
    cd /usr/sbin
    and type ./upload1.sh
    The email IS sent.

    I feel I am very close now but it is still not working.
     
  8. almeister9

    almeister9 Member

    Success

    Using this script I found at http://www.axllent.org/docs/networking/ssmtp_on_the_console

    Code:
    #!/bin/bash
    CURDATE=`date`
    ssmtp -oi [email protected] << EOF
    From: FTP Server <[email protected]>
    To: [email protected]
    Subject: File uploaded to FTP SERVER on $CURDATE
    
    The file $1 was uploaded to the FTP-SERVER on $CURDATE.
    
    EOF
    $1 is the argument passed from pure-uploadscript(8) so the email I got says:

    The file home/www.example.com/ST15_01.pdf was uploaded ...

    YAY double YAY
    and thanks to axllent.org for the script
     

Share This Page