Last active
November 9, 2022 07:09
-
-
Save rafiahmedd/168f4e25e6c0210dec37c2c646e4b769 to your computer and use it in GitHub Desktop.
Email Attachment from a external link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter('fluentform_filter_email_attachments', function($emailAttachments, $notification,$form, $submittedData){ | |
$target_form_id = 38; | |
if($form->id != $target_form_id){ | |
return; | |
} | |
$url = "your_file_location_url"; | |
$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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment