Solved

PHP:  Email a PDF from a URL

Posted on 2013-10-28
6
1,015 Views
Last Modified: 2013-10-28
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
Comment
Question by:detox1978
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:Giovanni Heward
Comment Utility
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
 
LVL 2

Author Comment

by:detox1978
Comment Utility
can you give me a hand with the code?


thanks
0
 
LVL 14

Expert Comment

by:Giovanni Heward
Comment Utility
Sure, is the internal location 80/TCP or 443/TCP?  Is the PDF download a GET or POST request?
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 14

Accepted Solution

by:
Giovanni Heward earned 250 total points
Comment Utility
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
 
LVL 10

Assisted Solution

by:Damjan
Damjan earned 250 total points
Comment Utility
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
 
LVL 2

Author Closing Comment

by:detox1978
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now