Skip to content

Instantly share code, notes, and snippets.

@rafiahmedd
Last active September 10, 2021 08:51
Show Gist options
  • Save rafiahmedd/11087c38c49d8a20dedb781fba6dc9ee to your computer and use it in GitHub Desktop.
Save rafiahmedd/11087c38c49d8a20dedb781fba6dc9ee to your computer and use it in GitHub Desktop.

Revisions

  1. rafiahmedd revised this gist Sep 10, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions EmailAttachmentfromAExternalLink.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    add_filter('fluentform_filter_email_attachments', function($emailAttachments, $notification,$form, $submittedData){

    $target_form_id = 38;
  2. rafiahmedd created this gist Sep 10, 2021.
    42 changes: 42 additions & 0 deletions EmailAttachmentfromAExternalLink.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    add_filter('fluentform_filter_email_attachments', function($emailAttachments, $notification,$form, $submittedData){

    $target_form_id = 38;
    if($form->id != $target_form_id){
    return;
    }

    $url = "https://www.australianshiatsucollege.com.au/documents/Prospectus.pdf";
    $tmp = download_url($url);
    if (is_wp_error($tmp)) {
    return 'Error while downloading';
    }
    $post_id = 0; // set 0 for no parent post id or simple attachment otherwise pass post id for include in post
    $desc = "Your Pdf";
    $file_array = array();

    // Set variables for storage
    // fix file filename for query strings
    preg_match('/[^?]+.(jpg|jpe|jpeg|gif|png|pdf)/i', $url, $matches);
    $file_array['name'] = basename($matches[0]);
    $file_array['tmp_name'] = $tmp;

    // If error storing temporarily, unlink
    if (is_wp_error($tmp)) {
    @unlink($file_array['tmp_name']);
    $file_array['tmp_name'] = '';
    }

    // do the validation and storage stuff
    $id = media_handle_sideload($file_array, $post_id, $desc);

    // If error storing permanently, unlink
    if (is_wp_error($id)) {
    @unlink($file_array['tmp_name']);
    return $id;
    }

    $src = wp_get_attachment_url($id);
    $emailAttachments[] = get_attached_file($id);
    return $emailAttachments;

    }, 10, 4);