Blog Tips

Send e-Mail Notification When Someone Downloads

Shahjada Entrepreneur, Software Engineer, and…
April 20, 2014 1 min read

Some users  asked me how to Add Mail Notification When Someone Downloads and actually it is pretty easy. WordPress Download Manager has an action hook “before_download” , if you attach any function with that hook and write email notification code inside the functional that will work perfectly. Here is the example code:

 
function download_notification($package){
    $package_data = get_post($package['ID']);
    $headers = 'From: Site Name ' . "\r\n";
    $headers .= 'Content-type: text/html' . "\r\n";
    $message = "Downloader's IP: ". $_SERVER['REMOTE_ADDR'];
    wp_mail(get_option("admin_email"), "Download Notification: ".$package_data->post_title, $message, $headers); 
} 

add_action("before_download", "download_notification"); 

Simply add the above code block at the end of your theme functions.php. Here get_option("admin_email") will take the site admin email address, but you can replace it with any email address like wp_mail("your.email@somewhere.com", "Download Notification: ".$package_data->post_title, $message, $headers);
 

Share this article

Write a comment...

6 Comments

  1. millerandco

    Have done and is now working. Thank you.

  2. millerandco

    Do I need to purchase the Download Notification Add On?

    1. Shaon

      Yes, download notification will do the job.

  3. millerandco

    Thank you for responding.

    Sadly, it doesn’t work as it is causing an HTTP 500 Error. I am placing the code into the functions.php file.

  4. millerandco

    Hi. I’ve tried to implement this but it isn’t working.

    function download_notification($package){
    $package_data = get_post($package[‘ID’]);
    $headers = ‘From: Vanguard Healthcare Website ‘ . “\r\n”;
    $headers .= ‘Content-type: text/html’ . “\r\n”;
    $message = “Downloader’s IP: “. $_SERVER[‘REMOTE_ADDR’];
    wp_mail(“jvmiller@gmail.com”, “Download Notification: “.$package_data->post_title, $message, $headers);
    }

    add_action(“before_download”, “download_notification”);

    Any help would be appreciated.

    J