[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1281
  • Last Modified:

PHP: Email a PDF from a URL

Hi All,

Our intranet has a PDF report that is created every time its accessed.

To save on server resources I want to email it to several users at 2am every night.

The server has Xapps installed and I've tested PHP mail(), but cant work out how to attach the PDF from a URL


All help is welcome.


Thanks in advance
D
0
detox1978
Asked:
detox1978
  • 3
  • 2
2 Solutions
 
Giovanni HewardCommented:
You'll want to download the PDF to the local disk first, then attach it from there.

This can be done with Curl.

After the email is sent successfully, you can then remove the PDF from the disk.
0
 
detox1978Author Commented:
can you give me a hand with the code?


thanks
0
 
Giovanni HewardCommented:
Sure, is the internal location 80/TCP or 443/TCP?  Is the PDF download a GET or POST request?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Giovanni HewardCommented:
Here's an example GET request over HTTP:

$ch = curl_init("http://www.example.com/report.pdf");
$fp = fopen("report.pdf", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);

Open in new window


Or, if fopen wrappers are enabled in your PHP conf:

$file = 'http://example.com/report.pdf'; // URL to the file
$contents = file_get_contents($file); // read the remote file
touch('report.pdf'); // create a local EMPTY copy
file_put_contents('report.pdf', $contents);

Open in new window


You'll then attach the local report.pdf to your mail function.  

$attachment = chunk_split(base64_encode(file_get_contents('report.pdf')));

Open in new window


You could add extra code to verify a minimum file size, etc. before proceeding to email.

You may want to consider PHPMailer or Swiftmailer as well.
0
 
DamjanCommented:
I use this code for sending pdf files and it works fine:

<?php  
$fileatt = "mypdffile.pdf"; // Path to the file                  
$fileatt_type = "application/pdf"; // File Type  
$fileatt_name = "mypdffile.pdf"; // Filename that will be used for the file as the attachment  
$email_from = "sales@mysite.com"; // Who the email is from  
$email_subject = "Your attached file"; // The Subject of the email  
$email_message = "Thanks for visiting mysite.com!  Here is your free file.<br>";
$email_message .= "Thanks for visiting.<br>"; // Message that the email has in it  
$email_to = "to@mysite.com"; // Who the email is to  
$headers = "From: ".$email_from;  
$file = fopen($fileatt,'rb');  
$data = fread($file,filesize($fileatt));  
fclose($file);  
$semi_rand = md5(time());  
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  
    
$headers .= "\nMIME-Version: 1.0\n" .  
            "Content-Type: multipart/mixed;\n" .  
            " boundary=\"{$mime_boundary}\"";  
$email_message .= "This is a multi-part message in MIME format.\n\n" .  
                "--{$mime_boundary}\n" .  
                "Content-Type:text/html; charset=\"iso-8859-1\"\n" .  
               "Content-Transfer-Encoding: 7bit\n\n" .  
$email_message .= "\n\n";  
$data = chunk_split(base64_encode($data));  
$email_message .= "--{$mime_boundary}\n" .  
                  "Content-Type: {$fileatt_type};\n" .  
                  " name=\"{$fileatt_name}\"\n" .  
                  //"Content-Disposition: attachment;\n" .  
                  //" filename=\"{$fileatt_name}\"\n" .  
                  "Content-Transfer-Encoding: base64\n\n" .  
                 $data .= "\n\n" .  
                  "--{$mime_boundary}--\n";  
$ok = @mail($email_to, $email_subject, $email_message, $headers);  
if($ok) { 
echo "<font face=verdana size=2><center>You file has been sent<br> to the email address you specified.<br> 
Make sure to check your junk mail!<br>
Click <a href=\"#\" onclick=\"history.back();\">here</a> to return to mysite.com.</center>";
} else {  
die("Sorry but the email could not be sent. Please go back and try again!");  
}  
?>

Open in new window

Source: http://www.daniweb.com/web-development/php/threads/60795/send-email-with-attachment

Update: Sorry, I missed that you want attach pdf from url...
0
 
detox1978Author Commented:
I used the second part of x66_x72_x65_x65 to download the PDF and damjanholsedl's to email it.

many thanks
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now