adding email notification to Mini AJAX File Upload

Good day programmers

I've gotten the Mini Ajax file upload script to work well on a site and am wondering if anyone has already modified it to send an email notification when something is uploaded? Thoughts on how easy it would be to do that?

Thank you for any guidance. Sean

Comments 6

  • Hello,
    You could at this lines to send email when the file was uploaded

    <?php
    $to      = '[email protected]';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: [email protected]' . "\r\n" .
        'Reply-To: [email protected]' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    
    mail($to, $subject, $message, $headers);
    ?> 

    If you need more help, just reply I will try to help you out.

    Best regards,
    Tamas

  • Tamas, thank you much.

    My son is starting to program in php and he's been working on the script to handle the email. It's working well. He added a link to the file that was uploaded in the email and replaces spaces in file names with "_".

    The one issue that I'm having is the "from" address is not being handled correctly by the server. Instead the email is coming from the server login and server name. Any thoughts? I've tried to reverse the order of the headers, eliminate the '.' that was in the first header call, and any change I make seems to cause no email to be received.

    Here's what it looks like now, any help appreciated.

    // next line replaces spaces in file names with underscore
    $newlink = strreplace(" ", "",$_FILES['upl']['name']);
    $to = "[email protected]";
    $subject = 'new file uploaded' ;
    $message = 'new file uploaded at www.servername.com/uploads/' . $newlink;
    $headers .= "Content-type: text/html; charset=iso-8859-1";
    $headers .= "From: [email protected] . \r\nReply-To: [email protected]";

    if(move_uploaded_file($_FILES['upl']['tmp_name'], '/home/serverlogin/public_html/uploads/' . str_replace(" ", "_",$_FILES['upl']['name']))){
        mail( $to, $subject, $message, $headers );
        exit;
    }

    }

    echo '{"status":"error"}';
    exit;

    ?>

    I did replace my son's header text with the one you provided. There were no php errors generated when the script executed, but the email didn't show up. Thanks for help. Sean

  • Hello,

    Can you please post the whole project
    I would like to test the whole script to see what happens.

    Thank you,
    Tamas

  • Here's the script in it's present mostly working form. To refresh, this DOES generate an email upon file upload, the only issue is that the "from" address is coming through as the user name and server name. The error file gets an error with the first call of the headers every time it runs.

    When I swapped out for the code you suggested, the file is uploaded, there is no error generated in the error file, but no email is received.

    I modified the script to place the uploaded file into a directory called "uploads" that is in the base html directory (NOT the directory that the upload script is contained), you'll need to create that directory to match my script or change it.

    If it easier to copy from a google doc instead of this thread, here's a link to the text of the upload.php script that is below:
    https://goo.gl/Jjwequ

    <?php
    /*
    This modified script facilitates uploading a file to a web server using AJAX
    and sending an email to notify of the upload.

    Original file upload script came from https://tutorialzine.com/2013/05/mini-ajax-file-upload-form and you need various files from that site to make this work.

    Edits to add emailing someone when an upload occurs as well as
    modifying file names to replace spaces was added by Andrew Caldwell

    with help by viewing coding by Bob Uehlein and the uploadify free script by Ronnie Garcia and Travis Nickels

    */

    // A list of permitted file extensions
    $allowed = array('png', 'jpg', 'gif','zip','mp3','wav','aif','mov','m4a','mp4');

    if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

    $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
    
    if(!in_array(strtolower($extension), $allowed)){
        echo '{"status":"error"}';
        exit;
    }

    // next line replaces spaces in file names with underscore
    $newlink = strreplace(" ", "",$_FILES['upl']['name']);

    // below is old code that worked but not with correct FROM address showing up in email

    $to = "[email protected]";
    $subject = 'new file uploaded at websitename.com' ;
    $message = 'new file uploaded at www.websitename.com/uploads/' . $newlink;
    $headers .= "Content-type: text/html; charset=iso-8859-1";
    $headers .= "From: [email protected] . \r\nReply-To: [email protected]";

    // headers to try out from Tamas.
    // $to = '[email protected]';
    // $subject = 'new file uploaded now at websitename.com';
    // $message = 'new file uploaded at www.websitename.com/uploads/' . $newlink;
    // $headers = 'From: [email protected]' . "\r\n" .
    // 'Reply-To: [email protected]' . "\r\n" .
    // 'X-Mailer: PHP/' . phpversion();

    if(move_uploaded_file($_FILES['upl']['tmp_name'], '/home/webservername/public_html/uploads/' . str_replace(" ", "_",$_FILES['upl']['name']))){
        mail( $to, $subject, $message, $headers );
        exit;
    }

    }

    echo '{"status":"error"}';
    exit;

    ?>