Last active
January 5, 2023 15:54
-
-
Save gioiliop7/b18e5f79f4fc6c5c93f58595139da1f1 to your computer and use it in GitHub Desktop.
[PHP][WORDPRESS] Donwload a file from external url and attach it to mail and remove it (help with email function send_email_attachment)
This file contains hidden or 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
<?php | |
function send_email_attachment($to, $subject, $template,$render_object,$attachments) | |
{ | |
$headers = array('Content-Type: text/html; charset=UTF-8', 'From: SiteName <noreply@siteDomain>'); | |
$GLOBALS["use_html_content_type"] = TRUE; | |
ob_start(); | |
require(locate_template($template)); | |
$body = ob_get_contents(); | |
ob_end_clean(); | |
wp_mail($to, $subject, $body, $headers,$attachments); | |
} |
This file contains hidden or 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
<?php | |
/* | |
* @author Giorgos Iliopoulos | |
*/ | |
$email = 'To email'; | |
$subject = 'Your title'; | |
$template = 'Path to your email template'; | |
$link = "Your Download Link"; | |
$ch = curl_init(); | |
$source = $link; | |
curl_setopt($ch, CURLOPT_URL, $source); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
$filename = 'Your filename'; | |
// If we can't download the file (False Link) | |
if (str_contains( $data, 'Could not find' )){ | |
// Do something | |
} | |
$destination = "Absolute path to the folder you want the file to be downloaded"; | |
$file = fopen($destination, "w+"); | |
fputs($file, $data); | |
fclose($file); | |
$attachments = array(ABSPATH . 'absolute-path-to-folder/'.$filename.'.{file extension}'); // | |
send_email_attachment($email, $subject, $template, 'render_sth_for_template', $attachments); | |
$path = ABSPATH. 'absolute-path-to-folder/'.$filename.'.tiff'; | |
unlink($path); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment